Treap 全操作模板 bzoj 3224 普通平衡树

来源:互联网 发布:淘宝网的鞋货源哪里找 编辑:程序博客网 时间:2024/05/16 18:42

BZOJ 3224 普通平衡树

Time Limit: 10 Sec Memory Limit: 128 MB
Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案

Sample Input

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的数据范围:n<=100000

2.每个数的数据范围:[-2e9,2e9]

#include<cstdio> #include<cstring>#include<algorithm>using namespace std;const int N = 100010;int n, opt, x, ans1, ans2, cc;struct Node{    Node *ch[2];    int cnt, val, key, siz;//cnt记录重复     Node(){}    void set(int x){        this->val = x;        key = rand();        siz = 1; cnt = 1;        ch[0] = ch[1] = 0;    }    int compare(int x){        if(x == val)  return -1;        return x > val ? 1 : 0;    }    void update(){        siz = this->cnt;        if(ch[0] != 0) siz += ch[0]->siz;        if(ch[1] != 0) siz += ch[1]->siz;    }}*root = 0, pool[N], *tail = pool;void rotate(Node* &t, int d){    Node *k = t->ch[d^1];    t->ch[d^1] = k->ch[d];    k->ch[d] = t;    t->update(); k->update();    t = k;}void insert(Node* &t,int x){    if(t == 0){        t = ++tail;        t->set(x); return;    }    int d = t->compare(x);    if(d == -1){        t->cnt++; t->siz++;    }    else{        t->siz++;        insert(t->ch[d], x);        if(t->ch[d]->key > t->key)            rotate(t, d^1);    }    t->update();}void del(Node* &t, int x){    if( !t ) return;    int d = t->compare(x);    if(d == -1){        Node *cc = t;        if(t->cnt > 1){t->siz--; t->cnt--;}        else if(t->ch[0] == 0){ t = t->ch[1];cc = 0;}        else if(t->ch[1] == 0){t = t->ch[0];cc = 0;}        else{            int e = t->ch[0]->key > t->ch[1]->key ? 1 : 0;            rotate(t, e); del(t->ch[e],x);        }    }    else{ t->siz--; del(t->ch[d], x);}    if(t != 0) t->update();}int rnk(Node* &t, int x){    int d = t->compare(x), delta;    if( !t->ch[0] ) delta = 0;    else delta = t->ch[0]->siz;    if(d == -1) return delta + 1;    else if( !d ) return rnk(t->ch[d], x);    else return rnk(t->ch[1], x) + delta + t->cnt;}int kth(Node* &t, int k){    if(t->ch[0] == 0 && k>=1 && k <= t->cnt) return t->val;    if( !t->ch[0] ) return kth(t->ch[1], k - t->cnt);    cc = t->ch[0]->siz + t->cnt;    if(k <= cc && k > t->ch[0]->siz) return t->val;    else if(k <= t->ch[0]->siz) return kth(t->ch[0], k);    else return kth(t->ch[1], k-cc);}void sub(Node *t, int x){    if( !t ) return;    int d = t->compare(x);    if(d == 1 || d == -1) sub(t->ch[1], x);    else if( !d ) {ans1 = t->val; sub(t->ch[0], x);}}void pre(Node *t, int x){    if( !t ) return;    int d = t->compare(x);    if(d == 1){ans1 = t->val; pre(t->ch[1], x);}    else if( !d || d == -1)  pre(t->ch[0], x);}int main(){    scanf("%d", &n);    for(int i=1; i<=n; i++){        scanf("%d%d", &opt, &x);        ans1 = 0; ans1 = 0;        switch(opt){            case 1: insert(root,x);break;            case 2: del(root,x);break;            case 3: printf("%d\n",rnk(root,x));break;            case 4: printf("%d\n",kth(root,x));break;            case 5: pre(root,x);printf("%d\n",ans1);break;            case 6: sub(root,x);printf("%d\n",ans1);break;        }    }    return 0;}
原创粉丝点击