Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3979 Accepted Submission(s): 1631
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?
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.
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:翻转区间
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
const int maxn=3*100000+10;
#define REP(i,n) for(int i=0;i<(n);++i)
#define Key_value ch[ch[root][1]][0]
#define MES(s,c) memset(s,c,sizeof(s))
using namespace std;
int tot1,root,key[maxn],pre[maxn],size[maxn],ch[maxn][2];
int s[maxn],tot2;
int rev[maxn];
int a[maxn];
int n,q;
void Treaval(int rt)
{
if(rt){
Treaval(ch[rt][0]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2d \n",rt,ch[rt][0],ch[rt][1],pre[rt],size[rt],key[rt]);
Treaval(ch[rt][1]);
}
}
void debug(){printf("%d\n",root);Treaval(root);}
void NewNode(int &rt,int f,int k)
{
if(tot2) rt=s[tot2--];
else rt=++tot1;
// rt=++tot1;
ch[rt][1]=ch[rt][0]=0;
pre[rt]=f;
key[rt]=k;
size[rt]=1;
}
void update_rev(int rt)
{
if(!rt) return ;
swap(ch[rt][0],ch[rt][1]);
rev[rt]^=1;
}
void push_up(int rt)
{
int lson=ch[rt][0],rson=ch[rt][1];
size[rt]=size[lson]+size[rson]+1;
}
void push_down(int rt)
{
if(rev[rt]){
update_rev(ch[rt][0]);
update_rev(ch[rt][1]);
rev[rt]=0;
}
}
void build(int &rt,int l,int r,int f)
{
if(l>r) return ;
int m=(l+r)>>1;
NewNode(rt,f,m);
build(ch[rt][0],l,m-1,rt);
build(ch[rt][1],m+1,r,rt);
push_up(rt);
}
void Init()
{
root=ch[root][1]=ch[root][0]=pre[root]=key[root]=size[root]=rev[root]=0;
tot1=tot2=0;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
// REP(i,n) a[i]=i+1;
build(Key_value,1,n,ch[root][1]);
push_up(ch[root][1]);
push_up(root);
}
void Rotate(int x,int c)
{
int y=pre[x];
push_down(y);
push_down(x);
ch[y][!c]=ch[x][c];
pre[ch[x][c]]=y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][c]=y;
pre[y]=x;
push_up(y);
}
void Splay(int x,int goal)
{
push_down(x);
while(pre[x]!=goal){
if(pre[pre[x]]==goal){
Rotate(x,ch[pre[x]][0]==x);
}
else{
int y=pre[x];
int c=ch[pre[y]][0]==y;
if(ch[y][c]==x){
Rotate(x,!c);
Rotate(x,c);
}
else{
Rotate(y,c);
Rotate(x,c);
}
}
}
push_up(x);
if(goal==0) root=x;
}
int Get_kth(int rt,int k)
{
push_down(rt);
int t=size[ch[rt][0]]+1;
if(t==k) return rt;
if(t>k) return Get_kth(ch[rt][0],k);
else return Get_kth(ch[rt][1],k-t);
}
//void Insert(int pos,int tot)
//{
// REP(i,tot) scanf("%d",&a[i]);
// Splay(Get_kth(root,pos+1),0);
// Splay(Get_kth(root,pos+2),root);
// build(Key_value,0,tot-1,ch[root][1]);
// push_up(ch[root][1]);
// push_up(root);
//}
//void erase(int rt)
//{
// if(!rt) return ;
// s[++tot2]=rt;
// erase(ch[rt][0]);
// erase(ch[rt][1]);
//}
//void Delete(int pos,int tot)
//{
// Splay(Get_kth(root,pos),0);
// Splay(Get_kth(root,pos+tot+1),root);
// erase(Key_value);
// pre[Key_value]=0;
// Key_value=0;
// push_up(ch[root][1]);
// push_up(root);
//}
void Reverse(int a,int b)
{
Splay(Get_kth(root,a),0);
Splay(Get_kth(root,b+2),root);
update_rev(Key_value);
push_up(ch[root][1]);
push_up(root);
}
void Cut(int a,int b,int c)
{
Splay(Get_kth(root,a),0);
Splay(Get_kth(root,b+2),root);
int index=Key_value;
pre[Key_value]=0;
Key_value=0;
push_up(ch[root][1]);
push_up(root);
Splay(Get_kth(root,c+1),0);
Splay(Get_kth(root,c+2),root);
Key_value=index;
pre[Key_value]=ch[root][1];
push_up(ch[root][1]);
push_up(root);
}
int cnt;
void InOrder(int rt)
{
if(!rt) return ;
push_down(rt);
InOrder(ch[rt][0]);
if(cnt>=1&&cnt<=n){
if(cnt>1) printf(" ");
printf("%d",key[rt]);
}
cnt++;
InOrder(ch[rt][1]);
}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//#endif // ONLINE_JUDGE
while(scanf("%d %d",&n,&q)!=EOF){
if(n==-1||q==-1) break;
Init();
char op[20];
int l,r,c;
while(q--){
scanf("%s",op);
if(op[0]=='C'){
scanf("%d %d %d",&l,&r,&c);
Cut(l,r,c);
}
else{
scanf("%d %d",&l,&r);
Reverse(l,r);
}
}
cnt=0;
InOrder(root);
printf("\n");
}
return 0;
}