bzoj3282: Tree
来源:互联网 发布:ubuntu网络连接不上 编辑:程序博客网 时间:2024/06/02 00:13
#include<cstdio>#include<cstring>#include<algorithm>#define ls c[x][0]#define rs c[x][1]using namespace std;const int maxn=300010;int n,m; struct LCT{ int c[maxn][2],fa[maxn],rev[maxn],sum[maxn],val[maxn]; bool isroot(int x){return (c[fa[x]][0]!=x)&&(c[fa[x]][1]!=x);} int which(int x){return c[fa[x]][1]==x;} void update(int x){sum[x]=sum[c[x][0]]^sum[c[x][1]]^val[x];} void flip(int x){swap(c[x][0],c[x][1]),rev[x]^=1;} void down(int x){if (rev[x]) flip(c[x][0]),flip(c[x][1]),rev[x]=0;} void relax(int x){if (!isroot(x)) relax(fa[x]);down(x);} void rotate(int x){ int y=fa[x],z=fa[y],nx=which(x),ny=which(y); fa[c[x][!nx]]=y,c[y][nx]=c[x][!nx]; fa[x]=z;if (!isroot(y)) c[z][ny]=x; fa[y]=x,c[x][!nx]=y;update(y); } void splay(int x){ relax(x); while (!isroot(x)){ if (isroot(fa[x])) rotate(x); else if (which(x)==which(fa[x])) rotate(fa[x]),rotate(x); else rotate(x),rotate(x); } update(x); } void access(int x){for (int p=0;x;x=fa[x]) splay(x),fa[c[x][1]=p]=x,update(x),p=x;} void makeroot(int x){access(x),splay(x),flip(x);} void modify(int x,int v){val[x]=v,splay(x);} int findroot(int x){access(x),splay(x);for (;c[x][0];x=c[x][0]);return x;} void link(int a,int b){makeroot(a),fa[a]=b;} void cut(int a,int b){makeroot(a),access(b),splay(b);if (c[b][0]==a) c[b][0]=fa[a]=0;} int query(int a,int b){makeroot(a),access(b),splay(b);return sum[b];}}T; int main(){ scanf("%d%d",&n,&m); for (int i=1,a;i<=n;i++) scanf("%d",&a),T.modify(i,a); for (int i=1,op,a,b;i<=m;i++){ scanf("%d%d%d",&op,&a,&b); if (op==0) printf("%d\n",T.query(a,b)); else if (op==1){if (!(T.findroot(a)==T.findroot(b))) T.link(a,b);} else if (op==2) T.cut(a,b); else T.modify(a,b); } return 0;}
又是一道tree...
题目大意:就是说的直接的bzoj2819:NIM,加边删边,单点修改。
思路:LCT维护链上的异或和,注意题目中的不保证。
0 0
- bzoj3282: Tree
- 【BZOJ3282】Tree
- [BZOJ3282] Tree
- 【bzoj3282】Tree
- BZOJ3282: Tree
- bzoj3282 tree
- 【bzoj3282】Tree LCT
- 【bzoj3282】【Tree】【lct】
- [BZOJ3282]Tree(LCT)
- [bzoj3282]tree 解题报告
- BZOJ3282——Tree
- [BZOJ3282][LCT]Tree
- 【bzoj3282】Tree LCT
- 【BZOJ3282】Tree【Link-Cut Tree】
- [bzoj3282]Tree Link-Cut-Tree
- 【BZOJ3282】Tree (Link-Cut Tree)
- [BZOJ3282/LGOJ3690] Link-Cut Tree 模板题
- BZOJ3282【LCT】
- In memory with Mr. Vijay Parthasarathy, my respected ex-boss, passed away at Farther's day
- 实战Linux下防火墙iptables设置
- 播放视频内容
- JSTL标签用法 详解
- Java集合1:HashMap的实现原理
- bzoj3282: Tree
- Java集合2:HashSet的实现原理
- 黑马程序员--C语言关键字,注释
- 打开CCS5 出现locking is not possible in the directory
- 黑马程序员--递归方法参数的自增自减
- Java集合3:LinkedHashMap的实现原理
- CSU1659: Graph Center(最短路)
- Java中Unicode编码和汉字之间的转换
- HelloVelocity