library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub tko919/library

:heavy_check_mark: Randomized Binary Search Tree (set)
(DataStructure/rbstset.hpp)

Depends on

Verified with

Code

#pragma once

#include "Utility/random.hpp"


template <typename T> class RBSTset {
    struct Node {
        Node *lp = nullptr, *rp = nullptr;
        int size = 1;
        T key;
        Node(T _k = 0) : key(_k) {}
        void apply() {
            size = 1;
            if (lp)
                size += lp->size;
            if (rp)
                size += rp->size;
        }
    };
    int size(Node *x) {
        return x ? x->size : 0;
    }
    Node *merge(Node *L, Node *R) {
        if (!L)
            return R;
        if (!R)
            return L;
        if ((int)Random::get(size(L) + size(R) - 1) < size(L)) {
            L->rp = merge(L->rp, R);
            L->apply();
            return L;
        } else {
            R->lp = merge(L, R->lp);
            R->apply();
            return R;
        }
    }
    array<Node *, 2> split(Node *x, int k) {
        if (!x)
            return {nullptr, nullptr};
        if (k == size(x))
            return {x, nullptr};
        if (k <= size(x->lp)) {
            auto [lb, rb] = split(x->lp, k);
            Node *L = lb, *R = x;
            x->lp = rb;
            x->apply();
            return {L, R};
        } else {
            auto [lb, rb] = split(x->rp, k - size(x->lp) - 1);
            Node *L = x, *R = rb;
            x->rp = lb;
            x->apply();
            return {L, R};
        }
    }
    int lower_bound(Node *x, T v) {
        if (!x)
            return 0;
        if (x->key >= v)
            return lower_bound(x->lp, v);
        else
            return size(x->lp) + 1 + lower_bound(x->rp, v);
    }
    int upper_bound(Node *x, T v) {
        if (!x)
            return 0;
        if (x->key > v)
            return upper_bound(x->lp, v);
        else
            return size(x->lp) + 1 + upper_bound(x->rp, v);
    }
    void _dump(Node *cur, int depth) {
        if (!cur)
            return;
        _dump(cur->lp, depth + 1);
        rep(_, 0, depth) cerr << ' ';
        cerr << cur->key << '\n';
        _dump(cur->rp, depth + 1);
    }

  public:
    Node *root;
    RBSTset(Node *_r = nullptr) : root(_r) {}
    int size() {
        return size(root);
    }
    void merge(RBSTset &a) {
        root = merge(root, a.root);
    }
    RBSTset split(int k) {
        auto [L, R] = split(root, k);
        root = L;
        return RBSTset(R);
    }
    bool find(T x) {
        Node *cur = root;
        for (;;) {
            if (!cur)
                break;
            if (cur->key == x)
                return true;
            else if (x < cur->key)
                cur = cur->lp;
            else
                cur = cur->rp;
        }
        return false;
    }
    void insert(T x) {
        int k = lower_bound(root, x);
        auto [L, R] = split(root, k);
        root = merge(merge(L, new Node(x)), R);
    }
    void erase(T x) {
        assert(find(x));
        int k = lower_bound(root, x);
        auto [L, t] = split(root, k);
        auto [tmp, R] = split(t, 1);
        root = merge(L, R);
    }
    T kth_element(int k) {
        if (k >= size(root) or k < 0)
            return -1;
        auto [L, R] = split(root, k);
        Node *cur = R;
        while (cur->lp)
            cur = cur->lp;
        root = merge(L, R);
        return cur->key;
    }
    int lower_bound(T v) {
        return lower_bound(root, v);
    }
    int upper_bound(T v) {
        return upper_bound(root, v);
    }
    void dump() {
        _dump(root, 1);
    }
};

/**
 * @brief Randomized Binary Search Tree (set)
 */
#line 2 "DataStructure/rbstset.hpp"

#line 2 "Utility/random.hpp"

namespace Random {
mt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count());
using u64 = unsigned long long;
u64 get() {
    return randgen();
}
template <typename T> T get(T L) { // [0,L]

    return get() % (L + 1);
}
template <typename T> T get(T L, T R) { // [L,R]

    return get(R - L) + L;
}
double uniform() {
    return double(get(1000000000)) / 1000000000;
}
string str(int n) {
    string ret;
    rep(i, 0, n) ret += get('a', 'z');
    return ret;
}
template <typename Iter> void shuffle(Iter first, Iter last) {
    if (first == last)
        return;
    int len = 1;
    for (auto it = first + 1; it != last; it++) {
        len++;
        int j = get(0, len - 1);
        if (j != len - 1)
            iter_swap(it, first + j);
    }
}
template <typename T> vector<T> select(int n, T L, T R) { // [L,R]

    if (n * 2 >= R - L + 1) {
        vector<T> ret(R - L + 1);
        iota(ALL(ret), L);
        shuffle(ALL(ret));
        ret.resize(n);
        return ret;
    } else {
        unordered_set<T> used;
        vector<T> ret;
        while (SZ(used) < n) {
            T x = get(L, R);
            if (!used.count(x)) {
                used.insert(x);
                ret.push_back(x);
            }
        }
        return ret;
    }
}

