题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3282
LCT模板题….
判断是否可以cut改一改就好…以前做的都是保证可以直接cut的…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
#include<bits/stdc++.h> #define MAXN 300030 #define ls son[x][0] #define rs son[x][1] typedef long long ll; using namespace std; inline int rd(){ int x=0,y=1;char c=getchar(); while(c<'0' || c>'9'){if(c=='-')y=-y;c=getchar();} while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); return x*y; } int n,m; int son[MAXN][2],fa[MAXN],stk[MAXN]; int val[MAXN],sz[MAXN]; ll sum[MAXN]; bool rev[MAXN]; inline bool isroot(int x){ return son[fa[x]][0]!=x&&son[fa[x]][1]!=x; } inline bool is_right(int x){ return son[fa[x]][1]==x; } inline void pushup(int x){ sz[x]=sz[ls]+sz[rs]+1; sum[x]=sum[ls]^sum[rs]^val[x]; } inline void pushdown(int x){ if(rev[x]){ rev[x]^=1,rev[ls]^=1,rev[rs]^=1; swap(ls,rs); } } inline void Rotate(int x){ int f=fa[x],ff=fa[f],which=is_right(x); if(!isroot(f))son[ff][son[ff][1]==f]=x; son[f][which]=son[x][which^1]; fa[son[f][which]]=f;fa[f]=x; son[x][which^1]=f;fa[x]=ff; pushup(f);pushup(x); } void getdown(int x){ if(!isroot(x))getdown(fa[x]); pushdown(x); } inline void splay(int x){ getdown(x); while(!isroot(x)){ int f=fa[x]; if(!isroot(f))Rotate((is_right(x)==is_right(f)?f:x)); Rotate(x); } } inline void access(int x){ int t=0; while(x){ splay(x); rs=t; pushup(x); t=x;x=fa[x]; } } inline void toroot(int x){ access(x);splay(x); rev[x]^=1; } inline void link(int x,int y){ toroot(x); fa[x]=y; splay(x); } inline void split(int x,int y){ toroot(x);access(y);splay(y); } inline int Find(int x){ access(x);splay(x); while(ls)x=ls,pushdown(x); return x; } inline void cut(int x,int y){ if(Find(x)!=Find(y))return; split(x,y); if(son[y][0]!=x || fa[x]!=y || son[x][1])return; son[y][0]=fa[x]=0; } int main(){ n=rd(),m=rd(); for(int i=1; i<=n; i++)val[i]=sum[i]=rd(),sz[i]=1; int u,v,op; while(m--){ op=rd(),u=rd(),v=rd(); if(op==0)split(u,v),printf("%lld\n",sum[v]); if(op==1){ int x=Find(u),y=Find(v); if(x!=y)link(u,v); } if(op==2)cut(u,v); if(op==3){ splay(u); val[u]=v; pushup(u); } } return 0; } |