程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> hdu3487 Play with Chain

hdu3487 Play with Chain

編輯:關於C++

 

Play with Chain

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4980 Accepted Submission(s): 2040



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

Source 2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
Recommend zhengfeng | We have carefully selected several similar problems for you: 3486 3479 3480 3481 3482

 

 

 

 

很顯然是Splay維護區間。然而這道題沒有自動過濾行末空格,感覺真是坑......(論OIer做ACM題的心態調整2333)


 

 

 

#include
#include
#include
#include
#include
#include
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define pa pair
#define MAXN 300005
#define key c[c[rt][1]][0]
using namespace std;
int flag,n,m,rt,x,y,z,c[MAXN][2],s[MAXN],fa[MAXN];
bool rev[MAXN];
char ch[10];
inline int read()
{
	int ret=0,flag=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
	return ret*flag;
}
inline void build(int x,int y,int f)
{
	if (x>y) return;
	int mid=(x+y)>>1;
	s[mid]=1;fa[mid]=f;s[mid]=y-x+1;
	c[f][mid>f]=mid;
	if (x==y) return;
	build(x,mid-1,mid);build(mid+1,y,mid);
}
inline void pushup(int k)
{
	s[k]=s[c[k][0]]+s[c[k][1]]+1;
}
inline void update(int k)
{
	if (!k) return;
	rev[k]^=1;
	swap(c[k][0],c[k][1]);
}
inline void pushdown(int k)
{
	if (!k||!rev[k]) return;
	update(c[k][0]);update(c[k][1]);
	rev[k]=0;
}
inline int find(int k,int x)
{
	pushdown(k);
	if (s[c[k][0]]+1==x) return k;
	else if (s[c[k][0]]>=x) return find(c[k][0],x);
	else return find(c[k][1],x-s[c[k][0]]-1);
}
inline void rotate(int x,int &k)
{
	int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1;
	if (y==k) k=x;
	else if (c[z][0]==y) c[z][0]=x;else c[z][1]=x;
	fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
	c[y][l]=c[x][r];c[x][r]=y;
	pushup(y);
}
inline void relax(int x,int k)
{
	if (x!=k) relax(fa[x],k);
	pushdown(x);
}
inline void splay(int x,int &k)
{
	relax(x,k);
	while (x!=k)
	{
		int y=fa[x],z=fa[y];
		if (y!=k)
		{
			if ((c[y][0]==x)^(c[z][0]==y)) rotate(x,k);
			else rotate(y,k);
		}
		rotate(x,k);
	}
	pushup(x);
}
inline void split(int l,int r)
{
	int x=find(rt,l-1),y=find(rt,r+1);
	splay(x,rt);splay(y,c[rt][1]);
}
inline void solvecut(int l,int r,int x)
{
	split(l,r);
	int tmp=key;key=0;pushup(c[rt][1]);pushup(rt);
	split(x+1,x);
	key=tmp;fa[tmp]=c[rt][1];pushup(c[rt][1]);pushup(rt);
}
inline void solverev(int l,int r)
{
	split(l,r);update(key);
	pushup(c[rt][1]);pushup(rt);
}
inline void getans(int k)
{
	if (!k) return;
	pushdown(k);
	getans(c[k][0]);
	if (k!=1&&k!=n+2)
	{
		if (flag++) printf(" %d",k-1);
		else printf("%d",k-1);
	}
	getans(c[k][1]);
}
int main()
{
	n=read();m=read();
	while (n>=0&&m>=0)
	{
		rt=0;
		memset(c,0,sizeof(c));
		memset(s,0,sizeof(s));
		memset(fa,0,sizeof(fa));
		memset(rev,0,sizeof(rev));
		build(1,n+2,0);
		rt=(n+2)>>1;
		F(i,1,m)
		{
			scanf("%s",ch);x=read()+1;y=read()+1;
			if (ch[0]=='C'){z=read()+1;solvecut(x,y,z);}
			else solverev(x,y);
		}
		flag=0;getans(rt);printf("\n");
		n=read();m=read();
	}
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved