HDU 3487 (Play with Chain-splay) [Template:splay]

Source: Internet
Author: User

Play with ChainTime limit:6000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4679 Accepted Submission (s): 1892


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'll perform types of operations:
Cut a B c:he would 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 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
Recommendzhengfeng | We have carefully selected several similar problems for you:3486 3479 3480 3481 3482

splay Nude Questions


#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)); #define INF (2139062143) #define F (100000007) #define MAXN (300000+10) #define MAXM (300000+10) typedef Long Long Ll;ll mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} int N,m;class splay{public:iNT Father[maxn],siz[maxn],n;int ch[maxn][2],val[maxn];bool root[maxn],rev[maxn];int Roo; Rootvoid mem (int _n) {mem (father) mem (siz) mem (Root) mem (rev) mem (CH) mem (val) flag=0;n=0; roo=1; build (roo,1,_n,0); Root[1]=1;} void NewNode (int &x,int f,int v) {X=++n;father[x]=f;val[x]=v-1;} void build (int &x,int l,int r,int f) {if (l>r) return; int m= (L+R) >>1;newnode (x,f,m); Build (ch[x][0],l,m-1,x ); build (ch[x][1],m+1,r,x); maintain (x);} int getkth (int x,int k) {pushdown (x); int t;if (ch[x][0]) t=siz[ch[x][0]]; else t=0;if (t==k-1) return X;else if (t>=k) Return getkth (ch[x][0],k), Else return getkth (ch[x][1],k-t-1); void pushdown (int x) {if (x) if (rev[x]) {swap (ch[x][0],ch[x][1]), if (ch[x][0]) rev[ch[x][0]]^=1;if (ch[x][1]) rev[ch[x][ 1]]^=1;rev[x]^=1;}} void maintain (int x) {siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;} void rotate (int x) {int y=father[x],kind=ch[y][1]==x;pushdown (y); pushdown (x); Ch[y][kind]=ch[x][!kind];if (ch[y][ Kind]) {father[ch[y][kind]]=y;} father[x]=father[y];father[y]=X;ch[x][!kind]=y;if (Root[y]) {root[x]=1;root[y]=0;roo=x;} else{ch[father[x]][ch[father[x]][1]==y] = x;} Maintain (y); maintain (x);} void splay (int x) {while (!root[x]) {int y=father[x];int z=father[y];if (root[y]) rotate (x); else if ((ch[y][1]==x) ^ (Ch[z] [1]==y) {rotate (x); rotate (x);} else {rotate (y); rotate (x);}} Roo=x;} void splay (int x,int R) {while (!) ( FATHER[X]==R) {int y=father[x];int z=father[y];if (father[y]==r) rotate (x), else if ((ch[y][1]==x) ^ (ch[z][1]==y)) { Rotate (x); Rotate (x);} else {rotate (y); rotate (x);}}} void Cut (int a,int b,int c) {int x=getkth (roo,a), y=getkth (roo,b), splay (x), splay (Y,roo);p ushdown (x);p ushdown (y); int z= ch[y][0];ch[y][0]=0; Maintain (y); Maintain (x); int u=getkth (ROO,C), v=getkth (roo,c+1), splay (U), splay (V,roo);p ushdown (U);p ushdown (v); ch[v][0]=z; Father[z]=v;maintain (v); Maintain (u);} void Flip (int a,int b) {int x=getkth (roo,a), y=getkth (roo,b), splay (x), splay (Y,roo);p ushdown (x);p ushdown (y); int z=ch[y ][0];rev[z]^=1;maintain (y); Maintain (x);} BOOL flag;void Print (int x) {if (x==0) return;p ushdown (x);p rint (ch[x][0]), if (val[x]!=0&&val[x]!=n-1) {if (flag) Putchar ("); else flag=1; printf ("%d", val[x]);} Print (ch[x][1]);} }s;char S[maxn];int Main () {//freopen ("hdu3487.in", "R", stdin),//freopen (". Out", "w", stdout) and while (cin>>n> &GT;M) {if (n<0&&m<0) break;n+=2; S.mem (n); for (i,m) {scanf ("%s", s), if (s[0]== ' C ') {int a,b,c;scanf ("%d%d%d", &a,&b,&c); S.cut (a,b+2,c+1);} else {int a,b;scanf ("%d%d", &a,&b); S.flip (a,b+2);}} S.print (S.roo); Cout<<endl;} return 0;}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3487 (Play with Chain-splay) [Template:splay]

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.