1. bzoj 首都
#include<bits/stdc++.h> #define maxn 200000 using namespace std; void setIO(string s) { string in=s+".in",out=s+".out"; freopen(in.c_str(),"r",stdin); } int n,Q,X=0; int siz[maxn],son[maxn],root[maxn]; char str[20]; namespace tr { #define isrt(x) (!(ch[f[x]][0]==x||ch[f[x]][1]==x)) #define get(x) (ch[f[x]][1]==x) #define lson ch[x][0] #define rson ch[x][1] int ch[maxn][2],f[maxn],rev[maxn],sta[maxn]; int findrt(int x) { while(lson) x=lson; return x; } void mark(int x) { if(!x)return; swap(lson,rson), rev[x]^=1; } void pushup(int x) { if(!x)return; siz[x]=siz[lson]+siz[rson]+son[x]+1; } void rotate(int x) { int old=f[x],fold=f[old],which=get(x); if(!isrt(old)) ch[fold][ch[fold][1]==old]=x; ch[old][which]=ch[x][which^1],f[ch[old][which]]=old; ch[x][which^1]=old,f[old]=x,f[x]=fold; pushup(old),pushup(x); } void pushdown(int x) { if(!rev[x]||!x)return; mark(lson), mark(rson),rev[x]^=1; } void splay(int x) { int v=0,u=x,fa; sta[++v]=u; while(!isrt(u)) sta[++v]=f[u],u=f[u]; while(v) pushdown(sta[v--]); for(u=f[u];(fa=f[x])!=u;rotate(x)) if(f[fa]!=u) rotate(get(x)==get(fa)?fa:x); } void Access(int x) { int t=0; while(x) { splay(x); son[x]=son[x]+siz[rson]-siz[t]; pushup(x); t=x,x=f[x]; } } void MakeRoot(int x) { Access(x), splay(x), mark(x); } void link(int x,int y) { MakeRoot(x),f[x]=y,son[y]+=siz[x], pushup(y); } void split(int x,int y) { MakeRoot(x),Access(y),splay(y); } int query(int x) { } }; int main() { setIO("input"); scanf("%d%d",&n,&Q); for(int i=1;i<=n;++i) root[i]=i; int x,y,a,b,c,d; while(Q--) { scanf("%s",str); switch(str[0]) { case 'A' : { scanf("%d%d",&x,&y); a=tr::findrt(x); b=tr::findrt(y); tr::link(x,y); tr::split(a,b); // b is on top of a tr::query(b); break; } case 'Q' : { scanf("%d",&x); break; } case 'X' : { printf("%d\n",X); break; } } } return 0; }
2.