牛客多校第四場——splay的區間平移操作

鏈接:https://www.nowcoder.com/acm/contest/141/C
來源:牛客網
 

Eddy likes to play cards game since there are always lots of randomness in the game. For most of the cards game, the very first step in the game is shuffling the cards. And, mostly the randomness in the game is from this step. However, Eddy doubts that if the shuffling is not done well, the order of the cards is predictable!

To prove that, Eddy wants to shuffle cards and tries to predict the final order of the cards. Actually, Eddy knows only one way to shuffle cards that is taking some middle consecutive cards and put them on the top of rest. When shuffling cards, Eddy just keeps repeating this procedure. After several rounds, Eddy has lost the track of the order of cards and believes that the assumption he made is wrong. As Eddy's friend, you are watching him doing such foolish thing and easily memorizes all the moves he done. Now, you are going to tell Eddy the final order of cards as a magic to surprise him.

Eddy has showed you at first that the cards are number from 1 to N from top to bottom.

For example, there are 5 cards and Eddy has done 1 shuffling. He takes out 2-nd card from top to 4-th card from top(indexed from 1) and put them on the top of rest cards. Then, the final order of cards from top will be [2,3,4,1,5].

輸入描述:

The first line contains two space-separated integer N, M indicating the number of cards and the number of shuffling Eddy has done.
Each of following M lines contains two space-separated integer pi, si indicating that Eddy takes pi-th card from top to (pi+si-1)-th card from top(indexed from 1) and put them on the top of rest cards.


1 ≤ N, M ≤ 105
1 ≤ pi ≤ N
1 ≤ si ≤ N-pi+1

輸出描述:

Output one line contains N space-separated integers indicating the final order of the cards from top to bottom.

示例1

輸入

5 1
2 3

輸出

2 3 4 1 5

示例2

輸入

5 2
2 3
2 3

輸出

3 4 1 2 5

示例3

輸入

5 3
2 3
1 4
2 4

輸出

3 4 1 5 2

區間翻轉三次就能完成一次題目描述的平移操作;

值得注意的是要判斷區間是否存在,當區間<=1不需要翻轉,否則WA

splay的模板copy的一位大佬的

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 100005
#define inf 1000000000
 
int n,m,l,r,root;
int a[N],f[N],ch[N][2],size[N],key[N],delta[N];
 
int get(int x)
{
    return ch[f[x]][1]==x;
}
void update(int x)
{
    size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
void pushdown(int x)
{
    if (x&&delta[x])
    {
        delta[ch[x][0]]^=1;
        delta[ch[x][1]]^=1;
        swap(ch[x][0],ch[x][1]);
        delta[x]=0;
    }
}
int build(int l,int r,int fa)
{
    if (l>r) return 0;
    int mid=(l+r)>>1;
    f[mid]=fa;key[mid]=a[mid];size[mid]=1;
    int lch=build(l,mid-1,mid);
    int rch=build(mid+1,r,mid);
    ch[mid][0]=lch,ch[mid][1]=rch;
    update(mid);
    return mid;
}
void rotate(int x)
{
    pushdown(f[x]);
    pushdown(x);
    int old=f[x],oldf=f[old],wh=get(x);
    ch[old][wh]=ch[x][wh^1];
    if (ch[old][wh]) f[ch[old][wh]]=old;
    ch[x][wh^1]=old;
    f[old]=x;
    if (oldf) ch[oldf][ch[oldf][1]==old]=x;
    f[x]=oldf;
    update(old);
    update(x);
}
void splay(int x,int tar)
{
    for (int fa;(fa=f[x])!=tar;rotate(x))
        if (f[fa]!=tar)
            rotate( (get(x)==get(fa))?fa:x );
    if (!tar) root=x;
}
int find(int x)
{
    int now=root;
    while (1)
    {
        pushdown(now);
        if (x<=size[ch[now][0]]) now=ch[now][0];
        else
        {
            x-=size[ch[now][0]];
            if (x==1) return now;
            x-=1;
            now=ch[now][1];
        }
    }
}
void write(int x)
{
    pushdown(x);
    if (ch[x][0]) write(ch[x][0]);
    if (key[x]!=-inf&&key[x]!=inf) printf("%d ",key[x]);
    if (ch[x][1]) write(ch[x][1]);
}
void reverse(int l,int r)
{
        int aa=find(l);
        int bb=find(r+2);
        splay(aa,0);
        splay(bb,aa);
        delta[ch[ch[root][1]][0]]^=1;
}
int main()
{
    scanf("%d%d",&n,&m);
    a[1]=-inf;a[n+2]=inf;
    for (int i=1;i<=n;++i) a[i+1]=i;
    root=build(1,n+2,0);
    for (int i=1;i<=m;++i)
    {
        scanf("%d%d",&l,&r);
        r=l+r-1;
        if(1<r) reverse(1,r);
        if(1<r-l+1)reverse(1,r-l+1);
        if(r-l+2<r)reverse(r-l+2,r);
 
 
    }
    write(root);
    return 0;
}

 

發佈了87 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章