Hdu3487-play with Chain (extensional tree split merge)

Source: Internet
Author: User

Problem Descriptionyaoyao is fond of playing he chains. He has a chain containing n diamonds on it. Diamonds is numbered from 1 to N. At first, the diamonds in the chain is a sequence:1, 2, 3, ..., N. He would perform and types of operations:cut a b c:he w Ill 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 would turn out to Be:1 4 3 7 6 2 5 8
He wants to know, what's the chain looks like after perform m operations.  Could help him? Inputthere'll is multiple test cases in a test data.
For each test case, the first line contains Numbers:n and M (1≤n, m≤3*100000), indicating the total number of diamond s on the chain and the number of operations respectively. Then M. lines follow, each of the 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 and the negative numbers, which should not being processed as a case. Outputfor Each test case, you should the print a line with n numbers. The ith number is the number of the ith diamond on the chain. Sample Input8 2CUT 3 5 4FLIP 2 6-1-1 Sample Output1 4 3 7 6 2 5 8 test instructions: At first the entire arrangement is 1,2,,,n, then there are two operations cut a B c separated [A, a] and inserted into the remaining number of After position c, flip a b flips [a, a] to the final arrangement. Parse: Stretch tree merge split. For the cut operation, the left and the right side of the split, and then merged together, and then divided into [1,c] and [C+1,k] (k is the number of remaining, note that the interval may be empty, special treatment is good), and then [A, a] insert. For the flip operation, split, and then [A, b] to flip. Finally, all rollover marks are pressed down. Record the answer. Code
#include <cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;Const intinf=1e9+7;Const intmaxn=300005;intn,m,ans,a[maxn],cnt;//a number of arrays, CNT is the node designator, I was using an array to simulate thestructtreap{treap* son[2];//left and right son    intV,s,rev; Treap () {v=s=rev=0; son[0]=son[1]=NULL;} Treap (intnv); intRK () {returnson[0]->s+1; }//rank, number of first few    intcmpintK//comparison, if equal returns-1, less than return 0, greater than 1    {        if(K==rk ())return-1; returnK<rk ()?0:1; }    voidPushup () {s=son[0]->s+son[1]->s+1; }//Update Size    voidPushdown ();//Handling Lazy Tags}NULL, Tr[maxn];treap::treap (intNV) {v=NV; S=1; Rev=0; son[0]=son[1]=&NULL;}voidtreap::p ushdown () {if( This==&NULL)return; if(rev) {Swap (son[0],son[1]); son[0]->rev^=1; son[1]->rev^=1; Rev=0; }}treap* NewNode (intx) {tr[cnt]=treap (x); returntr+cnt++;}structsplaytree{intSize; Treap*Root; Splaytree () {Size=0; root=&NULL; } voidRotate (treap* &t,intD//Rollover Action{T-pushdown (); Treap* p=t->son[d^1]; P-pushdown (); T->son[d^1]=p->Son[d]; P->son[d]=T; T-pushup (); T=p; T-pushup (); }    voidSplay (treap* &t,intK//extend the K-large node to the root{T-pushdown (); intD=t->CMP (k); if(d!=-1)        {            if(d) splay (t->son[d],k-t->RK ()); ElseSplay (t->son[d],k); Rotate (T,d^1); } t-pushup (); }    voidBuild (treap* &t,intLeintRi//build n number into a tree    {        if(Le>ri)return; intMid= (Le+ri)/2; T=NewNode (mid); Build (t->son[0],le,mid-1); Build (t->son[1],mid+1, RI); T-pushup (); }    voidCut (treap* &t,intAintBintc) {intlen=b-a+1; if(len==n)return;//It 's the whole range.Splay (T,a); T->pushdown ();//split out the left side of theTreap *l=t->son[0]; L-pushdown (); T->son[0]=&NULL; T->pushup (); Splay (T,len); T->pushdown ();//split the right side of theTreap *r=t->son[1]; R-pushdown (); T->son[1]=&NULL; T->pushup (); Treap*NT; if(r!=&NULL)//merge around{NT=s; Splay (NT,1); NT->son[0]=l; Nt->pushup (); }        Else{NT=m; Splay (Nt,a-1); NT->son[1]=r; Nt->pushup (); }        if(c+len==n)//after the whole thing, special treatment.{splay (nt,c); Splay (T,1); T->son[0]=NT; T-pushup (); return; } splay (Nt,c+1); Treap*l=nt->son[0]; L->pushdown (); NT->son[0]=&NULL; Nt->pushup (); T->son[1]=nt; T->pushup (); Splay (T,1); T->son[0]=l; T->pushup (); }    voidReverse (treap* &t,intAintb//Flip{splay (t,a);//leftTreap *l=t->son[0]; L-pushdown (); T->son[0]=&NULL; T->pushup (); Splay (T,b-a+1);//RightTreap *r=t->son[1]; R-pushdown (); T->son[1]=&NULL; T->pushup (); T->rev^=1;//Flip MarkerT->pushdown (); T->son[0]=l; T->pushup ();        Splay (T,B); T->son[1]=r; T->pushup (); }    voidPushall (treap* &t)//Middle Sequence Traversal    {        if(t==&NULL)return; T-pushdown (); Pushall (t->son[0]); a[++ans]=t->v; Pushall (t->son[1]); T-pushup (); }};intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {        if(n<0&&M<0) Break; Splaytree SPT; CNT=0; Spt. Build (Spt.root,1, N);//Achievements        intA,b,c; Charop[Ten];  while(m--) {scanf ("%s", op); if(op[0]=='C')//Cut Operation{scanf ("%d%d%d",&a,&b,&c); Spt.            Cut (SPT.ROOT,A,B,C); }            Else  //Flip Action{scanf ("%d%d",&a,&b); Spt.            Reverse (SPT.ROOT,A,B); }} ans=0; Spt. Pushall (Spt.root); //the whole lower pressure         for(intI=1; i<=ans;i++) printf ("%d%c", A[i],i==ans?'\ n':' '); }    return 0;}
View Code

Hdu3487-play with Chain (extensional tree split merge)

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.