splay rotate 较长但是简单易记的写法

void ro(int x)
{
	int y = fa[x], z = fa[y];
	int sz = ch[z][1] == y;
	int sy = ch[y][1] == x;
	int sx = sy^1;
	int d = ch[x][sx];
	ch[y][sy] = d;
	fa[d] = y;
	ch[x][sx] = y;
	fa[y] = x;
	ch[z][sz] = x;
	fa[x] = z;
	upd(y);
	upd(x);
	if(y==rt) rt=x;
}

当然这个方法在写lct的时候是有弊端的, 以下给出写lct时也可以的写法。


void ro(int x) {
	int y=fa[x], z=fa[y];
	int sz = ch[z][1] == y;
	int sy = ch[y][1] == x;
	int sx = sy ^ 1;
	int d = ch[x][sx];
	
	if(!is(y)) ch[z][sz] = x; // is(x) -> isroot(x)
	fa[x] = z;
	ch[x][sx] = y;
	fa[y] = x;
	ch[y][sy] = d;
	if(d) fa[d] = y;
	
	ud(y), ud(x); // ud(x) -> update(x)
}

原文地址:https://www.cnblogs.com/tztqwq/p/12518593.html