HDU 3487 Play with Chain

Problem Description
YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.
At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.

FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8

He wants to know what the chain looks like after perform m operations. Could you help him?
 

Input
There will be multiple test cases in a test data.
For each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively.
Then m lines follow, each line contains one operation. The command is like this:
CUT a b c // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).
FLIP a b    // Means a FLIP operation, 1 ≤ a < b ≤ n.
The input ends up with two negative numbers, which should not be processed as a case.
 

Output
For each test case, you should print a line with n numbers. The ith number is the number of the ith diamond on the chain.
 

Sample Input
8 2 CUT 3 5 4 FLIP 2 6 -1 -1
 

Sample Output
1 4 3 7 6 2 5 8
 
splay基本操作,插入就是剪下来,插进去就好了,注意:能fnd就不要写其他的,多fnd几次总没有错。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=300005;
int n,m,rt,sz[N],fa[N],ch[N][2],tag[N];
void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
}
void pushdown(int x)
{
	if(!tag[x])
		return;
	tag[ch[x][0]]^=1;
	tag[ch[x][1]]^=1;
	swap(ch[x][0],ch[x][1]);
	tag[x]=0;
}
void rotate(int x,int &k)
{
	int y=fa[x],z=fa[y],lc=ch[y][1]==x,rc=lc^1;
	if(y==k)
		k=x;
	else
		ch[z][ch[z][1]==y]=x;
	fa[x]=z,fa[y]=x,fa[ch[x][rc]]=y;
	ch[y][lc]=ch[x][rc],ch[x][rc]=y;
	pushup(y),pushup(x);
}
void splay(int x,int &k)
{
	while(x!=k)
	{
		int y=fa[x],z=fa[y];
		if(y!=k)
		{
			if((ch[y][0]==x)^(ch[z][0]==y))
				rotate(x,k);
			else
				rotate(y,k);
		}
		rotate(x,k);
	}
}
int fnd(int x,int rnk)
{
	pushdown(x);
	int lc=ch[x][0],rc=ch[x][1];
	if(rnk<=sz[lc])
		return fnd(lc,rnk);
	if(rnk>sz[lc]+1)
		return fnd(rc,rnk-sz[lc]-1);
	return x;
}
void split(int l,int r)
{
	int x=fnd(rt,l),y=fnd(rt,r);
	splay(x,rt);
	splay(y,ch[x][1]);
}
int build(int l,int r,int f)
{
	if(l>r)
		return 0;
	if(l==r)
	{
		fa[l]=f;
		sz[l]=1;
		ch[f][l>f]=l;
		return l;
	}
	int mid=(l+r)/2;
	build(l,mid-1,mid),build(mid+1,r,mid);
	fa[mid]=f;
	ch[f][mid>f]=mid;
	pushup(mid);
	return mid;
}
int main()
{
	int a,b,c;
	char s[11];
	while(scanf("%d%d",&n,&m)&&n>=0&&m>=0)
	{
		memset(ch,0,sizeof(ch));
		memset(fa,0,sizeof(fa));
		memset(tag,0,sizeof(tag));
		rt=build(1,n+2,0);
		while(m--)
		{
			scanf("%s%d%d",s,&a,&b);
			split(a,b+2);
			if(s[0]=='C')
			{
				scanf("%d",&c);
				c++;
				int x,y,z;
				x=ch[ch[rt][1]][0];
				ch[ch[rt][1]][0]=0,fa[x]=0;//剪下来 
				pushup(ch[rt][1]),pushup(rt);
				y=fnd(rt,c),z=fnd(rt,c+1);
				splay(y,rt),splay(z,ch[y][1]);
				ch[z][0]=x,fa[x]=z;
				pushup(z),pushup(rt);
			}
			else
			{
				int x=ch[ch[rt][1]][0];
				tag[x]^=1;
			}
		}
		for(int i=2;i<=n;i++)
			printf("%d ",fnd(rt,i)-1);
		printf("%d\n",fnd(rt,n+1)-1);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值