bzoj 3224: Tyvj 1728 普通平衡树

模版题;
—treap

#include<bits/stdc++.h>
#define rep(i,k,n) for(int i=k;i<=(n);i++)
using namespace std;
const int maxn = 400000;
const int inf = 0x7f7f7f7f;
struct Treap {
    int ch[maxn][2], s[maxn], sum[maxn], val[maxn], key[maxn], root, tot;
    Treap() {
        root = tot=0;
    }
    void debug(int x) {
        if(!x)return;

        debug(ch[x][0]);
        printf("( val: %d sum: %d size: %d ls:%d rs: %d)\n",val[x], sum[x],s[x], ch[x][0], ch[x][1]);
        debug(ch[x][1]);
    }
    void up(int x) {
        s[x] = sum[x] + s[ch[x][0]] + s[ch[x][1]];
    }
    void rotate(int& x, int d) {
        int t = ch[x][d ^ 1];
        ch[x][d ^ 1] = ch[t][d];
        ch[t][d] = x;
        up(x);
        up(t);
        x = t;
    }

    int newnode(int x) {
        int t = ++tot;
        sum[t] = s[t] = 1;
        ch[t][0] = ch[t][1] = 0;
        val[t] = x;
        key[t] = rand();
        return t;
    }
    void insert(int& u, int x) {
        if(!u) {
            u = newnode(x);
            return;
        }

        if(x == val[u]) {
            sum[u]++;
            up(u);
            return;
        }

        int d = 0;

        if(x < val[u])insert(ch[u][d], x);
        else insert(ch[u][d ^= 1], x);

        if(key[ch[u][d]] < key[u])rotate(u, d ^ 1);

        up(u);
    }
    void del(int &u, int x) {
        if(!u)return;

        if(val[u] == x) {
            if(sum[u] > 1)sum[u]--;
            else {
                if(ch[u][0]*ch[u][1] == 0) {
                    int k = ch[u][0] + ch[u][1];
                    u = k;
                    return;
                }

                int d = (key[ch[u][0]] < key[ch[u][1]]);
                rotate(u, d);
                del(ch[u][d], x);
            }
        }

        else del(ch[u][val[u] < x], x);  
        up(u);                         //up after do "del"
    }
    int getrank(int u, int x) {
        if(!u)return 0;

        if(val[u] == x)return s[ch[u][0]] + 1;

        if(val[u] < x)return s[ch[u][0]] + sum[u] + getrank(ch[u][1], x);
        else return getrank(ch[u][0], x);
    }
    int getkth(int u, int k) {
        if(!u)return 0;

        int ss = s[ch[u][0]];

        if(k <= ss)return getkth(ch[u][0], k);

        if(k > ss + sum[u])return getkth(ch[u][1], k - ss - sum[u]);

        return val[u];
    }
    int getpro(int u, int x) {
        if(!u)return -inf;

        if(val[u] < x)return max(val[u], getpro(ch[u][1], x));
        else return getpro(ch[u][0], x);
    }
    int getbehind(int u, int x) {
        if(!u)return inf;

        if(val[u] > x)return min(val[u], getbehind(ch[u][0], x));
        else return getbehind(ch[u][1], x);
    }



} treap;

int n, op, x, y;
int main() {
  //  freopen("in.in", "r", stdin);
 //   freopen("out.out", "w", stdout);
    scanf("%d", &n);
    rep(i, 1, n) {
        scanf("%d%d", &op, &x);

        if(op == 1)treap.insert(treap.root,x);

        if(op == 2)treap.del(treap.root,x);

        if(op == 3)printf("%d\n", treap.getrank(treap.root, x));

        if(op == 4)printf("%d\n", treap.getkth(treap.root,x));

        if(op == 5)printf("%d\n", treap.getpro(treap.root,x));

        if(op == 6)printf("%d\n", treap.getbehind(treap.root,x));
         //printf("\n\n");treap.debug(treap.root);
    }


}

这题业界良心,竟然给数据,感动…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值