zoukankan      html  css  js  c++  java
  • splay

    splay

    #include<bits/stdc++.h>
    #define inf 2147483647
    #define res register int
    using namespace std; 
    const int N=200010;
    int tot;
    int sz[N],val[N],ch[N][2],fa[N];
    struct SPLAY{
        int root;
        inline void pushup(res x){
            if(!x)return;
            sz[x]=1;
            if(ch[x][0])sz[x]+=sz[ch[x][0]],fa[ch[x][0]]=x;
            if(ch[x][1])sz[x]+=sz[ch[x][1]],fa[ch[x][1]]=x;
        }
        inline bool get(res x){
            return ch[fa[x]][1]==x;
        }
        inline void rotate(res x){
            res f=fa[x],oldf=fa[f],k=get(x);
            ch[f][k]=ch[x][k^1];
            if(ch[x][k^1])fa[ch[x][k^1]]=f;
            ch[x][k^1]=f,fa[f]=x;
            if(oldf)ch[oldf][ch[oldf][1]==f]=x;
            fa[x]=oldf;
            pushup(f),pushup(x);
        }
        void splay(int x){
            while(1){
                int y=fa[x];
                if(!y)break;
                if(get(x)==get(y)&&fa[y])rotate(y);
                rotate(x);
            }
            root=x;
        }
        void insert(int &rt,int v){
            if(!rt){
                rt=++tot;
                val[rt]=v;
                ch[rt][0]=ch[rt][1]=0;
                sz[rt]=1;
                return;
            }
            if(v>val[rt])insert(ch[rt][1],v);
            else insert(ch[rt][0],v);
            pushup(rt);
        }
        int erased;
        void erasesuc(int&rt){
            if(ch[rt][0]){
                erasesuc(ch[rt][0]);
            }else{
                erased=val[rt];
                rt=ch[rt][1];
                return;
            }
            pushup(rt);
        }
        void erase(int&rt,int v){
            if(val[rt]==v){
                if(!ch[rt][0]||!ch[rt][1]){
                    rt=ch[rt][0]+ch[rt][1];
                }else{
                    erasesuc(ch[rt][1]);
                    val[rt]=erased;
                }
                pushup(rt);
                return;
            }
            if(v<val[rt])erase(ch[rt][0],v);
            else erase(ch[rt][1],v);
            pushup(rt);
        }
        int lst;
        int rnk(int rt,int x){
            if(!rt)return 0;
            lst=rt;
            if(x<=val[rt])return rnk(ch[rt][0],x);
            else return sz[ch[rt][0]]+1+rnk(ch[rt][1],x);
        }
        int kth(int rt,int x){
            if(x<=sz[ch[rt][0]])return kth(ch[rt][0],x);
            else if(x==sz[ch[rt][0]]+1){
            	splay(rt);
                return val[rt];
            }
            else return kth(ch[rt][1],x-sz[ch[rt][0]]-1);
        }
        int pre(int x){
            return kth(root,rnk(root,x));
        }
        int suc(int x){
            return kth(root,rnk(root,x+1)+1);
        }
        void print(int rt){
            if(!rt)return;
            print(ch[rt][0]);
            printf("%d ",val[rt]);
            print(ch[rt][1]);
        //    pushup(rt);
        }
        void insert(int x){
            insert(root,x);
            splay(tot);
        }
        void erase(int x){
            erase(root,x);
        }
        int rnk(int x){
            int ret=rnk(root,x);
            splay(lst);
            return ret;
        }
        int kth(int x){
            return kth(root,x+1);
        }
        SPLAY(){insert(inf),insert(-inf);}
    }tr;
    int main(){
        int n;
        cin>>n;
        while(n--){
            int op,x;
    		scanf("%d%d",&op,&x);
    		if(op==1){
    	        tr.insert(x);
    	    }else if(op==2){
    	    	tr.erase(x);
    		}else if(op==3){
    		    printf("%d
    ",tr.rnk(x));
    		}else if(op==4){
    			printf("%d
    ",tr.kth(x));
    		}else if(op==5){
    		    printf("%d
    ",tr.pre(x));
    		}else {
    		    printf("%d
    ",tr.suc(x));
    		}
    	}
    }
    
  • 相关阅读:
    【剑指Offer】34、第一个只出现一次的字符
    【剑指Offer】33、丑数
    【剑指Offer】32、把数组排成最小的数
    linux精彩收集
    shell-总结【摘录】
    linux -特殊符号
    linux --mount
    linux--lsof
    linux--find
    Linux之rsync数据同步服务
  • 原文地址:https://www.cnblogs.com/zhenglier/p/11238206.html
Copyright © 2011-2022 走看看