void relabel(int n, vector<pair<int, int>> &es) {
    shuffle(ALL(es));
    vector<int> ord(n);
    iota(ALL(ord), 0);
    shuffle(ALL(ord));
    for (auto &[u, v] : es)
        u = ord[u], v = ord[v];
}
template <bool directed, bool simple> vector<pair<int, int>> genGraph(int n) {
    vector<pair<int, int>> cand, es;
    rep(u, 0, n) rep(v, 0, n) {
        if (simple and u == v)
            continue;
        if (!directed and u > v)
            continue;
        cand.push_back({u, v});
    }
    int m = get(SZ(cand));
    vector<int> ord;
    if (simple)
        ord = select(m, 0, SZ(cand) - 1);
    else {
        rep(_, 0, m) ord.push_back(get(SZ(cand) - 1));
    }
    for (auto &i : ord)
        es.push_back(cand[i]);
    relabel(n, es);
    return es;
}
vector<pair<int, int>> genTree(int n) {
    vector<pair<int, int>> es;
    rep(i, 1, n) es.push_back({get(i - 1), i});
    relabel(n, es);
    return es;
}
}; // namespace Random


/**
 * @brief Random
 */
#line 4 "DataStructure/rbstset.hpp"

template <typename T> class RBSTset {
    struct Node {
        Node *lp = nullptr, *rp = nullptr;
        int size = 1;
        T key;
        Node(T _k = 0) : key(_k) {}
        void apply() {
            size = 1;
            if (lp)
                size += lp->size;
            if (rp)
                size += rp->size;
        }
    };
    int size(Node *x) {
        return x ? x->size : 0;
    }
    Node *merge(Node *L, Node *R) {
        if (!L)
            return R;
        if (!R)
            return L;
        if ((int)Random::get(size(L) + size(R) - 1) < size(L)) {
            L->rp = merge(L->rp, R);
            L->apply();
            return L;
        } else {
            R->lp = merge(L, R->lp);
            R->apply();
            return R;
        }
    }
    array<Node *, 2> split(Node *x, int k) {
        if (!x)
            return {nullptr, nullptr};
        if (k == size(x))
            return {x, nullptr};
        if (k <= size(x->lp)) {
            auto [lb, rb] = split(x->lp, k);
            Node *L = lb, *R = x;
            x->lp = rb;
            x->apply();
            return {L, R};
        } else {
            auto [lb, rb] = split(x->rp, k - size(x->lp) - 1);
            Node *L = x, *R = rb;
            x->rp = lb;
            x->apply();
            return {L, R};
        }
    }
    int lower_bound(Node *x, T v) {
        if (!x)
            return 0;
        if (x->key >= v)
            return lower_bound(x->lp, v);
        else
            return size(x->lp) + 1 + lower_bound(x->rp, v);
    }
    int upper_bound(Node *x, T v) {
        if (!x)
            return 0;
        if (x->key > v)
            return upper_bound(x->lp, v);
        else
            return size(x->lp) + 1 + upper_bound(x->rp, v);
    }
    void _dump(Node *cur, int depth) {
        if (!cur)
            return;
        _dump(cur->lp, depth + 1);
        rep(_, 0, depth) cerr << ' ';
        cerr << cur->key << '\n';
        _dump(cur->rp, depth + 1);
    }

  public:
    Node *root;
    RBSTset(Node *_r = nullptr) : root(_r) {}
    int size() {
        return size(root);
    }
    void merge(RBSTset &a) {
        root = merge(root, a.root);
    }
    RBSTset split(int k) {
        auto [L, R] = split(root, k);
        root = L;
        return RBSTset(R);
    }
    bool find(T x) {
        Node *cur = root;
        for (;;) {
            if (!cur)
                break;
            if (cur->key == x)
                return true;
            else if (x < cur->key)
                cur = cur->lp;
            else
                cur = cur->rp;
        }
        return false;
    }
    void insert(T x) {
        int k = lower_bound(root, x);
        auto [L, R] = split(root, k);
        root = merge(merge(L, new Node(x)), R);
    }
    void erase(T x) {
        assert(find(x));
        int k = lower_bound(root, x);
        auto [L, t] = split(root, k);
        auto [tmp, R] = split(t, 1);
        root = merge(L, R);
    }
    T kth_element(int k) {
        if (k >= size(root) or k < 0)
            return -1;
        auto [L, R] = split(root, k);
        Node *cur = R;
        while (cur->lp)
            cur = cur->lp;
        root = merge(L, R);
        return cur->key;
    }
    int lower_bound(T v) {
        return lower_bound(root, v);
    }
    int upper_bound(T v) {
        return upper_bound(root, v);
    }
    void dump() {
        _dump(root, 1);
    }
};

/**
 * @brief Randomized Binary Search Tree (set)
 */
Back to top page