【moban】LCT+自带splay模板
跟我一起念zigzag,zigzag,zigzag,zigzag,zigzag,zigzag,zigzag,zigzag,zagzig,zagzig,zagzig,zagzig,zagzig,zagzag,zigzig,zagzag,zagzag,zigzig,zigzig,zigzig,zigzig,zagzag,zigzag,zagzig
#include#include #define zig(x) zigzag(x,1) #define zag(x) zigzag(x,2) using namespace std; const int maxn = 200005; int n,m,rev[maxn],ls[maxn],rs[maxn],fa[maxn],siz[maxn]; bool isroot(int x) { return ls[fa[x]]!=x&&rs[fa[x]]!=x; } void putup(int x) { siz[x]=siz[ls[x]]+siz[rs[x]]+1; } void zigzag(int x,int knd) { int y=fa[x],z=fa[y]; if(!isroot(y)) { if(ls[z]==y) ls[z]=x; else rs[z]=x; } fa[x]=z; fa[y]=x; if(knd==1) //右旋zig { ls[y]=rs[x]; fa[ls[y]]=y; rs[x]=y; } else//左旋 zag { rs[y]=ls[x]; fa[rs[y]]=y; ls[x]=y; } putup(y); putup(x); } void putdowm(int x) { if(!rev[x]) return; swap(ls[x],rs[x]); rev[ls[x]]^=1; rev[rs[x]]^=1; rev[x]=0; } void putdowmall(int x) { if(!isroot(x)) putdowmall(fa[x]); putdowm(x); } void splay(int x) { putdowmall(x); while(!isroot(x)) { int y=fa[x],z=fa[y]; if(isroot(y)) { if(ls[y]==x) zig(x); else zag(x); } else { if(ls[z]==y) { if(ls[y]==x) zig(y),zig(x); else zag(x),zig(x); } else { if(rs[y]==x) zag(y),zag(x); else zig(x),zag(x); } } } } void acc(int x) { for(int y=0;x;y=x,x=fa[x]) { splay(x); rs[x]=y; putup(x); } } void setroot(int x) { acc(x); splay(x); rev[x]^=1; } void link(int a,int b) { setroot(a); fa[a]=b; } void cut(int a,int b) { setroot(a); acc(b); splay(b); ls[b]=fa[a]=0; } int getroot(int a) { acc(a); splay(a); while(ls[a]) a=ls[a]; return a; } int main() { }