昨天看了一天的lct。。当然幸好最后看懂了(也许吧。。)
论善良学长的重要性T_T,老司机带带我!
这题主要是删边的时候还要判断一下。。蒟蒻一开始天真的以为存在的边才能删结果吃了一发wa。。。
事实是只要两个点之间联通就能断开了,管它有没有边。。。。整个就一模板题。。
交上去后跑得很慢(记录类型的锅)。。但还是很短?(请自行无视过长变量名)
1 #include<cstdio> 2 #include<math.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=300233; 7 struct zs{ 8 int c[2],fa,val,sum; 9 bool rev; 10 }tree[maxn]; 11 int i,j,n,m,id,x,y; 12 int stack[maxn]; 13 inline void update(int x){tree[x].sum=tree[tree[x].c[0]].sum^tree[x].val^tree[tree[x].c[1]].sum; 14 } 15 inline bool isroot(int x){return tree[tree[x].fa].c[0]!=x&&tree[tree[x].fa].c[1]!=x; 16 } 17 void rotate(int x){ 18 int fa=tree[x].fa,gfa=tree[fa].fa; 19 if(!isroot(fa))tree[gfa].c[tree[gfa].c[1]==fa]=x; 20 int l=tree[fa].c[1]==x,r=l^1; 21 tree[fa].c[l]=tree[x].c[r];tree[x].c[r]=fa; 22 tree[fa].fa=x;tree[x].fa=gfa;tree[tree[fa].c[l]].fa=fa; 23 update(fa);update(x); 24 } 25 void pushdown(int x){ 26 if(!tree[x].rev)return; 27 int l=tree[x].c[0],r=tree[x].c[1]; 28 if(l)tree[l].rev^=1;if(r)tree[r].rev^=1; 29 swap(tree[x].c[0],tree[x].c[1]);tree[x].rev^=1; 30 } 31 void splay(int x){ 32 int top=0,tmp=x;stack[++top]=x; 33 while(!isroot(tmp))stack[++top]=tree[tmp].fa,tmp=tree[tmp].fa; 34 while(top)pushdown(stack[top]),top--; 35 int fa,gfa; 36 while(!isroot(x)){ 37 fa=tree[x].fa,gfa=tree[fa].fa; 38 if(!isroot(fa)) 39 if((tree[gfa].c[0]==fa)^(tree[fa].c[0]==x))rotate(x); 40 else rotate(fa); 41 rotate(x); 42 } 43 } 44 void access(int x){ 45 int son=0; 46 while(x){ 47 splay(x);tree[x].c[1]=son; 48 update(x); 49 son=x;x=tree[x].fa; 50 } 51 } 52 void makeroot(int x){ 53 access(x);splay(x);tree[x].rev^=1; 54 } 55 void link(int x,int y){ 56 makeroot(x);tree[x].fa=y;splay(x); 57 } 58 void cut(int x,int y){ 59 makeroot(x);access(y);splay(y);tree[y].c[0]=tree[x].fa=0; 60 } 61 int query(int x,int y){ 62 makeroot(x);access(y);splay(y);return tree[y].sum; 63 } 64 int getfa(int x){ 65 access(x);splay(x);while(tree[x].c[0])pushdown(x),x=tree[x].c[0];splay(x); 66 return x; 67 } 68 void change(int x,int y){ 69 makeroot(x);tree[x].val=y;update(x); 70 } 71 int main(){ 72 scanf("%d%d",&n,&m); 73 for(i=1;i<=n;i++)scanf("%d",&tree[i].val),tree[i].sum=tree[i].val; 74 while(m--){ 75 scanf("%d%d%d",&id,&x,&y); 76 if(id==0)printf("%d\n",query(x,y)); 77 else if(id==1){if(getfa(x)!=getfa(y))link(x,y);} 78 else if(id==2){if(getfa(x)==getfa(y))cut(x,y);} 79 else if(id==3)change(x,y); 80 } 81 return 0; 82 }