程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDOJ 3487 Play with Chain

HDOJ 3487 Play with Chain

編輯:C++入門知識

裸的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 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
#include 
#include 
#include 
#include 

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



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved