Treap模板

#include <iostream>
#include <algorithm>

const int INF = 0x7fffffff;
const int MAXN = 100005;
using namespace std;

int root = 0;
struct {
    int lef, rgh;
    int val, key;
    int cnt, sub;
} treap[MAXN];
int t = 0;

void Updata(int p) {
    treap[p].sub = treap[treap[p].lef].sub + treap[treap[p].rgh].sub + treap[p].cnt;
}

void zig(int &p) {
    int tmp = treap[p].lef;
    treap[p].lef = treap[tmp].rgh;
    treap[tmp].rgh = p;
    p = tmp;
    Updata(treap[p].rgh);
    Updata(p);
}

void zag(int &p) {
    int tmp = treap[p].rgh;
    treap[p].rgh = treap[tmp].lef;
    treap[tmp].lef = p;
    p = tmp;
    Updata(treap[p].lef);
    Updata(p);
}

void Insert(int &p, int x) {
    if (p == 0) {
        treap[++t] = {0, 0, x, rand(), 1, 1};
        p = t;
        return;
    }
    if (x == treap[p].val) {
        treap[p].cnt++;
        Updata(p);
        return;
    }
    if (x < treap[p].val) {
        Insert(treap[p].lef, x);
        if (treap[p].key < treap[treap[p].lef].key)zig(p);
    }
    if (x > treap[p].val) {
        Insert(treap[p].rgh, x);
        if (treap[p].key < treap[treap[p].rgh].key)zag(p);
    }
    Updata(p);
}

void Erase(int &p, int x) {
    if (p == 0)return;
    if (x == treap[p].val) {
        if (treap[p].cnt > 1) {
            treap[p].cnt--;
            Updata(p);
            return;
        }
        if (treap[p].lef || treap[p].rgh) {
            if (treap[p].rgh == 0 || treap[treap[p].lef].key > treap[treap[p].rgh].key) {
                zig(p);
                Erase(treap[p].rgh, x);
            } else {
                zag(p);
                Erase(treap[p].lef, x);
            }
        } else p = 0;
    } else if (x < treap[p].val)Erase(treap[p].lef, x);
    else if (x > treap[p].val)Erase(treap[p].rgh, x);
    Updata(p);
}

int Getval(int p, int x) {
    if (p == 0)return 0;
    if (x == treap[p].val)return treap[treap[p].lef].sub + 1;
    if (x < treap[p].val)return Getval(treap[p].lef, x);
    return Getval(treap[p].rgh, x) + treap[treap[p].lef].sub + treap[p].cnt;
}

int Getrank(int p, int x) {
    if (p == 0)return INF;
    if (treap[treap[p].lef].sub >= x)return Getrank(treap[p].lef, x);
    if (treap[treap[p].lef].sub + treap[p].cnt >= x)return treap[p].val;
    return Getrank(treap[p].rgh, x - treap[treap[p].lef].sub - treap[p].cnt);
}

int Getpre(int p, int x) {
    if (p == 0)return -INF;
    if (x <= treap[p].val)return Getpre(treap[p].lef, x);
    return max(treap[p].val, Getpre(treap[p].rgh, x));
}

int Getnext(int p, int x) {
    if (p == 0)return INF;
    if (x >= treap[p].val)return Getnext(treap[p].rgh, x);
    return min(treap[p].val, Getnext(treap[p].lef, x));
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int op, x;
        cin >> op >> x;
        if (op == 1)Insert(root, x);
        if (op == 2)Erase(root, x);
        if (op == 3)cout << Getval(root, x) << endl;
        if (op == 4)cout << Getrank(root, x) << endl;
        if (op == 5)cout << Getpre(root, x) << endl;
        if (op == 6)cout << Getnext(root, x) << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值