1. 程式人生 > >hdu 3487 Play with Chain splay

hdu 3487 Play with Chain splay

Play with Chain

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


 

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. CUT a b c 把區間[a,b]這一段元素切下來接到第c個元素的後面 
2. FLIP a b 反轉區間[a,b]

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 3e5 + 10;
const int INF = 1e9;
int root, tot;
int a[maxn], fa[maxn], tr[maxn][2], sz[maxn];
int tag[maxn], num[maxn];
vector<int> ans;

int lson(int x) {
    return tr[fa[x]][1] == x;
}

void update(int x) {
    if (x) sz[x] = sz[tr[x][0]] + sz[tr[x][1]] + 1;
}

void pushdown(int x) {
    if (tag[x]) {
        tag[tr[x][0]] ^= 1;
        tag[tr[x][1]] ^= 1;
        swap(tr[x][0], tr[x][1]);
        tag[x] = 0;
    }
}

void rotate(int x) {
    int y = fa[x], d = lson(x);
    if (tr[y][d] = tr[x][d ^ 1]) fa[tr[y][d]] = y;
    if (fa[x] = fa[y]) tr[fa[y]][lson(y)] = x;
    tr[fa[y] = x][d ^ 1] = y;
    update(y);
}

void splay(int x, int k) {
    for (int y; (y = fa[x]) != k; rotate(x))
        if (fa[y] != k) rotate(lson(x) == lson(y) ? y : x);
    update(x);
    if (!k) root = x;
}

int kth(int x, int k) {
    pushdown(x);
    if (sz[tr[x][0]] + 1 == k) return x;
    if (k <= sz[tr[x][0]]) return kth(tr[x][0], k);
    return kth(tr[x][1], k - 1 - sz[tr[x][0]]);
}

void reverse(int l, int r) {
    int x = kth(root, l - 1);
    int y = kth(root, r + 1);
    splay(x, 0); splay(y, x);
    tag[tr[y][0]] ^= 1;
}

void cut(int l, int r, int c) {
    int x = kth(root, l - 1);
    int y = kth(root, r + 1);
    splay(x, 0);
    splay(y, x);
    int rt = tr[y][0];
    tr[y][0] = 0;
    update(y); update(x);

    x = kth(root, c);
    y = kth(root, c + 1);
    splay(x, 0); splay(y, x);
    fa[tr[y][0] = rt] = y;
    update(y);update(x);
}

void build(int &x, int l, int r) {
    if (l > r) {
        x = 0;
        return;
    }
    int mid = (l + r) / 2;
    x = ++tot;
    num[x] = a[mid];
    sz[x] = 1;
    tag[x] = 0;
    build(tr[x][0], l, mid - 1);
    build(tr[x][1], mid + 1, r);
    fa[tr[x][0]] = fa[tr[x][1]] = x;
    update(x);
}

void out(int x) {
    pushdown(x);
    if (tr[x][0]) out(tr[x][0]);
    if (num[x] != INF) ans.push_back(num[x]);
    if (tr[x][1]) out(tr[x][1]);
}

int main() {
    int n, m, l, r, c;
    char op[10];
    while (scanf("%d%d", &n, &m) != EOF) {
        if (n < 0 && m < 0) break;
        a[1] = a[n + 2] = INF;
        fa[1] = 0;
        ans.clear();
        for (int i = 2; i <= n + 1; i++) a[i] = i - 1;
        root = tot = 0;
        build(root, 1, n + 2);
        while (m--) {
            scanf("%s", op);
            if (op[0] == 'C') {
                scanf("%d%d%d", &l, &r, &c);
                l++; r++; c++;
                cut(l, r, c);
            }
            if (op[0] == 'F') {
                scanf("%d%d", &l, &r);
                l++; r++;
                reverse(l, r);
            }
        }
        out(root);
        for (int i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]);
        printf("%d\n", ans[ans.size() - 1]);
    }
    return 0;
}