堆板子计划

LCT*3
struct LCT{
	#define lch V[p].son[0]
	#define rch V[p].son[1]
	#define Son(p) (V[V[p].fa].son[1]==p)
	#define Isroot(p) (V[V[p].fa].son[0]!=p && V[V[p].fa].son[1]!=p)
	struct Ver{int siz,son[2],fa,t1;}V[maxn];
	int tot;
	int New(int x,int fa){++tot;V[tot].fa=fa;V[tot].siz=1;return tot;}
	
	void Pushup(int p){V[p].siz=V[lch].siz+V[rch].siz+1;}
	void Upt1(int p){swap(lch,rch);V[p].t1^=1;}
	void Pushdown(int p){if(V[p].t1)Upt1(lch),Upt1(rch);V[p].t1=0;}
	void Spread(int p){if(!Isroot(p))Spread(V[p].fa);Pushdown(p);}

	void Rotate(int p){
		int f=V[p].fa,lf=V[f].fa,s=Son(p),ls=Son(f);
		if(!Isroot(f))V[lf].son[ls]=p;
		V[p].fa=lf;
		V[f].son[s]=V[p].son[s^1],V[V[p].son[s^1]].fa=f;;
		V[p].son[s^1]=f,V[f].fa=p;
		Pushup(f),Pushup(p);
	}
	void Splay(int p){
		Spread(p);
		for(int f=V[p].fa;!Isroot(p);Rotate(p),f=V[p].fa)
			if(!Isroot(f))Rotate(Son(f)==Son(p) ? f : p);
	}

	void Access(int x){int p;for(p=0;x;p=x,x=V[x].fa)Splay(x),V[x].son[1]=p,Pushup(x);}
	void Makeroot(int x){Access(x),Splay(x),Upt1(x);}
	int Findroot(int p){Access(p),Splay(p),Pushdown(p);while(lch)p=lch,Pushdown(p);Splay(p);return p;}
	void Link(int x,int y){if(Findroot(x)==Findroot(y))return;Makeroot(x),V[x].fa=y;}
	void Split(int x,int y){Makeroot(x),Access(y),Splay(y);}
	void Cut(int x,int y){Split(x,y);if(V[y].son[0]==x && V[x].son[1]==0)V[y].son[0]=V[x].fa=0;Pushup(y);}
}L;
posted @ 2022-07-25 07:46  Delov  阅读(48)  评论(1编辑  收藏  举报