zoukankan      html  css  js  c++  java
  • bzoj1503 郁闷的出纳员 splay版

    自己yy的写法 可能有点奇怪吧 详情看代码 还是蛮短的
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int M=100055,inf=0x3f3f3f3f;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    int t,root,sum,n,mn,delta,now,leave;
    int c[M][2],v[M],size[M],fa[M];
    void up(int k){size[k]=size[c[k][0]]+size[c[k][1]]+1;}
    void rotate(int x,int& k){
        int y=fa[x],z=fa[y],l=0,r=1;
        if(c[y][1]==x) l=1,r=0;
        if(y==k) k=x;
        else{if(c[z][0]==y) c[z][0]=x; else c[z][1]=x;}
        fa[y]=x; fa[x]=z; fa[c[x][r]]=y;
        c[y][l]=c[x][r]; c[x][r]=y;
        up(y); up(x);
    }
    void splay(int x,int& k){
        while(x!=k){
            int y=fa[x],z=fa[y];
            if(y!=k){
                if(c[z][0]==y^c[y][0]==x) rotate(y,k);
                else rotate(x,k);
            }
            rotate(x,k);
        }
    }
    void insert(int &k,int w,int last){
        if(!k){k=++sum; now=k; fa[k]=last; size[k]=1; v[k]=w; return ;}
        if(w<v[k]) insert(c[k][0],w,k);
        else insert(c[k][1],w,k);
        up(k);
    }
    void push_before(int k,int w){
        if(!k) return ;
        if(v[k]<w) {t=k; push_before(c[k][1],w);}
        else push_before(c[k][0],w);
    }
    int find(int x,int rank){
        if(!x) return 0;
        int l=c[x][0],r=c[x][1];
        if(size[l]+1==rank) return x;
        else if(size[l]>=rank) return find(l,rank);
        else return find(r,rank-size[l]-1);
    }
    int main()
    {
        n=read(); mn=read();
        char ch[15]; int k,s;
        while(n--){
            scanf("%s",ch); k=read();
            if(ch[0]=='I'&&k>=mn) insert(root,k-delta,0),splay(now,root);
            if(ch[0]=='A') delta+=k;
            if(ch[0]=='S') {
                delta-=k,t=0,push_before(root,mn-delta); if(!t) continue;
                splay(t,root); int l=c[t][0]; leave+=size[l]+1; root=c[t][1]; fa[root]=0;
            }
            if(ch[0]=='F'){
                if(k>size[root]) printf("-1
    ");
                else s=find(root,size[root]-k+1),splay(s,root),printf("%d
    ",v[root]+delta);
            }
            //printf("%d ",root); printf("%d [%d %d %d]
    ",v[root],size[root],c[root][0],c[root][1]);
        }
        printf("%d
    ",leave);
        return 0;
    }
    View Code
  • 相关阅读:
    Anagram
    HDU 1205 吃糖果(鸽巢原理)
    Codeforces 1243D 0-1 MST(补图的连通图数量)
    Codeforces 1243C Tile Painting(素数)
    Codeforces 1243B2 Character Swap (Hard Version)
    Codeforces 1243B1 Character Swap (Easy Version)
    Codeforces 1243A Maximum Square
    Codeforces 1272E Nearest Opposite Parity(BFS)
    Codeforces 1272D Remove One Element
    Codeforces 1272C Yet Another Broken Keyboard
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/6922425.html
Copyright © 2011-2022 走看看