Treap

Treap:

#include<iostream>
#include<cstdio>
using namespace std;
#define INF 0x7fffffff
#define ha 19260817
#define N 100100
inline int rand(){
    static int seed=ha;
    return seed=(int)seed*482711LL%INF;
}
struct node{
    node *ch[2];
    int siz,val,key,cnt;
    inline node(){
        ch[0]=ch[1]=NULL;
        siz=cnt=0;
    }
    inline node(int k,int r):key(k),val(r){}
    inline void update(){
        siz=ch[0]->siz+ch[1]->siz+cnt;
    }
};
node *root,*null;
struct Treap{
    inline void init(){
        root=null=new node[N];
        null->ch[0]=null->ch[1]=null;
    }
    inline void rot(node* &u,int op){
        node* v=u->ch[op^1];
        u->ch[op^1]=v->ch[op];
        v->ch[op]=u;
        u->update();
        (u=v)->update();
    }
    void insert(node* &u,int k){
        if(!u || u==null){
            u=new node(k,rand());
            u->siz=u->cnt=1;
            u->ch[0]=u->ch[1]=null;
            return ;
        }
        if(u->key==k)
            u->cnt++;
        else{
            int op=k>u->key;
            insert(u->ch[op],k);
            if(u->val>u->ch[op]->val)
                rot(u,op^1);
        }
        u->update();
    }
    void erase(node* &u,int k){
        if(u==null)
            return ;
        if(u->key==k){
            if(u->cnt>1)
                u->cnt--;
            else if(u->ch[0]==null)
                u=u->ch[1];
            else if(u->ch[1]==null)
                u=u->ch[0];
            else{
                int op=u->ch[0]->val>u->ch[1]->val;
                rot(u,op);
                erase(u->ch[op],k);
            }
        }
        else erase(u->ch[k>u->key],k);
        u->update();
    }
    inline int qrnk(int val){
        int rnk=1;
        node* u=root;
        for(;u!=null;){
            if(val<u->key)
                u=u->ch[0];
            else if(val>u->key){
                rnk+=u->ch[0]->siz+u->cnt;
                u=u->ch[1];
            }
            else return rnk+u->ch[0]->siz;
        }
        return rnk;
    }
    inline int qkey(int rnk){
        node* u=root;
        for(;u!=null;){
            if(rnk<=u->ch[0]->siz)
                u=u->ch[0];
            else if(rnk>u->ch[0]->siz+u->cnt){
                rnk-=u->ch[0]->siz+u->cnt;
                u=u->ch[1];
            }
            else return u->key;
        }
        return 0;
    }
    inline int qpre(int val){
        int pre;
        node* u=root;
        for(;u!=null;){
            if(val>u->key){
                pre=u->key;
                u=u->ch[1];
            }
            else u=u->ch[0];
        }
        return pre;
    }
    inline int qsuc(int val){
        int suc;
        node* u=root;
        for(;u!=null;){
            if(val<u->key){
                suc=u->key;
                u=u->ch[0];
            }
            else u=u->ch[1];
        }
        return suc;
    }
} tr;
int n,op,x;
int main(){
    scanf("%d",&n);
    tr.init();
    for(int i=1;i<=n;i++){
        scanf("%d%d",&op,&x);
        switch(op){
            case 1:tr.insert(root,x);break;
            case 2:tr.erase(root,x);break;
            case 3:printf("%d\n",tr.qrnk(x));break;
            case 4:printf("%d\n",tr.qkey(x));break;
            case 5:printf("%d\n",tr.qpre(x));break;
            case 6:printf("%d\n",tr.qsuc(x));break;
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/pelom/p/10259990.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值