01Trie 实现普通平衡树

· · 算法·理论

01 Trie 实现普通平衡树

我估计是除了 rope 外最短的平衡树模板代码了

注:下面将以 P3369 【模板】普通平衡树 作为参考。

操作实现细节

为方便论述,对 01 Trie 进行如下定义:

  1. 若某个非根节点与其父节点的连边的权值为 0,我们设其为其父节点的左儿子,反之为右儿子;
  2. 若某个非叶子节点与其右儿子连边的权值为 2^k ,则此节点为第 k 层,叶子节点为第 -1 层;
  3. 每个节点的 key 为当前节点所表示的数值,val 为以此节点为根节点的子树的每个节点所表示的数值在 M 中数量的和;
  4. 根节点下表为 1;
  5. 左儿子为 son_0,右儿子为 son_1。

另外,对于类似 "p 的第 5 位" 的描述,默认其含义为数值二进制表达式中的某一位。

*01 Trie 基本性质

  1. 01 Trie 每个节点均表示仅一个 key。
  2. 任意子树的左子树内的节点的 key 均小于右子树内的节点的 key。

插入 p(insert)

从节点 1 开始,依次向下遍历,直到遍历到叶子节点。

在遍历过程中,将遍历到的每个节点的 val 加一。

时间复杂度:O(\log{\max{x}})

删除 p(erase)

从节点 1 开始,依次向下遍历,直到遍历到叶子节点,随后判断叶子节点val 是否为 0,若不为 0,则将遍历到的每个节点 val 都减去一,否则不做任何修改。

查询 M 中有多少个数比 p 小(get_smaller)

从节点 1 开始,依次向下遍历,直到遍历到叶子节点。

在遍历到节点 x 时,若 p 的第 k 位为 0,继续向下遍历即可,否则将答案累加 son_0 的 val,再向下遍历。

查询排名位于第 p 位的数(find_num)

从节点 1 开始,依次向下遍历,直到遍历到叶子节点。

当遍历到节点 x 时,若此时 p \le son_0 的 val,则直接继续遍历,否则 p 需减去 son_0 的 val,随后继续便利求右子树中排名位于新的 p 的数。

查询 M 中 q 的前驱

使用 get_smaller 与 find_num 函数实现,先用 get_smaller 求出比 q 小的数量 cnt,随后用 find_num 找到排名第 cnt 的输出即可。

查询 M 中 q 的后继

使用 get_smaller 与 find_num 函数实现,先用 get_smaller 求出比 q + 1 小的数量 cnt(即小于等于 q 的数量),随后用 find_num 找到排名第 cnt + 1 的输出即可。

完整代码参考(97 行)

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

const int offset = 1e7;
int n;
vector<int> t;
vector<int> s[2];

void init() {
    t.push_back(0);
    s[0].push_back(0);
    s[1].push_back(0);
}

bool have_bit(int k, int p) {
    return p >> k & 1;
}

void ensure(int x, bool c) {
    if (s[c][x] == 0) {
        s[c][x] = ++n;
        init();
    }
}

void insert(int x, int k, int p) {
    t[x]++;
    if (k < 0) return ;
    bool c = have_bit(k, p);
    ensure(x, c);
    insert(s[c][x], k - 1, p);
}

void erase(int x, int k, int p) {
    if (k < 0) {
        if (t[x] > 0) t[x]--;
        return ;
    }
    bool c = have_bit(k, p);
    if (s[c][x] == 0) return ;
    erase(s[c][x], k - 1, p);
    t[x] = t[s[0][x]] + t[s[1][x]];
}

int get_smaller(int x, int k, int p) {
    if (x == 0 || k < 0) return 0;
    bool c = have_bit(k, p);
    int ans = get_smaller(s[c][x], k - 1, p);
    if (c == true) ans += t[s[0][x]];
    return ans;
}

int get_num(int x, int k, int cnt) {
    if (k < 0) return 0;
    if (cnt <= t[s[0][x]])
        return get_num(s[0][x], k-1, cnt);
    else
        return (1 << k) + get_num(s[1][x], k-1, cnt-t[s[0][x]]);
}

int get_prev(int p) {
    int cnt = get_smaller(1, 23, p);
    if (cnt == 0) return INT_MIN;
    return get_num(1, 23, cnt);
}

int get_next(int p) {
    int cnt = get_smaller(1, 23, p + 1);
    int total = t[1];
    if (cnt >= total) return INT_MAX;
    return get_num(1, 23, cnt + 1);
}

int m;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    n = 1;
    t = {0, 0};
    s[0] = {0, 0};
    s[1] = {0, 0};
    cin >> m;
    while(m--) {
        int opt, x;
        cin >> opt >> x;
        if (opt != 4) x += offset;
        if (opt == 1) insert(1, 23, x);
        else if (opt == 2) erase(1, 23, x);
        else if (opt == 3) cout << get_smaller(1, 23, x) + 1 << '\n';
        else if (opt == 4) cout << get_num(1, 23, x) - offset << '\n';
        else if (opt == 5) cout << get_prev(x) - offset << '\n';
        else if (opt == 6) cout << get_next(x) - offset << '\n';
    }
}