HDOJ 3487 Play with Chain

来源:互联网 发布:vb打印九九乘法表 编辑:程序博客网 时间:2024/05/16 09:30

裸的Splay  翻转+剪切粘贴

Play with Chain

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


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 2CUT 3 5 4FLIP 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
 

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=330000;const int INF=0x3f3f3f3f;#define Key_Value ch[ch[root][1]][0]int ch[maxn][2],rev[maxn],sz[maxn],pre[maxn],key[maxn];int root,tot1;int s[maxn],tot2;int n,m,ans[maxn],cnt;void NewNode(int& x,int father,int k){    if(tot2) x=s[tot2--];    else x=++tot1;    ch[x][0]=ch[x][1]=rev[x]=0;    pre[x]=father; key[x]=k; sz[x]=1;}void Upd_Rev(int x){    if(!x) return ;    swap(ch[x][0],ch[x][1]);    rev[x]^=1;}void Push_Up(int x){    sz[x]=sz[ch[x][1]]+sz[ch[x][0]]+1;}void Push_Down(int x){    if(rev[x])    {        Upd_Rev(ch[x][0]); Upd_Rev(ch[x][1]);        rev[x]=0;    }}void Build(int& x,int l,int r,int fa){    if(l>r) return ;    int mid=(l+r)/2;    NewNode(x,fa,mid);    Build(ch[x][0],l,mid-1,x);    Build(ch[x][1],mid+1,r,x);    Push_Up(x);}void Init(){    root=tot1=tot2=0;    ch[root][0]=ch[root][1]=pre[root]=sz[root]=0;    key[root]=INF;    NewNode(root,0,INF);    NewNode(ch[root][1],root,INF);    Build(Key_Value,1,n,ch[root][1]);    Push_Up(ch[root][1]);    Push_Up(root);}void Rotate(int x,int kind){    int y=pre[x];    Push_Down(y),Push_Down(x);    ch[y][!kind]=ch[x][kind];    pre[ch[x][kind]]=y;    if(pre[y])        ch[pre[y]][ch[pre[y]][1]==y]=x;    pre[x]=pre[y];    pre[y]=x;    ch[x][kind]=y;    Push_Up(y);}void Splay(int r,int goal){    Push_Down(r);    while(pre[r]!=goal)    {        if(pre[pre[r]]==goal)        {            Push_Down(pre[r]);            Push_Down(r);            Rotate(r,ch[pre[r]][0]==r);        }        else        {            Push_Down(pre[pre[r]]);            Push_Down(pre[r]);            Push_Down(r);            int y=pre[r];            int kind=(ch[pre[y]][0]==y);            if(ch[y][kind]==r) Rotate(r,!kind);            else Rotate(y,kind);            Rotate(r,kind);        }    }    Push_Up(r);    if(goal==0) root=r;}int Get_Kth(int r,int k){    Push_Down(r);    int t=sz[ch[r][0]]+1;    if(k==t) return r;    if(t<k) return Get_Kth(ch[r][1],k-t);    else return Get_Kth(ch[r][0],k);}int Get_Min(int r){    Push_Down(r);    while(ch[r][0])    {        r=ch[r][0];        Push_Down(r);    }    return r;}int Get_Max(int r){    Push_Down(r);    while(ch[r][1])    {        r=ch[r][1];        Push_Down(r);    }    return r;}/*************doit*************/void REVERSE(int L,int R){    Splay(Get_Kth(root,L),0);    Splay(Get_Kth(root,R+2),root);    Upd_Rev(Key_Value);    Push_Up(ch[root][1]);    Push_Up(root);}void CUT(int L,int R,int P){    Splay(Get_Kth(root,L),0);    Splay(Get_Kth(root,R+2),root);    int temp=Key_Value;    Key_Value=0;    Push_Up(ch[root][1]);    Push_Up(root);    Splay(Get_Kth(root,P+1),0);    Splay(Get_Kth(root,P+2),root);    Key_Value=temp;    pre[temp]=ch[root][1];    Push_Up(ch[root][1]);    Push_Up(root);}void Get_Order(int x){    if(x)    {        Push_Down(x);        Get_Order(ch[x][0]);        ans[cnt++]=key[x];        Get_Order(ch[x][1]);    }}/***********DEBUG**************/void showit(int x){    if(x)    {        Push_Down(x);        showit(ch[x][0]);        printf("结点: %2d key:%d 左儿子: %2d 右儿子: %2d 父结点: %2d size: %2d rev: %2d \n",               x,key[x],ch[x][0],ch[x][1],pre[x],sz[x],rev[x]);        showit(ch[x][1]);    }}void debug(){    cout<<"root: "<<root<<endl;    showit(root);}/*******************************/int main(){    char op[20];    int a,b,c;    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n==-1&&m==-1) break;        Init();      //  debug();        while(m--)        {            scanf("%s",op);            if(op[0]=='F')            {                scanf("%d%d",&a,&b);                REVERSE(a,b);            }            else if(op[0]=='C')            {                scanf("%d%d%d",&a,&b,&c);                CUT(a,b,c);            }           // debug();        }        cnt=0;        Get_Order(root);        for(int i=1;i<=n;i++)        {            if(i!=1) putchar(32);            printf("%d",ans[i]);        }        putchar(10);    }    return 0;}




2 0
原创粉丝点击