萌新刚学OI,求助 Splay 旋转/kk
查看原帖
萌新刚学OI,求助 Splay 旋转/kk
298549
SIXIANG32楼主2020/10/28 13:25
struct node{
		int son[2],fa,val;
		int size,cnt;
	}tr[100010];
	//0:l 1:r
	/*void test(int x)
	{
		if(tr[x].son[0])test(tr[x].son[0]);
		cout<<x<<' ';
		if(tr[x].son[1])test(tr[x].son[1]);
	}
	void test2(int x) 
	{
		cout<<x<<' ';
		if(tr[x].son[0])test(tr[x].son[0]);
		if(tr[x].son[1])test(tr[x].son[1]);
	}*/
	bool LorR(int x){return tr[tr[x].fa].son[0]==x?0:1;}
	void updata(int x){tr[x].size=tr[tr[x].son[0]].size+tr[tr[x].son[1]].size+tr[x].cnt;}
	void link(int x,int fa,int son)
	{
		tr[x].fa=fa;
		tr[fa].son[son]=x;
	}
	void rotate(int x)
	{
	    int y=tr[x].fa,m=tr[y].fa;
	    int ms=LorR(y),ys=LorR(x);
	    int B=tr[x].son[ys^1];
	    link(B,y,ys),link(y,x,(ys^1)),link(x,m,ms);
	    updata(y);updata(x);
	}
	void splay(int x,int to)
	{
		to=tr[to].fa;
		while(tr[x].fa!=to)
		{
			int fat=tr[x].fa;
			if(tr[fat].fa==to)rotate(x);
			else if(LorR(x)==LorR(fat))rotate(fat),rotate(x);
			else rotate(x),rotate(x); 
		}
	}

旋转张这样
目测旋转出错,然而鹅从昨天就开始调试但是没看出来哪里错了/kk

求助啊/kk

2020/10/28 13:25
加载中...