Play with Chain+hdu+splay树的基本操作

Play with Chain

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4043    Accepted Submission(s): 1649


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
解决方案:
此题也相当于splay树的模板题,主要涉及分解,合并,翻滚的操作
code:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{

    node *ch[2];///0做孩子,1右孩子
    int s;///子孙及其自己的总结点的个数
    int flip;///翻滚的标识
    int v;///节点的值
    int cmp(int k)const
    {
        int d=k-ch[0]->s;
        if(d==1) return -1;
        return d<=0?0:1;
    }///比较函数,用于翻滚时找出翻滚节点
    void maintain()
    {
        s=ch[0]->s+ch[1]->s+1;
    }///更新节点个数
    void pushdown()
    {
        if(flip)
        {
            flip=0;
            swap(ch[0],ch[1]);
            ch[0]->flip=!ch[0]->flip;
            ch[1]->flip=!ch[1]->flip;
        }

    }///根据翻滚标志进行翻滚
};
node *null=new node();
void rotate(node *&o,int d)
{
    node *k=o->ch[d^1];
    o->ch[d^1]=k->ch[d];
    k->ch[d]=o;
    o->maintain();
    k->maintain();
    o=k;
}///旋转操作
void splay(node *&o,int k)
{
    o->pushdown();
    int d=o->cmp(k);
    if(d==1) k-=(o->ch[0]->s+1);
    if(d!=-1)
    {
        node *p=o->ch[d];
        p->pushdown();
        int d2=p->cmp(k);
        int k2=(d2==0?k:k-p->ch[0]->s-1);
        if(d2!=-1)
        {
            splay(p->ch[d2],k2);
            if(d==d2) rotate(o,d^1);
            else rotate(o->ch[d],d);
        }
        rotate(o,d^1);
    }

}///伸展
node * merge(node *left,node *right)
{

    splay(left,left->s);
    left->ch[1]=right;
    left->maintain();
    return left;
}///合并
void split(node *o,int k,node *&left,node *&right)
{
    splay(o,k);
    left=o;
    right=o->ch[1];
    o->ch[1]=null;
    left->maintain();
}
const int maxn=400000;
struct splaysequence
{

    int n;
    node seq[maxn];
    node *root;
    node *build(int sz)
    {
        if(!sz) return null;
        node *L=build(sz/2);
        node *o=&seq[++n];
        o->v=n;
        o->ch[0]=L;
        o->ch[1]=build(sz-sz/2-1);
        o->flip=o->s=0;
        o->maintain();
        return o;
    }
    void init(int sz)
    {
        n=0;
        null->s=0;
        root=build(sz);
    }

};
vector<int >ans;
void print(node *o)
{
    if(o!=null)
    {
        o->pushdown();
        print(o->ch[0]);
        ans.push_back(o->v);
        print(o->ch[1]);
    }
}
splaysequence ss;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)&&(m+n)!=-2)
    {
        ss.init(n+1);
        char op[20];
        while(m--)
        {
            scanf("%s",op);
            if(strcmp(op,"CUT")==0)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
              //  cout<<a<<b<<c<<endl;
                node *left,*right,*o,*mid,*temp;
                split(ss.root,a,o,right);
                split(right,b-a+1,mid,temp);
                o=merge(o,temp);
                split(o,c+1,left,right);
                ss.root=merge(merge(left,mid),right);
            }
            else
            {
                int a,b;
                scanf("%d%d",&a,&b);
               // cout<<a<<b<<endl;
                node *left,*right,*mid,*temp;
               split(ss.root,a,left,right);
                split(right,b-a+1,mid,temp);
                mid->flip^=1;
                ss.root=merge(merge(left,mid),temp);
            }

        }
        ans.clear();
        print(ss.root);
        int len=ans.size();
        for(int i=1; i<len; i++)
        {
            printf("%d%c",ans[i]-1,i!=len-1?' ':'\n');
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值