HDU-3487 Play with Chain (splay好题 带翻转 cut-link操作)

Play with Chain

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


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



#include <bits/stdc++.h>
using namespace std;
#define maxn 300005
int pre[maxn], ch[maxn][2], sz[maxn], rev[maxn], rt, tot;
vector<pair<int,int> >g;
inline void newNode(int& r, int father){
    r = ++tot;
    pre[r] = father;
    ch[r][0] = ch[r][1] = 0;
    sz[r] = 1;
    rev[r] = 0;
}
inline void update(int r){
    if(r){
        swap(ch[r][0], ch[r][1]);
        rev[r] ^= 1;
    }
}
inline void pushdown(int r){
    if(rev[r]){
        update(ch[r][0]);
        update(ch[r][1]);
        rev[r] = 0;
    }
}
inline void pushup(int r){
    sz[r] = 1 + sz[ch[r][0]] + sz[ch[r][1]];
}
inline void rotate(int r, int kind){
    int y = pre[r];
    pushdown(y);
    pushdown(r);
    ch[y][!kind] = ch[r][kind];
    pre[ch[r][kind]] = y;
    if(pre[y]){
        ch[pre[y]][ch[pre[y]][1] == y] = r;
    }
    pre[r] = pre[y];
    ch[r][kind] = y;
    pre[y] = r;
    sz[r] = sz[y];
    sz[y] = 1 + sz[ch[y][0]] + sz[ch[y][1]];
}
void splay(int r, int goal){
    while(pre[r] != goal){
        if(pre[pre[r]] == goal){
            rotate(r, ch[pre[r]][0] == r);
        }
        else{
            int y = pre[r];
            int kind = ch[pre[y]][0] == y;
            if(ch[y][kind] == r){
                rotate(r, !kind);
                rotate(r, kind);
            }
            else{
                rotate(y, kind);
                rotate(r, kind);
            }
        }
    }
    if(goal == 0) rt = r;
}
void insert(int x){
    int r = rt;
    while(1){
        sz[r]++;
        if(x < r){
            if(ch[r][0]) r = ch[r][0];
            else break;
        }
        else{
            if(ch[r][1]) r = ch[r][1];
            else break;
        }
    }
    newNode(ch[r][x > r], r);
    splay(ch[r][x > r], 0);
}
void findkth(int x, int goal){
    int r = rt;
    pushdown(r);
    while(sz[ch[r][0]] + 1 != x){
        if(x < sz[ch[r][0]] + 1){
            if(ch[r][0]) r = ch[r][0];
            else break;
        }
        else{
            x -= sz[ch[r][0]] + 1;
            if(ch[r][1]) r = ch[r][1];
            else break;
        }
        pushdown(r);
    }
    splay(r, goal);
}
void cut(int l, int r, int w){
    findkth(l - 1, 0);
    findkth(r + 1, rt);
    pushdown(rt);
    int y = ch[ch[rt][1]][0];   //把该子树切除,维护sz[]
    ch[ch[rt][1]][0] = 0;
    sz[rt] -= sz[y];
    sz[ch[rt][1]] -= sz[y];
    findkth(w, 0);
    int x = ch[rt][1];
    ch[rt][1] = y;
    pre[y] = rt;
    sz[rt] += sz[y];
    sz[y] += sz[x];
    pushdown(y);
    while(ch[y][1]){
        y = ch[y][1];
        sz[y] += sz[x];
        pushdown(y);
    }
    ch[y][1] = x;    //把该子树接上
    pre[x] = y;
    splay(x, 0);
}
void flip(int l, int r){
    findkth(l - 1, 0);
    findkth(r + 1, rt);
    update(ch[ch[rt][1]][0]);
}
void dfs(int r, int& d){
    pushdown(r);
    if(ch[r][0]){
        dfs(ch[r][0], d);
    }
    if(d != 1 && d != tot)
        printf("%d%s", r - 1, d == tot - 1 ? "\n" : " ");
    d++;
    if(ch[r][1]){
        dfs(ch[r][1], d);
    }
}
int main(){
    int n, m, a, b, c;
    while(scanf("%d %d", &n, &m) != EOF){
        if(n < 0 || m < 0) return 0;
        rt = tot = 0;
        newNode(rt, 0);
        for(int i = 1; i <= n; ++i){
            insert(i + 1);
        }
        insert(n + 2);
        char s[6];
        for(int i = 1; i <= m; ++i){
            scanf("%s", s);
            if(s[0] == 'C'){
                scanf("%d %d %d", &a, &b, &c);
                cut(a + 1, b + 1, c + 1);

            }
            else{
                scanf("%d %d", &a, &b);
                flip(a + 1, b + 1);
            }
        }
        int d = 1;
        dfs(rt, d);
    }
}

/*
题意:3e5个数字,3e5次操作,每次操作要么将一段区间翻转,要么将一段区间整体插入到某个数后,原本在该数后面
的数顺接到区间后面。

思路:
利用splay去做区间的翻转和移动插入操作是很方便的,维护一个sz[]就可以轻松的在log(n)的时间将一个区间插入。
具体的做法是:按数字下标建树,节点存下标对应的数字。对于区间[L,R]的翻转操作,我们只需要将L-1号结点splay到
根结点,R+1号结点splay到根节点的右儿子,这样以ch[ch[rt][1]][0]为根的子树就是区间[L,R]中的所有点,然后在此处
做翻转操作即可,打翻转标记。对于cut操作,还是同样的方法定位区间,然后把以ch[ch[rt][1]][0]为根的子树单独拿出来,
注意sz[]的变化,然后我们要将该子树接到第i个数后面,我们再对切除该子树的树找到第i小的结点,然后把子树接上去即可。
具体操作可以看代码或者自己想一下。注意维护sz[],这个很关键,很容易错。
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值