HDU 3487 Play with Chain(Splay)

제목:
조작 1: 구간 [a, b] 을 잘라 c 위치 뒤에 놓는다.
조작 2: 구간 [a, b] 을 뒤 집 습 니 다.
마지막 수열 을 출력 합 니 다.
생각:
분명히 Splay.
뒤 집기 란 뒤 집기 표 시 를 추가 하면 된다.정상 적 인 조작.
간단하게 절단 구간 을 말씀 드 리 겠 습 니 다.
먼저 a - 1 을 뿌리 로 옮 기 고 b + 1 을 뿌리 아래로 옮 기 며 오른쪽 아들 의 왼쪽 아들 을 잘라 낸다 (자 나 무 는 구간 [a, b] 을 보증한다)
Pushup 해 주세요.
c 를 뿌리 로 돌리 고 c + 1 을 뿌리 아래로 돌리 면 오른쪽 아들 왼쪽 아들 이 비어 있 을 거 예요. 꽂 으 면 돼 요.
#include 
#include 
#include 
using namespace std;


const int maxn = 300000 + 10;
int n, m;
int ks = 0;
int num[maxn];
const int inf = 0x3f3f3f3f;
struct SplayTree{
    void Rotate(int x, int f){
        int y = pre[x], z = pre[y];
        pushdown(x);
        pushdown(y);
        ch[y][!f] = ch[x][f]; pre[ch[x][f] ] = y;
        ch[x][f] = y; pre[y] = x;
        pre[x] = z;
        if (pre[x]) ch[z][ch[z][1] == y ] = x;
        pushup(y);
    }
    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], z = pre[y];
                int f = (ch[z][0] == y);
                if (ch[y][f] == x) Rotate(x, !f), Rotate(x, f);
                else Rotate(y, f), Rotate(x, f);
            }

        }
        pushup(x);
        if (goal == 0) root = x;
    }


    void RotateTo(int k,int goal){
        int x = root;
        pushdown(x);
        while(sz[ ch[x][0] ] != k){
            if (k < sz[ ch[x][0] ]){
                x = ch[x][0];
            }
            else {
                k -= (sz[ ch[x][0] ] + 1);
                x = ch[x][1];
            }
            pushdown(x);

        }
        Splay(x, goal);
    }
    void clear(){
        ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0;
        root = n = 0;
        val[0] = -inf;
        sum[0] = 0;
        NewNode(root, -inf);
        NewNode(ch[root][1], -inf);
        pre[n] = root;
        sz[root] = 2;
    }
    void NewNode(int& x,int c){
        x = ++n;
        ch[x][0] = ch[x][1] = pre[x] = 0;
        sz[x] = 1;
        val[x] = sum[x] = c;
        lazy[x] = 0;
    }
    void pushup(int x){
        sz[x] = 1 + sz[ch[x][0] ] + sz[ch[x][1] ];
        sum[x] = val[x] + (sum[ch[x][0] ] + sum[ch[x][1] ]);
    }
    void pushdown(int x){
        if (lazy[x] != 0){
            update_rev(ch[x][0]);
            update_rev(ch[x][1]);
            lazy[x] = 0;
        }
    }
    void init(int pos, int tot){
        clear();
        cnt = tot;
        RotateTo(pos, 0);
        RotateTo(pos + 1, root);
        build(ch[ ch[root][1] ][0], 1, tot, ch[root][1]);
        pushup(ch[root][1]);
        pushup(root);
    }
    void build(int& x,int l,int r,int f){
        if (l > r) return ;
        int mid = (l + r) >> 1;
        NewNode(x, num[mid]);
        build(ch[x][0],l, mid-1, x);
        build(ch[x][1], mid+1, r, x);
        pre[x] = f;
        pushup(x);
    }


    void update_same(int x,int v){
        if (!x) return;
        val[x] += v;
        sum[x] += 1LL * v * sz[x];
        lazy[x] += v;
    }
    void change(int l,int r,int c){
        RotateTo(l - 1, 0);
        RotateTo(r + 1, root);
        int key = ch[ch[root][1] ][0];
        update_same(key, c);
        pushup(ch[root][1]);
        pushup(root);
    }
    long long getsum(int l,int r){
        RotateTo(l-1,0);
        RotateTo(r+1,root);
        int key = ch[ch[root][1] ][0];
        return sum[key];
    }

    void cut(int a,int b,int c){
        RotateTo(a-1,0);
        RotateTo(b+1,root);
        int key = ch[ ch[root][1] ][ 0 ];
        ch[ ch[root][1] ][0] = 0;
        pre[key] = 0;
        pushup(ch[root][1]);
        pushup(root);

        RotateTo(c, 0);
        RotateTo(c + 1, root);

        ch[ ch[root][1] ][0] = key;
        pre[key] = ch[root][1];
        pushup(ch[root][1]);
        pushup(root);
    }


    void flip(int l,int r){
        RotateTo(l-1,0);
        RotateTo(r+1,root);
        int key = ch[ ch[root][1] ][0];
        update_rev(key);
    }

    void update_rev(int x){
        if (!x) return ;
        swap(ch[x][0], ch[x][1]);
        lazy[x] ^= 1;
    }

    void dfs(int cur){
        if (cur == 0) return;
        pushdown(cur);
        dfs(ch[cur][0]);
        if (val[cur] > -inf){
            if (ks ++) putchar(' ');
            printf("%d", val[cur]);
        }
        dfs(ch[cur][1]);
    }
    void solve(){
        dfs(root);
        puts("");
    }
    int root, n, cnt, ct, top;
    int ch[maxn][2];
    int pre[maxn];
    int sz[maxn];
    int val[maxn];
    int lazy[maxn];
    long long sum[maxn];
    int pool[maxn];
}spt;

int main(){
    int n, m;
    while(~scanf("%d %d",&n, &m)){
            ks = 0;
        if (n == -1 && m == -1) break;
        for (int i = 1; i <= n; ++i){
            num[i] = i;
        }
        char op[10];
        spt.init(0, n);
        while(m--){
            scanf("%s", op);
            if (op[0] == 'C'){
                int x, y, z;
                scanf("%d %d %d",&x, &y, &z);
                spt.cut(x, y, z);
            }
            else {
                int x, y;
                scanf("%d %d",&x, &y);
                spt.flip(x, y);
            }
        }
        spt.solve();
    }
    return 0;
}


 
Play with Chain
Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7083    Accepted Submission(s): 2806
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
2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
 
Recommend
zhengfeng

좋은 웹페이지 즐겨찾기