<span>LCT板子</span>

#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
int fa[N], ch[N][2], sum[N], val[N], rev[N];
inline void update(int x) { sum[x] = sum[ls(x)] ^ sum[rs(x)] ^ val[x]; }
inline int nroot(int x) { return ls(fa[x]) == x || rs(fa[x]) == x; }
inline int get(int x) { return rs(fa[x]) == x; }
inline void rotate(int x) {
	int y = fa[x], z = fa[y], k = get(x), w = ch[x][!k];
	if(nroot(y)) ch[z][get(y)] = x; ch[x][!k] = y; ch[y][k] = w;
	if(w) fa[w] = y; fa[y] = x; fa[x] = z; update(y);
}
inline void pushdown(int x) {
	if(rev[x]) {swap(ls(x), rs(x));
		if(ls(x)) rev[ls(x)] ^= 1; if(rs(x)) rev[rs(x)] ^= 1;
		 rev[x] = 0;
	}
}
inline void pushall(int x) { if(nroot(x)) pushall(fa[x]); pushdown(x); }
inline void splay(int x) {
	pushall(x);
	while(nroot(x)) {
		if(nroot(fa[x])) rotate(get(x) ^ get(fa[x]) ? x : fa[x]);
		rotate(x);
	} update(x);
}
inline void access(int x) {
	for(R int y = 0; x; x = fa[y = x]) splay(x), rs(x) = y, update(x);
}
inline void makeroot(int x) { access(x); splay(x); rev[x] ^= 1; }
inline void split(int x, int y) { makeroot(x); access(y); splay(y); }
inline int findroot(int x) {
	access(x); splay(x); while(ls(x)) pushdown(x), x = ls(x); splay(x); return x;
}
inline void link(int x, int y) {
	makeroot(x);  if(findroot(y) != x) fa[x] = y, update(y);
}
inline void cut(int x, int y) {
	makeroot(x);
	if(findroot(y) == x && fa[y] == x && ls(y) == 0) ch[x][1] = fa[y] = 0, update(x);
}
全部评论

相关推荐

屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
爱吃肉的伊登在写日记:好棒,27届简历能做成这个样子,但是第一个项目感觉cover住难度还是不小的,特别是二面的时候肯定要对分布式系统设计这一块儿有高出正常面试者的水平才行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务