BZOJ3282——Tree
1、题目大意:动态树问题,点修改,链查询xor和
#include <stack> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; namespace LinkCutTree{ struct Node{ Node *ch[2], *fa; int sum, num; bool rev; inline int which(); inline void reverse(){ if(this) rev ^= 1; } inline void pd(){ if(rev){ swap(ch[0], ch[1]); ch[0] -> reverse(); ch[1] -> reverse(); rev = false; } } inline void maintain(){ sum = num ^ ch[0] -> sum ^ ch[1] -> sum; } Node(); } *null = new Node, tree[300010], *pos[300010]; Node::Node(){ num = sum = 0; rev = false; ch[0] = ch[1] = fa = null; } inline int Node::which(){ if(fa == null || (this != fa -> ch[0] && this != fa -> ch[1])) return -1; return this == fa -> ch[1]; } inline void rotate(Node *o){ Node *p = o -> fa; int l = o -> which(), r = l ^ 1; o -> fa = p -> fa; if(p -> which() != -1) p -> fa -> ch[p -> which()] = o; p -> ch[l] = o -> ch[r]; if(o -> ch[r]) o -> ch[r] -> fa = p; o -> ch[r] = p; p -> fa = o; o -> ch[r] -> maintain(); o -> maintain(); } inline void splay(Node *o){ static stack<Node*> st; if(!o) return; Node *p = o; while(1){ st.push(p); if(p -> which() == -1) break; p = p -> fa; } while(!st.empty()){ st.top() -> pd(); st.pop(); } while(o -> which() != -1){ p = o -> fa; if(p -> which() != -1){ if(p -> which() ^ o -> which()) rotate(o); else rotate(p); } rotate(o); } } inline void Access(Node *o){ Node *y = null; while(o != null){ splay(o); o -> ch[1] = y; o -> maintain(); y = o; o = o -> fa; } } inline void MovetoRoot(Node *o){ Access(o); splay(o); o -> reverse(); } inline Node* FindRoot(Node *o){ Access(o); splay(o); while(o -> ch[0] != null) o = o -> ch[0]; return o; } inline void Link(Node *x, Node *y){ MovetoRoot(x); x -> fa = y; } inline void Cut(Node *x, Node *y){ MovetoRoot(x); Access(y); splay(y); y -> ch[0] = x -> fa = null; y -> maintain(); } } int main(){ using namespace LinkCutTree; int n, m; scanf("%d%d", &n, &m); for(int i = 1; i <= n; i ++){ int x; scanf("%d", &x); pos[i] = &tree[i]; pos[i] -> num = pos[i] -> sum = x; } int op, x, y; while(m --){ scanf("%d%d%d", &op, &x, &y); if(op == 0){ MovetoRoot(pos[x]); Access(pos[y]); splay(pos[y]); printf("%d\n", pos[y] -> sum); } else if(op == 1){ if(FindRoot(pos[x]) != FindRoot(pos[y])) Link(pos[x], pos[y]); } else if(op == 2){ if(FindRoot(pos[x]) == FindRoot(pos[y])) Cut(pos[x], pos[y]); } else{ Access(pos[x]); splay(pos[x]); pos[x] -> num = y; pos[x] -> maintain(); } } return 0; }
BZOJ3282——Tree的更多相关文章
- BZOJ3282: Tree
传送门 又是权限题= =,过了NOIp我就要去当一只权限狗! LCT裸题,get到了两个小姿势. 1.LCA操作应该在access中随时updata 2.Link操作可以更简单 void Link(i ...
- BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)
蛋蛋用链剖A的,我写的LCT 3282: Tree Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1241 Solved: 542 [Submit][ ...
- [bzoj3282]Tree (lct)
昨天看了一天的lct..当然幸好最后看懂了(也许吧..) 论善良学长的重要性T_T,老司机带带我! 这题主要是删边的时候还要判断一下..蒟蒻一开始天真的以为存在的边才能删结果吃了一发wa... 事实是 ...
- BZOJ3282: Tree (LCT模板)
Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...
- 【学习心得】Link-cut Tree
Link-cut Tree是一种支持改变树(森林)的形态(link和cut),同时维护树的路径上节点信息的数据结构.lct通过splay来维护每次的perferred path,说白了就是一个动态的树 ...
- AHOI2018训练日程(3.10~4.12)
(总计:共90题) 3.10~3.16:17题 3.17~3.23:6题 3.24~3.30:17题 3.31~4.6:21题 4.7~4.12:29题 ZJOI&&FJOI(6题) ...
- 【BZOJ3282】Tree (Link-Cut Tree)
[BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...
- 【BZOJ3282】Tree LCT
1A爽,感觉又对指针重怀信心了呢= =,模板题,注意单点修改时splay就好,其实按吾本意是没写的也A了,不过应该加上能更好维护平衡性. ..还是得加上好= = #include <iostre ...
- 【bzoj3282】Tree
LCT模板题: 话说xor和的意思是所有数xor一下: #include<iostream> #include<cstdio> #include<cstring> ...
随机推荐
- 【原】dangerouslySetInnerHTML, 让React正常显示你的html代码
昨天在弄一个让内容换行显示时,遇到一个问题,就是我有<br />的代码在页面中不换行,而是直接显示<br />,代码如下: <!DOCTYPE html> < ...
- Microsoft.Office.Interop.Excel的用法
1)ApplicationClass ExcelApp = New ApplicationClass(); 2) 更改 Excel 标题栏: ExcelApp.Caption := '应用程序调用 M ...
- win7怎么显示隐藏文件夹
1. 点击“组织”,再选择“文件夹和搜索选项”命令. 2. 接下来在打开的“文件夹选项”对话框中,单击“查看”,切换到“查看”选项卡中. 3. 然后在下面的“高级设置”区域,取消“隐藏受保护的操作系统 ...
- C#中int,string,char[],char的转换(待续)
//char[]转string string mm = "woshicainiao"; char[] ss = mm.ToCharArray(); string AA = new ...
- hibernate实现有两种配置,xml配置与注释配置。
(1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) <?xml version='1.0' encoding= ...
- c++异常捕获
概念: “C++异常”就是 try{}catch(...){} “SEH异常”就是 __try{} __except(-//){} (关于这两种异常,如有不了解的地方,网上有很多资料可以参考) 目前微 ...
- JavaWeb学习笔记——javabean与表单
- PHP 弹出文件下载
/** * @author default7<default7@zbphp.com> * @description 演示PHP弹出下载的原理 * * @param $file_name * ...
- 第2章 两种调用JS的方法——在HTML中使用JavaScript
一. <script>标记 第一种方法是把 <sript></script>直接放到head和script两个标记之间(title下面,</head>上 ...
- Construct Bounding Sphere
点集的包围球 http://en.wikipedia.org/wiki/Bounding_sphere http://blogs.agi.com/insight3d/index.php/2008/02 ...