Problem Description

YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1$1$ to n$n$.
At first, the diamonds on the chain is a sequence: 1,2,3,,n$1, 2, 3, …, n$.
He will perform two types of operations:
CUT a$a$ b$b$ c$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$n=8$, the chain is: 1 2 3 4 5 6 7 8$1\ 2\ 3\ 4\ 5\ 6\ 7\ 8$; We perform “CUT 3$3$ 5$5$ 4$4$”, Then we first cut down 3$3$ 4$4$ 5$5$, and the remaining chain would be: 1$1$ 2$2$ 6$6$ 7$7$ 8$8$. Then we insert “3$3$ 4$4$ 5$5$” into the chain before 5$5$th diamond, the chain turns out to be: 1$1$ 2$2$ 6$6$ 7$7$ 3$3$ 4$4$ 5$5$ 8$8$.
FLIP a$a$ b$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$2$ 6$6$” on the chain: 1$1$ 2$2$ 6$6$ 7$7$ 3$3$ 4$4$ 5$5$ 8$8$. The chain will turn out to be: 1$1$ 4$4$ 3$3$ 7$7$ 6$6$ 2$2$ 5$5$ 8$8$.
He wants to know what the chain looks like after perform m$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$n$ and m$m$ (1n,m3×100000$1≤n, m≤3\times 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$a$ b$b$ c$c$ // Means a CUT operation, 1abn,0cn(ba+1)$1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1)$.
FLIP a$a$ b$b$ // Means a FLIP operation, 1a<bn$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$n$ numbers. The i$i$th number is the number of the i$i$th diamond on the chain.

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

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直接操作。

代码

#include <cstdio>
#include <cstring>

const int maxn=300000;

int n;

struct splay_tree
{
int fa[maxn+10],son[2][maxn+10],size[maxn+10],rev[maxn+10],root;

inline int t(int x)
{
return son[1][fa[x]]==x;
}

inline int push_rev(int x)
{
rev[x]^=1;
int t=son[0][x];
son[0][x]=son[1][x];
son[1][x]=t;
return 0;
}

inline int pushdown(int x)
{
if(rev[x])
{
if(son[0][x])
{
push_rev(son[0][x]);
}
if(son[1][x])
{
push_rev(son[1][x]);
}
rev[x]=0;
}
return 0;
}

inline int updata(int x)
{
return size[x]=size[son[0][x]]+size[son[1][x]]+1;
}

inline int rotate(int x)
{
int k=t(x),f=fa[x];
if(fa[f])
{
son[t(f)][fa[f]]=x;
}
fa[x]=fa[f];
if(son[!k][x])
{
fa[son[!k][x]]=f;
}
son[k][f]=son[!k][x];
fa[f]=x;
son[!k][x]=f;
updata(f);
updata(x);
return 0;
}

inline int splay(int x,int c)
{
while(fa[x]!=c)
{
int f=fa[x];
if(fa[f]==c)
{
rotate(x);
}
else if(t(x)==t(f))
{
rotate(f);
rotate(x);
}
else
{
rotate(x);
rotate(x);
}
}
if(!c)
{
root=x;
}
return 0;
}

inline int getkth(int x)
{
int now=root;
while(now)
{
pushdown(now);
if(size[son[0][now]]+1==x)
{
return now;
}
else if(size[son[0][now]]+1<x)
{
x-=size[son[0][now]]+1;
now=son[1][now];
}
else
{
now=son[0][now];
}
}
return 0;
}

int build(int l,int r)
{
int mid=(l+r)>>1;
rev[mid]=0;
size[mid]=r-l+1;
if(l<=mid-1)
{
son[0][mid]=build(l,mid-1);
fa[son[0][mid]]=mid;
}
if(mid+1<=r)
{
son[1][mid]=build(mid+1,r);
fa[son[1][mid]]=mid;
}
return mid;
}

inline int cut(int l,int r,int x)
{
int now;
if(l==1)
{
if(r==n)
{
now=root;
root=0;
}
else
{
int w=getkth(r+1);
splay(w,0);
now=son[0][w];
son[0][w]=0;
fa[now]=0;
updata(w);
}
}
else
{
int w=getkth(l-1);
splay(w,0);
if(r==n)
{
now=son[1][w];
son[1][w]=0;
fa[now]=0;
}
else
{
int y=getkth(r+1);
splay(y,w);
now=son[0][y];
son[0][y]=0;
fa[now]=0;
updata(y);
}
updata(w);
}
if(!x)
{
if(!root)
{
root=now;
}
else
{
int w=getkth(1);
splay(w,0);
son[0][w]=now;
fa[now]=w;
updata(w);
}
}
else
{
int w=getkth(x);
splay(w,0);
if(x==n-(r-l+1))
{
son[1][w]=now;
fa[now]=w;
}
else
{
int y=getkth(x+1);
splay(y,w);
son[0][y]=now;
fa[now]=y;
updata(y);
}
updata(w);
}
return 0;
}

inline int reverse(int l,int r)
{
if(l==1)
{
if(r==n)
{
push_rev(root);
}
else
{
int x=getkth(r+1);
splay(x,0);
push_rev(son[0][x]);
}
}
else
{
int x=getkth(l-1);
splay(x,0);
if(r==n)
{
push_rev(son[1][x]);
}
else
{
int y=getkth(r+1);
splay(y,x);
push_rev(son[0][y]);
}
}
return 0;
}

inline int mem()
{
memset(fa,0,sizeof fa);
memset(son,0,sizeof son);
memset(size,0,sizeof size);
memset(rev,0,sizeof rev);
return 0;
}
};

splay_tree st;
int m,a,b,c;
char s[10];

int main()
{
while(1)
{
st.mem();
scanf("%d%d",&n,&m);
if((n<0)&&(m<0))
{
break;
}
st.build(1,n);
st.root=(n+1)>>1;
while(m--)
{
scanf("%s%d%d",s,&a,&b);
if(s[0]=='C')
{
scanf("%d",&c);
st.cut(a,b,c);
}
else
{
st.reverse(a,b);
}
}
for(register int i=1; i<n; ++i)
{
printf("%d ",st.getkth(i));
}
printf("%d\n",st.getkth(n));
}
return 0;
}