pbds 平衡树模板

· · 个人记录

#include <bits/extc++.h>
using namespace __gnu_pbds;

typedef long long ll;

typedef pair<int, ll> pii;
struct SBT {
    tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> tr;
    int cnt = 0;
    void clear() {
        tr.clear();
        cnt = 0;
    }
    void ins(ll k) {
        tr.insert({k, ++cnt});
    }
    void del(ll k) {
        tr.erase(tr.lower_bound({k, 0}));
    }
    int rnk(ll k) {
        return tr.order_of_key({k, 0}) + 1;
    }
    ll kth(int x) {
        return tr.find_by_order(x - 1)->first;
    }
    ll pre(ll k) {
        return kth(rnk(k) - 1);
    }
    ll nxt(ll k) {
        return kth(rnk(k + 1));
    }
};