2025-10-28 2025-10-28 WARNING 平衡树还没更完写什么LCT 题目传送门 $ Code $123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132#include<bits/stdc++.h>#define ll long long#define ma 5114511#define il inlineusing namespace std;const ll inf=1e18+10;ll read(){ char ch=getchar(); ll x=0,f=1; while('0'>ch||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while('0'<=ch&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f;}void write(ll x){ if(x<0){ putchar('-'); x=-x; } if(x>=10){ write(x/10); } putchar(x%10+'0');}//--------------------------------------------ll n,m;//--------------------------------------------struct Splay{ ll son[2]; ll fa; ll val,sum; ll tg;}t[ma<<2];ll st[ma];ll which(ll x){ return t[t[x].fa].son[1]==x;}ll noroot(ll x){ return t[t[x].fa].son[which(x)]==x;}void pushup(ll x){ t[x].sum=t[t[x].son[0]].sum^t[t[x].son[1]].sum^t[x].val;}void reverser(ll x){ swap(t[x].son[0],t[x].son[1]); t[x].tg^=1;}void down(ll x){ if(t[x].tg){ if(t[x].son[0]) reverser(t[x].son[0]); if(t[x].son[1]) reverser(t[x].son[1]); t[x].tg=0; }}void rotate(ll x){ ll y=t[x].fa,z=t[y].fa,k=which(x),son=t[x].son[!k]; if(noroot(y)) t[z].son[which(y)]=x;t[x],t[x].son[!k]=y;t[y].son[k]=son; if(son) t[son].fa=y;t[y].fa=x;t[x].fa=z; pushup(y);}void splay(ll x){ ll y=x,z=0; st[++z]=y; while(noroot(y)) st[++z]=y=t[y].fa; while(z) down(st[z--]); while(noroot(x)){ y=t[x].fa,z=t[y].fa; if(noroot(y)) which(x)^which(y)?rotate(x):rotate(y); rotate(x); } pushup(x);}//--------------------------------------------void access(ll x){ for(ll y=0;x;y=x,x=t[x].fa){ splay(x),t[x].son[1]=y,pushup(x); }}void make_root(ll x){ access(x),splay(x); reverser(x);}ll find_root(ll x){ access(x),splay(x); while(t[x].son[0]) down(x),x=t[x].son[0]; splay(x); return x;}void split(ll x,ll y){ make_root(x); access(y),splay(y);}void link(ll x,ll y){ make_root(x); if(find_root(y)!=x) t[x].fa=y;}void cut(ll x,ll y){ make_root(x); if(find_root(y)==x&&t[y].fa==x&&!t[y].son[0]){ t[y].fa=t[x].son[1]==0; pushup(x); }}//--------------------------------------------int main(){ n=read(),m=read(); for(ll i=1;i<=n;i++) t[i].val=read(); while(m--){ ll op=read(); ll x=read(),y=read(); if(op==0){ split(x,y); printf("%lld\n",t[y].sum); } if(op==1){ link(x,y); } if(op==2){ cut(x,y); } if(op==3){ splay(x); t[x].val=y; } } return 0;} Prev 最大流模板 Next 平衡树模板