Hdu3487 play with chain (splay)

Source: Internet
Author: User
Play with chain

Time Limit: 6000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2720 accepted submission (s): 1103


Problem descriptionyaoyao 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 wocould 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. cocould you help him?


Inputthere 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 shoshould not be processed as a case.


Outputfor each test case, You shoshould print a line with N numbers. The ith number is the number of the ith diamond on the chain.


Sample Input

8 2CUT 3 5 4FLIP 2 6-1 -1
 


Sample output

1 4 3 7 6 2 5 8
 


Source2010
ACM-ICPC multi-university training Contest (5) -- host by bjtu

For N numbers 1-N, cut A, B, and C represent the number of [a, B] ranges in the original sequence, after the number C in the remaining sequence. Flip a, B indicates that the range [a, B] is flipped.

Returns the sequence after M operations.

Question Analysis: When the interval is flipped, the key is the cut operation of the interval. At this time, the strong operation advantage of the stretch tree is displayed. You can also add the lazy flag to the reverse operation.

This is the second splay. It was called for one afternoon...

Code is relatively long:

#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int N = 300005;struct node{    int l,r,f,size,lazy,key;}tree[N];int next[N<<1];int m,n;int cnt;void init(){    tree[0].f = tree[0].size = 0;    for(int i = 0;i < N - 1;i ++)        next[i] = i + 1;}int newnode(int key){    int p = next[0];    next[0] = next[p];    tree[p].l = tree[p].f = tree[p].r = tree[p].lazy = 0;    tree[p].size = 1;    tree[p].key = key;    return p;}void push_up(int rt){    if(rt)        tree[rt].size = tree[tree[rt].l].size + tree[tree[rt].r].size + 1;}void push_down(int rt){    if(tree[rt].lazy)    {        swap(tree[rt].l,tree[rt].r);        tree[tree[rt].l].lazy ^= 1;        tree[tree[rt].r].lazy ^= 1;        tree[rt].lazy = 0;    }}void zig(int x){    push_down(x);    int p = tree[x].f;    bool lf;    if(tree[p].f)    {        if(tree[tree[p].f].l == p)            lf = true;        else            lf = false;    }    tree[p].l = tree[x].r;    if(tree[x].r)        tree[tree[x].r].f = p;    push_up(p);    tree[x].r = p;    tree[x].f = tree[p].f;    push_up(x);    tree[p].f = x;    if(tree[x].f == 0)        return;    if(lf)        tree[tree[x].f].l = x;    else        tree[tree[x].f].r = x;}void zag(int x){    push_down(x);    int p = tree[x].f;    bool lf;    if(tree[p].f)    {        if(tree[tree[p].f].l == p)            lf = true;        else            lf = false;    }    tree[p].r = tree[x].l;    if(tree[x].l)        tree[tree[x].l].f = p;    push_up(p);    tree[x].l = p;    tree[x].f = tree[p].f;    push_up(x);    tree[p].f = x;    if(tree[x].f == 0)        return;    if(lf)        tree[tree[x].f].l = x;    else        tree[tree[x].f].r = x;}int splay(int x,int goal){    int p;    push_down(x);    while(tree[x].f != goal)    {        p = tree[x].f;        if(tree[p].f == goal)        {            if(tree[p].l == x)                zig(x);            if(tree[p].r == x)                zag(x);        }        else        {            int g = tree[p].f;            if(tree[g].l == p && tree[p].l == x)            {                zig(p);                zig(x);            }            else if(tree[g].l == p && tree[p].r == x)            {                zag(x);                zig(x);            }            else if(tree[g].r == p && tree[p].l == x)            {                zig(x);                zag(x);            }            else if(tree[g].r == p && tree[p].r == x)            {                zag(p);                zag(x);            }        }    }    push_up(x);    return x;}int build(int l,int r,int fa){    if(l > r)        return 0;    int mid = (l + r)>>1;    int p = newnode(mid);    tree[p].f = fa;    tree[p].l = build(l,mid - 1,p);    tree[p].r = build(mid + 1,r,p);    push_up(p);    return p;}void prepare(int &root){    root = newnode(-1);    tree[root].r = newnode(-1);    tree[tree[root].r].f = root;    tree[tree[root].r].l = build(1,n,tree[root].r);push_up(tree[root].r);push_up(root);}int find(int pos,int rt){    if(!rt)        return 0;push_down(rt);    if(tree[tree[rt].l].size == pos - 1)        return rt;    if(tree[tree[rt].l].size >= pos)        return find(pos,tree[rt].l);    else        return find(pos - tree[tree[rt].l].size - 1,tree[rt].r);}int getnext(int rt){    push_down(rt);    while(tree[rt].l)    {        rt = tree[rt].l;        push_down(rt);    }    return rt;}void cut(int a,int b,int c,int &root){    int l = find(a,root);    int r = find(b + 2,root);    root = splay(l,0);    tree[root].r = splay(r,root);    int tmp = tree[tree[root].r].l;    tree[tree[root].r].l = 0;    push_up(tree[root].r);    push_up(root);    int t = find(c + 1,root);    root = splay(t,0);    int nt = getnext(tree[root].r);    tree[root].r = splay(nt,root);    tree[tree[root].r].l = tmp;tree[tmp].f = tree[root].r;    push_up(tree[root].r);    push_up(root);}void Reverse(int a,int b,int &root){    int l = find(a,root);    int r = find(b + 2,root);    root = splay(l,0);    tree[root].r = splay(r,root);    tree[tree[tree[root].r].l].lazy ^= 1;}void print(int rt){    if(!rt)        return ;push_down(rt);    print(tree[rt].l);if(cnt >= 1 && cnt <= n){    printf("%d",tree[rt].key);if(cnt < n)putchar(' ');elseputchar(10);}cnt ++;    print(tree[rt].r);}int main(){    char op[8];    int a,b,c;    int root;    while(scanf("%d",&n),n > 0)    {        scanf("%d",&m);        init();        prepare(root);        while(m --)        {            scanf("%s",op);            scanf("%d%d",&a,&b);            if(*op == 'C')            {                scanf("%d",&c);                cut(a,b,c,root);            }            else                Reverse(a,b,root);        }        cnt = 0;        print(root);    }    return 0;}//421MS8480K/*8 2CUT 3 5 4FLIP 2 68 1FLIP 2 68 1CUT 1 2 68 2FLIP 4 7FLIP 4 78 2FLIP 2 5FLIP 4 7-1 -1*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.