# 洛谷SP16549 QTREE6 - Query on a tree VI（LCT)

## 思路分析

#include<cstdio>
#include<cstdlib>
#define R register int
#define I inline void
const int N=1000009,M=N<<1;
#define lc c[x][0]
#define rc c[x][1]
#define C col[u]
int fa[N],he[N],ne[M],to[M];
bool col[N];
struct LCT{
int f[N],c[N][2],si[N],s[N],h[N];
bool r[N];
LCT(){for(R i=1;i<N;++i)s[i]=1;}//注意初始化
inline bool nroot(R x){return c[f[x]][0]==x||c[f[x]][1]==x;}
I pushup(R x){
s[x]=s[lc]+s[rc]+si[x]+1;
}
I rotate(R x){
R y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
if(nroot(y))c[z][c[z][1]==y]=x;c[x][!k]=y;c[y][k]=w;
f[w]=y;f[y]=x;f[x]=z;
pushup(y);
}
I splay(R x){
R y;
while(nroot(x)){
if(nroot(y=f[x]))rotate((c[f[y]][0]==y)^(c[y][0]==x)?x:y);
rotate(x);
}
pushup(x);
}
I access(R x){
for(R y=0;x;x=f[y=x]){
splay(x);
si[x]+=s[rc];
si[x]-=s[rc=y];
}
}
inline int findroot(R x){
access(x);splay(x);
while(lc)x=lc;
splay(x);
return x;
}
splay(x);//不用access(x)，因为x一定是连通块的根
R y=f[x]=fa[x];
access(y);splay(y);//与常规LCT不同，别忘加
si[y]+=s[x];s[y]+=s[x];
}
I cut(R x){
access(x);splay(x);
lc=f[lc]=0;
pushup(x);
}
}lct[2];
void dfs(R x){
for(R y,i=he[x];i;i=ne[i])
if((y=to[i])!=fa[x])
}
#define G ch=getchar()
#define in(z) G;\
while(ch<'-')G;\
z=ch&15;G;\
while(ch>'-')z*=10,z+=ch&15,G
int main(){
register char ch;
R p=1,n,m,i,u,v,op;
in(n);
for(i=1;i<n;++i){
in(u);in(v);
to[++p]=v;ne[p]=he[u];he[u]=p;
to[++p]=u;ne[p]=he[v];he[v]=p;
}
dfs(1);