rope(平衡二叉树)

题目描述:

链接: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.

示例:

输入5 1

2 3

输出

2 3 4 1 5

 

题意:

给你N张牌,初始顺序为1....n;有M次洗牌操作;(qi,si) 表示从【1,n】中 拿第qi张牌+si张牌 放到最上面(类似洗牌)问你到最后牌面的顺序是? 

 

题解:

rope大法好

代码:

#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
rope<int>r;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define pb push_back
int main()
{
    ios_base::sync_with_stdio(0);
    //cin.tie(0);
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            r.pb(i);
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
           r=r.substr(x-1,y)+r.substr(0,x-1)+r.substr(x+y-1,n-x-y+1);
        }
       for(int i=0;i<n;++i) cout<<r[i]<<" ";
    }

    return 0;
}

 

好像这里用了rope之后如果再用cin.tie(0);会输不出来,不知道为什么

另外关于rope有博客参考:

https://blog.csdn.net/tianwei0822/article/details/81240063

https://blog.csdn.net/tianwei0822/article/details/81240063

 下面是splay的做法:

#include <bits/stdc++.h>
using namespace std;
const int N=200010;
namespace Splay{
    int a[N]; 
    int val[N],mn[N],tag[N],size[N],son[N][2],f[N],tot,root;bool rev[N];
    int build(int,int,int);
    void Initialize(int n){tot=0;root=build(0,n+1,0);} 
    void rev1(int x){if(!x)return;swap(son[x][0],son[x][1]);rev[x]^=1;}
    void add1(int x,int p){if(!x)return;val[x]+=p;mn[x]+=p;tag[x]+=p;}
    void pb(int x){
        if(rev[x]){
            rev1(son[x][0]);
            rev1(son[x][1]);
            rev[x]=0;
        }
        if(tag[x]){
            add1(son[x][0],tag[x]);
            add1(son[x][1],tag[x]);
            tag[x]=0;
        }
    }
    void up(int x){
        size[x]=1,mn[x]=val[x];
        if(son[x][0]){
            size[x]+=size[son[x][0]];
            if(mn[x]>mn[son[x][0]])mn[x]=mn[son[x][0]];
        }
        if(son[x][1]){
            size[x]+=size[son[x][1]];
            if(mn[x]>mn[son[x][1]])mn[x]=mn[son[x][1]];
        }
    }
    void rotate(int x){
        int y=f[x],w=son[y][1]==x;
        son[y][w]=son[x][w^1];
        if(son[x][w^1])f[son[x][w^1]]=y;
        if(f[y]){
            int z=f[y];
            if(son[z][0]==y)son[z][0]=x;
            if(son[z][1]==y)son[z][1]=x;
        }f[x]=f[y];son[x][w^1]=y;f[y]=x;up(y);
    }
    void splay(int x,int w){
        int s=1,i=x,y;a[1]=x;
        while(f[i])a[++s]=i=f[i];
        while(s)pb(a[s--]);
        while(f[x]!=w){
            y=f[x];
            if(f[y]!=w){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);}
            rotate(x);
        }if(!w)root=x;
        up(x);
    }
    int build(int l,int r,int fa){
        int x=++tot,mid=(l+r)>>1;
        f[x]=fa;val[x]=a[mid];
        if(l<mid)son[x][0]=build(l,mid-1,x);
        if(r>mid)son[x][1]=build(mid+1,r,x);
        up(x);
        return x;
    }
    int kth(int k){
        int x=root,tmp;
        while(1){
            pb(x);
            tmp=size[son[x][0]]+1;
            if(k==tmp)return x;
            if(k<tmp)x=son[x][0];else k-=tmp,x=son[x][1];
        }
    }
    void REVOLVE(int x,int y,int z){
        z%=y-x+1;
        if(z){
            int u=x,t;
            x=kth(y-z+1),y=kth(y+2);
            splay(x,0),splay(y,x),t=son[y][0];
            son[y][0]=0,up(y),up(x);
            x=kth(u),y=kth(u+1);
            splay(x,0),splay(y,x),son[y][0]=t,f[t]=y;
            up(y),up(x);
        }
    }
    int A(int x){
        int y=kth(x+1); 
        return val[y];
    }
}
using namespace Splay;
int n,m;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)a[i]=i;
    root=build(0,n+1,0);
    while(m--){
        int x,y;
        scanf("%d%d",&x,&y);
        REVOLVE(1,x+y-1,y);
    }   
    for(int i=1;i<n;i++)printf("%d ",A(i));
    printf("%d\n",A(n));
    return 0;
}
splay

 

posted @ 2018-08-22 10:50  better46  阅读(532)  评论(0编辑  收藏  举报