LCT简洁模板

搬运自此

struct LCT {
	typedef int arr[N];
	arr fa,ch[2],rev;
	#define lc ch[0][x]
	#define rc ch[1][x]
	
	void upd(int x) {...}
	bool son(int x) {return ch[1][fa[x]] == x;}
	bool nroot(int x) {return ch[son(x)][fa[x]] == x;}
	void rotate(int x) {
		int y=fa[x],z=fa[y],c=son(x);
		ch[c][y]=ch[c^1][x]; if(ch[c][y]) fa[ch[c][y]]=y;
		if(nroot(y)) ch[son(y)][z]=x; fa[x]=z;
		ch[c^1][x]=y; fa[y]=x; upd(y); upd(x);
	}
	void reverse(int x) { if(x) swap(lc,rc),rev[x] ^= 1;}
	void pushdown(int x) { if(rev[x]) reverse(lc),reverse(rc),rev[x]=0;}
	void dfs(int x) {if(nroot(x)) dfs(fa[x]); pushdown(x);}
	void splay(int x) {
		dfs(x);
		for(int y=fa[x]; nroot(x); rotate(x),y=fa[x])
			if(nroot(y)) rotate(son(x)^son(y)?x:y);
	}
	void access(int x) {for(int y=0; x; x=fa[y=x]) splay(x), rc=y, upd(x);}
	void makeroot(int x) {access(x); splay(x); reverse(x);}
	int findroot(int x) { access(x); splay(x); while(lc) x=lc,pushdown(x); return splay(x),x;}
	void link(int x,int y) {makeroot(x); if(findroot(y)^x) fa[x]=y; access(x);}
	void cut(int x,int y) { makeroot(x); if(findroot(y) == x && fa[y] == x && !ch[0][y]) {rc=fa[y]=0; upd(x); } }
	void split(int x,int y) {makeroot(y); access(x); splay(x);}
} s;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值