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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值