hdu 3487 Play with Chain splay tree

Play with Chain

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


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

题意:给一个从1到n的连续区间,做两个操作

1.把区间l---r的数字切割下来,接在剩下的序列的第n个数字后面

2把一个区间的序列反转


解题:splay 写好旋转,伸展操作,写个find函数查找区间第i个数字的位置即可,

然后用size记录节点个数,lz标记下这段区间是否需要反转,val记录节点的值

添加两个新节点在1的左边和n的右边,可以避免特殊判断

操作1:对于l,r,n , 查找l-1的位置提到根,查找r+1的位置提到根的右节点,然后 l---r区间的数都是r+1的左孩子了

把r+1的做孩子切割下来,然后把n提到根,n+1提到根的右节点,然后把l-r作为n+1的做孩子拼接上去即可


操作2:对于区间l,r 把l-1提到根,r+1提到根的右孩子,对于r+1的左孩子记录一个懒操作,表示要反转即可


只要在查找节点的时候记得push更新懒操作,在每次改变树的结构的时候,update一下就好了


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 5000001
int ch[maxn][2],fa[maxn],val[maxn],lz[maxn],root,cnt,size[maxn];
void init(){
    cnt=1;
    root=ch[0][0]=ch[0][1]=0;
    val[0]=lz[0]=fa[0]=0;
    size[0]=0;
}
void update(int n){
    size[n] = 1+size[ch[n][0]]+size[ch[n][1]];
}
int build(int l,int r,int f){
    if(r<l)return 0;
    fa[cnt]=f;
    int mid=(l+r)/2;
    val[cnt]=mid;
    int id=cnt++;
    size[id]=1;
    lz[id]=0;
    ch[id][0]=build(l,mid-1,id);
    ch[id][1]=build(mid+1,r,id);
    update(id);
    return id;
}
void push(int n){
    if(lz[n]){
        lz[n] = 0;
        swap(ch[n][0],ch[n][1]);
        lz[ch[n][0]]^=1;
        lz[ch[n][1]]^=1;
    }
}
void rotate(int n){
    int f=fa[n],ff=fa[f];
    int x=(ch[f][1]==n),x1=(ch[ff][1]==f);
    ch[ff][x1]=n;
    ch[f][x]=ch[n][!x];
    ch[n][!x]=f;
    fa[ch[f][x]]=f;
    fa[f]=n;
    fa[n]=ff;
    update(f),update(n);
}
int find(int n){
    int p = ch[root][1];
    push(p);
    int l = size[p]-size[ch[p][1]];
    while(l!=n){
        if(l<n){
            p=ch[p][1];
            push(p);
            l=l+size[p]-size[ch[p][1]];
        }
        else {
            p=ch[p][0];
            push(p);
            l=l-1-size[ch[p][1]];
        }
    }
    return p;
}
void splay(int n,int p){
    while(fa[n]!=p){
        int f=fa[n],ff=fa[f];
        if(ff==p)rotate(n);
        else if((ch[f][1]==n)==(ch[ff][1]==f)) rotate(n),rotate(n);
        else rotate(f),rotate(n);
    }
}
int flag;
void output(int n,int x){
    if(n == 0) return ;
    push(n);
    output(ch[n][0],x);
    if(val[n] != 1 && val[n]!=x) {
        if(flag) printf(" ");
        printf("%d",val[n]-1);flag=1;
    }
    output(ch[n][1],x);
}
int main(){
    int n,ope,l,r,u,v,p1,p2,q1,q2,q3;
    char word[5];
    while(scanf("%d%d",&n,&ope),n>0&&ope>0){
        init();
        ch[root][1] = build(1,n+2,root);
        for(int i = 0;i < ope;i++){
            scanf("%s",word);
            if(word[0] == 'C'){
                scanf("%d%d%d",&l,&r,&u);
                l++,r++,u++;
                p1=find(l-1);
                splay(p1,root);
                p2=find(r+1);
                splay(p2,p1);
                q1=ch[p2][0];
                ch[p2][0]=0;
                update(p2),update(p1);
                q2=find(u);
                splay(q2,root);
                q3=find(u+1);
                splay(q3,q2);
                ch[q3][0]=q1,fa[q1]=q3;
                update(q3),update(q2);
            }
            else {
                scanf("%d%d",&l,&r);
                l++,r++;
                p1=find(l-1);
                splay(p1,root);
                p2=find(r+1);
                splay(p2,p1);
                lz[ch[p2][0]]^=1;
            }
        }
        flag = 0;
        output(ch[root][1],n+2);
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值