程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU3487(splay區間翻轉+區間切割)

HDU3487(splay區間翻轉+區間切割)

編輯:C++入門知識

HDU3487(splay區間翻轉+區間切割)




F - Play with Chain Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status

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 


題意:開始有一個1,2,3,。。。n的序列,進行m次操作,cut a b c將區間[a,b]取出得到新序列,將區間插入到新序列第c個元素之後。filp a b 將區間a,b翻轉,輸出最終的序列。

思路:對於cut操作我們需要先提取出區間[a,b]然後,先暫時分裂出去,然後以c為邊界分裂左右兩部分,然後合並左邊的和區間[a,b],將最大的旋轉至根,然後和右邊的合並。對於filp操作,我們可以使用lazy標記,先不翻轉,需要的時候再翻轉,代碼如下

/*************************************************************************
    > File Name: A.cpp
    > Author: acvcla
    > QQ:
    > Mail: [email protected] 
    > Created Time: 2014年11月17日 星期一 23時34分13秒
 ************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
typedef pairpii;
const int maxn = 3e5 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int siz[maxn],rev[maxn],pre[maxn],ch[maxn][2],key[maxn],ans[maxn];
int tot,root,n;
inline void newnode(int &x,int fa,int k){
	x=++tot;
	pre[x]=fa;
	siz[x]=1;
	key[x]=k;
	ch[x][0]=ch[x][1]=rev[x]=0;
}
inline void Modify(int x){
	if(!x)return;
	rev[x]^=1;
}
inline void push_down(int x){
	if(x&&rev[x]){
		swap(ch[x][0],ch[x][1]);
		Modify(ch[x][0]);
		Modify(ch[x][1]);
		Modify(x);
	}
}
inline void push_up(int x){
	if(!x)return ;
	siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}
void built(int &x,int L,int R,int fa){
	if(L>R)return;
	int M=(L+R)>>1;
	newnode(x,fa,M);
	built(ch[x][0],L,M-1,x);
	built(ch[x][1],M+1,R,x);
	push_up(x);
}
void Rotate(int x,int kind){
	int y=pre[x];
	push_down(y);
	push_down(x);

	ch[y][!kind]=ch[x][kind];
	pre[ch[x][kind]]=y;
	ch[x][kind]=y;

	if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
	pre[x]=pre[y];
	pre[y]=x;

	push_up(y);
	push_up(x);
}
void Splay(int x,int goal){
	while(pre[x]!=goal){
		if(pre[pre[x]]==goal){
			Rotate(x,ch[pre[x]][0]==x);
		}else{
			int y=pre[x];
			int kind=(ch[pre[y]][0]==y);
			if(ch[y][kind]==x){
				Rotate(x,!kind);
				Rotate(x,kind);
			}else{
				Rotate(y,kind);
				Rotate(x,kind);
			}
		}
	}
	if(goal==0)root=x;
}
int Get_kth(int x,int k){
	push_down(x);
	int tz=siz[ch[x][0]]+1;
	if(tz==k)return x;
	if(tz>k)return Get_kth(ch[x][0],k);
	return Get_kth(ch[x][1],k-tz);
}
void init(int n){
	root=tot=siz[0]=pre[0]=ch[0][0]=ch[0][1]=rev[0]=0;
	newnode(root,0,-1);
	newnode(ch[root][1],root,n+1);
	built(ch[ch[root][1]][0],1,n,ch[root][1]);
	push_up(ch[root][1]);
	push_up(root);
}
int Get_max(int x){
	push_down(x);
	while(ch[x][1]){
		x=ch[x][1];
		push_down(x);
	}
	return x;
}
void merge(int root1,int root2)/*root2接到root1右子樹,要求root1無右子樹*/
{
	ch[root1][1]=root2;
	pre[root2]=root1;
}
int __;
void travel(int x){
	if(!x)return;
	push_down(x);
	travel(ch[x][0]);
	ans[__++]=key[x];
	travel(ch[x][1]);
}
int main(){
		int n,m;
		while(~scanf("%d%d",&n,&m)){
			if(n<0&&m<0)return 0;
			init(n);
			char op[10];
			int L,R,C;
			while(m--){
				scanf("%s%d%d",op,&L,&R);
				if(op[0]=='F'){
					Splay(Get_kth(root,L),0);
					Splay(Get_kth(root,R+2),root);
					Modify(ch[ch[root][1]][0]);
				}else{
					scanf("%d",&C);
					Splay(Get_kth(root,L),0);
					Splay(Get_kth(root,R+2),root);
					int root1=ch[ch[root][1]][0];/*刪除區間[L,R]*/
					ch[ch[root][1]][0]=0;
					push_up(ch[root][1]);
					push_up(root);
					Splay(Get_kth(root,C+1),0);/*先分裂區間C兩邊,插入區間[L,R],然後合並*/
					int root2=ch[root][1];
					merge(root,root1);
					push_up(root);
					Splay(Get_max(root),0);
					merge(root,root2);
					push_up(root);
				}
			}
			__=0;
			travel(root);
			rep(i,1,n)printf("%d%c",ans[i],i==n?'\n':' ');
		}
		return 0;
}


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