Play with Chain hdu 3487(伸展树翻转+删子树添子树)

Play with Chain

Time Limit:2000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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-n个数字,两种操作: 1.取出一个区间,将这个区间加入到第k个数字的后面。 2.翻转一个区间 分析: 伸展树的删树,翻转操作 对于一个子树,不必真的删除,而是保留,直接把整颗树放到第k个数字后面
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f
#define keytree (ch[ch[root][1]][0])
#define maxn 300010

int tmp[maxn],pre[maxn],siz[maxn],rev[maxn],val[maxn],ch[maxn][2];
int top,root;

inline void newnode(int &x,int p,int v)
{
    x=++top;
    siz[x]=ch[x][0]=ch[x][1]=rev[x]=0;
    pre[x]=p;
    val[x]=v;
}

inline void pushup(int x)
{
    siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}

inline void build(int &x,int l,int r,int p)
{
    int mid=(l+r)>>1;
    newnode(x,p,tmp[mid]);
    if(l<mid) build(ch[x][0],l,mid-1,x);
    if(r>mid) build(ch[x][1],mid+1,r,x);
    pushup(x);
}



inline void pushdown(int x)
{
    if(rev[x])
    {
        swap(ch[x][0],ch[x][1]);
        rev[ch[x][0]]^=1;
        rev[ch[x][1]]^=1;
        rev[x]=0;
    }
}

inline void init(int n)
{
    for(int i=0; i<n; i++) tmp[i]=i+1;
    ch[0][0]=ch[0][1]=siz[0]=pre[0]=rev[0]=0;
    root=top=0;
    newnode(root,0,-1);
    newnode(ch[root][1],root,-1);
    build(keytree,0,n-1,ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}

inline int get_kth(int x,int k)
{
    pushdown(x);
    int t=siz[ch[x][0]]+1;
    if(k==t) return x;
    if(k<t) return get_kth(ch[x][0],k);
    else return get_kth(ch[x][1],k-t);
}

inline void Rotate(int x,int kind)
{
    int y=pre[x];
    pushdown(y);
    pushdown(x);
    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;
    if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    ch[x][kind]=y;
    pre[y]=x;
    pushup(y);
}

inline void splay(int x,int goal)
{
    pushdown(x);
    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);
            }
        }
    }
    pushup(x);
    if(goal==0) root=x;
}

inline void Rev(int l,int r)
{
    splay(get_kth(root,l),0);
    splay(get_kth(root,r+2),root);
    rev[keytree]^=1;
}

inline void Cut(int l,int r,int k)
{
    splay(get_kth(root,l),0);
    splay(get_kth(root,r+2),root);
    int p=keytree;
    ch[pre[p]][0]=0;
    siz[root]-=siz[p];
    siz[ch[root][1]]-=siz[p];
    pre[p]=0;
    splay(get_kth(root,k+1),0);
    splay(get_kth(root,k+2),root);
    keytree=p;
    siz[root]+=siz[p];
    siz[ch[root][1]]+=siz[p];
    pre[p]=ch[root][1];
}

inline void print(int root)
{
    pushdown(root);
    if(root)
    {
        print(ch[root][0]);
        if(ok) printf("%d",val[root]),ok=0;
        else printf(" %d",val[root]);
        print(ch[root][1]);
    }
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==-1&&m==-1) break;
        init(n);
        while(m--)
        {
            char s[10];
            scanf("%s",s);
            if(s[0]=='C')
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                Cut(a,b,c);
            }
            else
            {
                int a,b;
                scanf("%d%d",&a,&b);
                Rev(a,b);
            }
        }
        for(int i=1; i<=n; i++)
        {
            int p=get_kth(root,i+1);
            if(i!=n) printf("%d ",val[p]);
            else printf("%d\n",val[p]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值