Link Cut Tree留坑
LCT:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#include<bitset>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dwn(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
const int N=300000;
int n,m,fa[N+10],v[N+10],sum[N+10],ch[N+10][2],st[N+10];
bool rev[N+10];
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return x*f;
}
inline bool isroot(int x){return !(ch[fa[x]][0]==x||ch[fa[x]][1]==x);}
inline void pushup(int x){sum[x]=sum[ch[x][0]]^sum[ch[x][1]]^v[x];}
inline void pushrev(int x){rev[x]^=1;swap(ch[x][0],ch[x][1]);}
inline void pushdown(int x)
{
if(rev[x])
{
rev[x]=0;
if(ch[x][0]) pushrev(ch[x][0]);
if(ch[x][1]) pushrev(ch[x][1]);
}
}
inline int ident(int x){return x==ch[fa[x]][0]?0:1;}
inline void rotate(int x)
{
int Y=fa[x],R=fa[Y],Yson=ident(x),Rson=ident(Y);
if(!isroot(Y)) ch[R][Rson]=x;
if(ch[x][Yson^1]) fa[ch[x][Yson^1]]=Y;
ch[Y][Yson]=ch[x][Yson^1];
ch[x][Yson^1]=Y;
fa[Y]=x;
fa[x]=R;
pushup(Y);pushup(x);
}
inline void splay(int x)
{
int y=x,cnt=0;
st[++cnt]=y;
while(!isroot(y)) st[++cnt]=fa[y],y=fa[y];
while(cnt) pushdown(st[cnt--]);
while(!isroot(x))
{
y=fa[x];
if(isroot(y)) rotate(x);
else if(ident(x)==ident(y)) rotate(y),rotate(x);
else rotate(x),rotate(x);
}
}
inline void access(int x)
{
for(int y=0;x;x=fa[y=x])
splay(x),ch[x][1]=y,pushup(x);
}
inline void makeroot(int x)
{
access(x),splay(x),pushrev(x);
}
inline int findroot(int x)
{
access(x),splay(x);
while(ch[x][0]) pushdown(x),x=ch[x][0];
splay(x);
return x;
}
inline void split(int x,int y)
{
makeroot(x),access(y),splay(y);
}
inline void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x) fa[x]=y;
}
inline void cut(int x,int y)
{
makeroot(x);
if(findroot(y)!=x||fa[y]!=x||ch[y][0]) return;
fa[y]=ch[x][1]=0;
pushup(x);
}
int main()
{
n=read(),m=read();
rep(i,1,n) v[i]=read();
rep(i,1,m)
{
int opt=read(),x=read(),y=read();
if(opt==0){split(x,y);printf("%d
",sum[y]);}
if(opt==1){link(x,y);}
if(opt==2){cut(x,y);}
if(opt==3){splay(x);v[x]=y;pushup(x);}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#include<bitset>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dwn(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
const int N=100000,mod=51061;
unsigned int n,m,mul[N+10],add[N+10],sum[N+10],sz[N+10],v[N+10],ch[N+10][2],fa[N+10];
bool rev[N+10];
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return x*f;
}
inline bool isroot(int x)
{
return !(ch[fa[x]][0]==x||ch[fa[x]][1]==x);
}
inline void pushup(int x)
{
sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
sum[x]=(sum[ch[x][0]]+sum[ch[x][1]]+v[x])%mod;
}
inline void pushrev(int x)
{
rev[x]^=1,swap(ch[x][0],ch[x][1]);
}
inline void pushmul(int x,int d)
{
sum[x]=sum[x]*d%mod;
v[x]=v[x]*d%mod;
add[x]=add[x]*d%mod;
mul[x]=mul[x]*d%mod;
}
inline void pushadd(int x,int d)
{
sum[x]=(sum[x]+sz[x]*d)%mod;
v[x]=(v[x]+d)%mod;
add[x]=(add[x]+d)%mod;
}
inline void pushdown(int x)
{
int l=ch[x][0],r=ch[x][1];
if(mul[x]!=1)
{
pushmul(l,mul[x]);
pushmul(r,mul[x]);
mul[x]=1;
}
if(add[x])
{
pushadd(l,add[x]);
pushadd(r,add[x]);
add[x]=0;
}
if(rev[x])
{
rev[x]=0;
if(l) pushrev(l);
if(r) pushrev(r);
}
}
inline int ident(int x){return x==ch[fa[x]][0]?0:1;}
inline void rotate(int x)
{
int Y=fa[x],R=fa[Y],Yson=ident(x),Rson=ident(Y);
if(!isroot(Y)) ch[R][Rson]=x;
if(ch[x][Yson^1]) fa[ch[x][Yson^1]]=Y;
ch[Y][Yson]=ch[x][Yson^1];
ch[x][Yson^1]=Y;
fa[Y]=x;
fa[x]=R;
pushup(Y),pushup(x);
}
void pushall(int x)
{
if(!isroot(x)) pushall(fa[x]);
pushdown(x);
}
inline void splay(int x)
{
pushall(x);
while(!isroot(x))
{
int y=fa[x];
if(isroot(y)) rotate(x);
else if(ident(x)==ident(y)) rotate(y),rotate(x);
else rotate(x),rotate(x);
}
}
inline void access(int x)
{
for(int y=0;x;x=fa[y=x])
splay(x),ch[x][1]=y,pushup(x);
}
inline void makeroot(int x)
{
access(x),splay(x),pushrev(x);
}
inline void split(int x,int y)
{
makeroot(x),access(y),splay(y);
}
inline void link(int x,int y)
{
makeroot(x),fa[x]=y;
}
inline void cut(int x,int y)
{
split(x,y),fa[x]=ch[y][0]=0;
}
int main()
{
n=read(),m=read();
rep(i,1,n) v[i]=mul[i]=sz[i]=1;
rep(i,1,n-1)
{
int u=read(),v=read();
link(u,v);
}
rep(i,1,m)
{
char ch=getchar();
while(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/') ch=getchar();
if(ch=='+')
{
int u=read(),v=read(),d=read();
split(u,v),pushadd(v,d);
}
if(ch=='-')
{
int u1=read(),v1=read(),u2=read(),v2=read();
cut(u1,v1),link(u2,v2);
}
if(ch=='*')
{
int u=read(),v=read(),d=read();
split(u,v),pushmul(v,d);
}
if(ch=='/')
{
int u=read(),v=read();
split(u,v);
printf("%u
",sum[v]);
}
}
return 0;
}