HDU 3487. Play with Chain

Play with Chain

Time Limit: 6000/2000 MS (Java/Others)

Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7099 Accepted Submission(s): 2815

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

Solution

  • 同样是 Splay 的模板题,支持区间翻转和区间切割操作。

  • 区间切割的话,一个区间 [l,r] ,就把 l1 旋到根,再把 r+1 旋到 l1 的右儿子。

  • 这样就能从 r+1 的左儿子处剪下区间 [l,r]

  • 之后把插入位置 c 旋到根,再把 c+1 旋到 c 的右儿子。

  • 最后把 [l,r] 接到 c+1 的左儿子即可。

Code

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=3e5+3,inf=1e9;
int root,tot,ans;
int a[N];
int fa[N],key[N],s[N][2],size[N];
bool rev[N];
inline int read()
{
    int X=0,w=1; char ch=0;
    while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
    return X*w;
}
inline void write(int x)
{
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline bool pd(int x)
{
    return x==s[fa[x]][1];
}
inline void update(int x)
{
    if(x) size[x]=size[s[x][0]]+size[s[x][1]]+1;
}
inline void down(int x)
{
    if(!rev[x]) return;
    rev[s[x][0]]^=1,rev[s[x][1]]^=1;
    swap(s[x][0],s[x][1]);
    rev[x]=0;
}
inline void build(int &v,int l,int r)
{
    if(l>r){v=0;return;}
    int mid=(l+r)>>1;
    key[v=++tot]=a[mid],size[v]=1,rev[v]=0;
    build(s[v][0],l,mid-1);
    build(s[v][1],mid+1,r);
    fa[s[v][0]]=fa[s[v][1]]=v;
    update(v);
}
inline void rotate(int x)
{
    down(x);
    int y=fa[x],w=pd(x);
    if(s[y][w]=s[x][w^1]) fa[s[y][w]]=y;
    if(fa[x]=fa[y]) s[fa[y]][pd(y)]=x;
    s[fa[y]=x][w^1]=y;
    update(y);
}
inline void splay(int x,int k)
{
    for(int y;(y=fa[x])!=k;rotate(x))
        if(fa[y]!=k) rotate(pd(x)==pd(y)?y:x);
    update(x);
    if(!k) root=x;
}
inline int kth(int x,int k)
{
    down(x);
    if(size[s[x][0]]+1==k) return x;
    if(k<=size[s[x][0]]) return kth(s[x][0],k);
    return kth(s[x][1],k-size[s[x][0]]-1);
}
inline void flip(int l,int r)
{
    int x=kth(root,l-1),y=kth(root,r+1);
    splay(x,0),splay(y,x);
    rev[s[y][0]]^=1;
}
inline void cut(int l,int r,int c)
{
    int x=kth(root,l-1),y=kth(root,r+1);
    splay(x,0),splay(y,x);
    int rt=s[y][0];s[y][0]=0;
    update(y),update(x);//delete

    x=kth(root,c),y=kth(root,c+1);
    splay(x,0),splay(y,x);
    fa[s[y][0]=rt]=y;
    update(y),update(x);//insert
}
inline void print(int v,int k)
{
    down(v);
    if(s[v][0]) print(s[v][0],k);
    if(key[v]<inf) write(key[v]),putchar(++ans==k?'\n':' ');
    if(s[v][1]) print(s[v][1],k);
}
int main()
{
    while(true)
    {
        int n=read(),m=read();
        if(n<0 && m<0) return 0;
        a[1]=a[n+2]=inf;
        ans=fa[1]=0;
        for(int i=2;i<=n+1;i++) a[i]=i-1;
        build(root=tot=0,1,n+2);
        while(m--)
        {
            char ch=getchar();
            while(ch!='F' && ch!='C') ch=getchar();
            int l=read()+1,r=read()+1;
            if(ch=='F') flip(l,r); else cut(l,r,read()+1);
        }
        print(root,n);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值