hdu 3487 Play with Chain(splay)

/*

这是我第一次做splay树的题,看了一天的有关splay博客和很多有关大牛的解题报告,终于把它拿下了;

有关splay树的实现可以看这个博客,讲解可以看这个博客,讲的不错,做题看这个博客;


这题要在splay树上执行两个操作

第一个是区间的转移:

        这里又分两小步,第一小步是把区间摘下来,第二小步是把摘下的区间放到指定的位置;

        要实现第一小步,必须想办法把这个区间变成一颗splay子树(用splay操作实现),并让指定位置的右子树为空(用splay操作实现)

        最后把这棵splay子树插入到指定位置的右子树就好了,这样摘下插入就实现了;


第二个是区间的逆序:

        这里只要在指定区间递归交换一下左右子树就可以了,不过具体实现的时候,不能每次都更新,不然会超时,不过我们可以采用lazy思想

        要访问时再更新,这样就不会超时了;


这题的代码如下 :        

*/


#include<stdio.h>
#include<string.h>
#define N 300010
int tree[N][2],pa[N],cnt[N],rev[N];
void update(int t)
{
	cnt[t]=cnt[tree[t][0]]+cnt[tree[t][1]]+1;
}
void rotate(int t,int c)
{
	int p=pa[t];
	tree[p][!c]=tree[t][c];
	if(tree[t][c])pa[tree[t][c]]=p;
	tree[t][c]=p;	
	pa[t]=pa[p];
	if(pa[p])tree[pa[p]][tree[pa[p]][1]==p]=t;
	pa[p]=t;
	update(p);
	update(t);	
}

void splay(int x,int &head)
{
	int p,pp;
	while(pa[x]&&x!=head)
	{
		p=pa[x];
		if(p==head)
		{
			if(tree[p][1]==x)rotate(x,0);
			else rotate(x,1);
		}
		else 
		{
			pp=pa[p];
			if(tree[pp][0]==p&&tree[p][0]==x){ rotate(p,1);rotate(x,1);}
			else if(tree[pp][1]==p&&tree[p][1]==x){ rotate(p,0);rotate(x,0);}
			else if(tree[pp][1]==p&&tree[p][0]==x){ rotate(x,1);rotate(x,0);}
			else {rotate(x,0);rotate(x,1);}	
		}
	}
	if(!pa[x])head=x;
}
void push(int t)
{
	if(t&&rev[t])
	{
		int temp=tree[t][0];
		tree[t][0]=tree[t][1];
		tree[t][1]=temp;
		rev[t]=0;
		if(tree[t][0])
			rev[tree[t][0]]^=1;
		if(tree[t][1])
			rev[tree[t][1]]^=1;
	}
}
int find(int k,int head)
{
	int t;
	push(head);
	t=cnt[tree[head][0]];
	if(k==t+1)return head;
	if(k<t+1)return find(k,tree[head][0]);
	else return find(k-t-1,tree[head][1]);
}
int ans[N],count;
void output(int head)
{
	if(!head)return;
	if(rev[head])push(head);
	output(tree[head][0]);
	ans[count++]=head;
	output(tree[head][1]);
}
void print(int head)
{
	int i;
	count=0;
	output(head);
	for(i=1;i<count-2;i++)
		printf("%d ",ans[i]-1);
	printf("%d\n",ans[i]-1);
}
void Cut(int &head)
{
	int a,b,c,l;
	scanf("%d%d%d",&a,&b,&c);
	splay(find(a,head),head);
	splay(find(b-a+2,tree[head][1]),tree[head][1]);
	l=tree[tree[head][1]][0];tree[tree[head][1]][0]=0;
	update(tree[head][1]);update(head);
	splay(find(c+2,head),head);
	splay(find(c+1,tree[head][0]),tree[head][0]);
	tree[tree[head][0]][1]=l;
	pa[l]=tree[head][0];
	update(tree[head][0]);update(head);
}
void Flip(int &head)
{
	int a,b;
	scanf("%d%d",&a,&b);
	splay(find(a,head),head);
	splay(find(b-a+2,tree[head][1]),tree[head][1]);
	rev[tree[tree[head][1]][0]]^=1;
}
int main()
{
	char op[15];
	int n,m,i,head;
	while(scanf("%d%d",&n,&m),n>=0||m>=0)
	{
		memset(rev,0,sizeof(rev));
		memset(tree,0,sizeof(tree));
		for(i=1;i<=n+2;i++)
		{
			pa[i-1]=i;
			cnt[i]=i;
			tree[i][0]=i-1;
		}
		head=n+2;pa[head]=0;pa[0]=cnt[0]=0;
		while(m--)
		{
			scanf("%s",op);
			if(op[0]=='C')Cut(head);
			else Flip(head);
		}
		print(head);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值