penguin8331's Library

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

View the Project on GitHub penguin8331/library

:heavy_check_mark: data-structure/binary-trie.hpp

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"

template <typename INT, size_t MAX_DIGIT>
struct BinaryTrie {
    struct Node {
        size_t count;
        Node *prev, *left, *right;
        explicit Node(Node *prev)
            : count(0), prev(prev), left(nullptr), right(nullptr) {}
    };
    INT lazy;
    Node *root;

    // constructor
    BinaryTrie() : lazy(0), root(emplace(nullptr)) {}
    inline size_t get_count(Node *v) const { return v ? v->count : 0; }
    inline size_t size() const { return get_count(root); }

    // add and get value of Node
    inline void add(INT val) { lazy ^= val; }
    inline INT get(Node *v) {
        if (!v) return -1;
        INT res = 0;
        for (int i = 0; i < MAX_DIGIT; ++i) {
            if (v == v->prev->right) res |= (1 << i);
            v = v->prev;
        }
        return res ^ lazy;
    }

    // find Node* whose value is val
    Node *find(INT val) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (flag)
                v = v->right;
            else
                v = v->left;
            if (!v) return v;
        }
        return v;
    }

    // insert
    inline Node *emplace(Node *prev) { return new Node(prev); }
    void insert(INT val, size_t k = 1) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (flag && !v->right) v->right = emplace(v);
            if (!flag && !v->left) v->left = emplace(v);
            if (flag)
                v = v->right;
            else
                v = v->left;
        }
        v->count += k;
        while ((v = v->prev))
            v->count = get_count(v->left) + get_count(v->right);
    }

    // erase
    Node *clear(Node *v) {
        if (!v || get_count(v)) return v;
        delete (v);
        return nullptr;
    }
    bool erase(Node *v, size_t k = 1) {
        if (!v) return false;
        v->count -= k;
        while ((v = v->prev)) {
            v->left = clear(v->left);
            v->right = clear(v->right);
            v->count = get_count(v->left) + get_count(v->right);
        }
        return true;
    }
    bool erase(INT val) {
        auto v = find(val);
        return erase(v);
    }

    // max (with xor-addition of val) and min (with xor-addition of val)
    Node *get_max(INT val = 0) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (!v->right)
                v = v->left;
            else if (!v->left)
                v = v->right;
            else if (flag)
                v = v->left;
            else
                v = v->right;
        }
        return v;
    }
    Node *get_min(INT val = 0) {
        return get_max(~val & ((1 << MAX_DIGIT) - 1));
    }

    // lower_bound, upper_bound
    Node *get_cur_node(Node *v, int i) {
        if (!v) return v;
        Node *left = v->left, *right = v->right;
        if ((lazy >> i) & 1) swap(left, right);
        if (left)
            return get_cur_node(left, i + 1);
        else if (right)
            return get_cur_node(right, i + 1);
        return v;
    }
    Node *get_next_node(Node *v, int i) {
        if (!v->prev) return nullptr;
        Node *left = v->prev->left, *right = v->prev->right;
        if ((lazy >> (i + 1)) & 1) swap(left, right);
        if (v == left && right)
            return get_cur_node(right, i);
        else
            return get_next_node(v->prev, i + 1);
    }
    Node *lower_bound(INT val) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (flag && v->right)
                v = v->right;
            else if (!flag && v->left)
                v = v->left;
            else if ((val >> i) & 1)
                return get_next_node(v, i);
            else
                return get_cur_node(v, i);
        }
        return v;
    }
    Node *upper_bound(INT val) { return lower_bound(val + 1); }
    size_t order_of_val(INT val) {
        Node *v = root;
        size_t res = 0;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            Node *left = v->left, *right = v->right;
            if ((lazy >> i) & 1) swap(left, right);
            bool flag = (val >> i) & 1;
            if (flag) {
                res += get_count(left);
                v = right;
            } else
                v = left;
        }
        return res;
    }

    // k-th, k is 0-indexed
    Node *get_kth(size_t k, INT val = 0) {
        Node *v = root;
        if (get_count(v) <= k) return nullptr;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (lazy >> i) & 1;
            Node *left = (flag ? v->right : v->left);
            Node *right = (flag ? v->left : v->right);
            if (get_count(left) <= k)
                k -= get_count(left), v = right;
            else
                v = left;
        }
        return v;
    }

    // debug
    void print(Node *v, const string &prefix = "") {
        if (!v) return;
        cout << prefix << ": " << v->count << endl;
        print(v->left, prefix + "0");
        print(v->right, prefix + "1");
    }
    void print() { print(root); }
};
#line 2 "template/template.hpp"
#include <bits/stdc++.h>
#line 3 "template/macro.hpp"

#define overload3(_1, _2, _3, name, ...) name
#define all1(v) std::begin(v), std::end(v)
#define all2(v, a) std::begin(v), std::begin(v) + a
#define all3(v, a, b) std::begin(v) + a, std::begin(v) + b
#define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__)
#define rall1(v) std::rbegin(v), std::rend(v)
#define rall2(v, a) std::rbegin(v), std::rbegin(v) + a
#define rall3(v, a, b) std::rbegin(v) + a, std::rbegin(v) + b
#define rall(...) overload3(__VA_ARGS__, rall3, rall2, rall1)(__VA_ARGS__)
#define elif else if
#define updiv(N, X) (((N) + (X) - 1) / (X))
#define sigma(a, b) (((a) + (b)) * ((b) - (a) + 1) / 2)
#define INT(...)     \
    int __VA_ARGS__; \
    scan(__VA_ARGS__)
#define LL(...)     \
    ll __VA_ARGS__; \
    scan(__VA_ARGS__)
#define STR(...)        \
    string __VA_ARGS__; \
    scan(__VA_ARGS__)
#define CHR(...)      \
    char __VA_ARGS__; \
    scan(__VA_ARGS__)
#define DOU(...)        \
    double __VA_ARGS__; \
    scan(__VA_ARGS__)
#define LD(...)     \
    ld __VA_ARGS__; \
    scan(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#line 3 "template/alias.hpp"

using ll = long long;
using ld = long double;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
constexpr int inf = 1 << 30;
constexpr ll INF = 1LL << 60;
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int mod = 998244353;
constexpr int MOD = 1e9 + 7;
#line 3 "template/func.hpp"

template <typename T>
inline bool chmax(T& a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template <typename T>
inline bool chmin(T& a, T b) { return ((a > b) ? (a = b, true) : (false)); }
template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    os << p.first << " " << p.second;
    return os;
}
template <typename T, typename U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    is >> p.first >> p.second;
    return is;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = std::begin(v); it != std::end(v);) {
        os << *it << ((++it) != std::end(v) ? " " : "");
    }
    return os;
}
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &in : v) {
        is >> in;
    }
    return is;
}
inline void scan() {}
template <class Head, class... Tail>
inline void scan(Head &head, Tail &...tail) {
    std::cin >> head;
    scan(tail...);
}
template <class T>
inline void print(const T &t) { std::cout << t << '\n'; }
template <class Head, class... Tail>
inline void print(const Head &head, const Tail &...tail) {
    std::cout << head << ' ';
    print(tail...);
}
template <class... T>
inline void fin(const T &...a) {
    print(a...);
    exit(0);
}
#line 3 "template/util.hpp"

struct IOSetup {
    IOSetup() {
        std::cin.tie(nullptr);
        std::ios::sync_with_stdio(false);
        std::cout.tie(0);
        std::cout << std::fixed << std::setprecision(12);
        std::cerr << std::fixed << std::setprecision(12);
    }
} IOSetup;
#line 3 "template/debug.hpp"

#ifdef LOCAL
#include <dump.hpp>
#else
#define debug(...)
#endif
#line 8 "template/template.hpp"
using namespace std;
#line 3 "data-structure/binary-trie.hpp"

template <typename INT, size_t MAX_DIGIT>
struct BinaryTrie {
    struct Node {
        size_t count;
        Node *prev, *left, *right;
        explicit Node(Node *prev)
            : count(0), prev(prev), left(nullptr), right(nullptr) {}
    };
    INT lazy;
    Node *root;

    // constructor
    BinaryTrie() : lazy(0), root(emplace(nullptr)) {}
    inline size_t get_count(Node *v) const { return v ? v->count : 0; }
    inline size_t size() const { return get_count(root); }

    // add and get value of Node
    inline void add(INT val) { lazy ^= val; }
    inline INT get(Node *v) {
        if (!v) return -1;
        INT res = 0;
        for (int i = 0; i < MAX_DIGIT; ++i) {
            if (v == v->prev->right) res |= (1 << i);
            v = v->prev;
        }
        return res ^ lazy;
    }

    // find Node* whose value is val
    Node *find(INT val) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (flag)
                v = v->right;
            else
                v = v->left;
            if (!v) return v;
        }
        return v;
    }

    // insert
    inline Node *emplace(Node *prev) { return new Node(prev); }
    void insert(INT val, size_t k = 1) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (flag && !v->right) v->right = emplace(v);
            if (!flag && !v->left) v->left = emplace(v);
            if (flag)
                v = v->right;
            else
                v = v->left;
        }
        v->count += k;
        while ((v = v->prev))
            v->count = get_count(v->left) + get_count(v->right);
    }

    // erase
    Node *clear(Node *v) {
        if (!v || get_count(v)) return v;
        delete (v);
        return nullptr;
    }
    bool erase(Node *v, size_t k = 1) {
        if (!v) return false;
        v->count -= k;
        while ((v = v->prev)) {
            v->left = clear(v->left);
            v->right = clear(v->right);
            v->count = get_count(v->left) + get_count(v->right);
        }
        return true;
    }
    bool erase(INT val) {
        auto v = find(val);
        return erase(v);
    }

    // max (with xor-addition of val) and min (with xor-addition of val)
    Node *get_max(INT val = 0) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (!v->right)
                v = v->left;
            else if (!v->left)
                v = v->right;
            else if (flag)
                v = v->left;
            else
                v = v->right;
        }
        return v;
    }
    Node *get_min(INT val = 0) {
        return get_max(~val & ((1 << MAX_DIGIT) - 1));
    }

    // lower_bound, upper_bound
    Node *get_cur_node(Node *v, int i) {
        if (!v) return v;
        Node *left = v->left, *right = v->right;
        if ((lazy >> i) & 1) swap(left, right);
        if (left)
            return get_cur_node(left, i + 1);
        else if (right)
            return get_cur_node(right, i + 1);
        return v;
    }
    Node *get_next_node(Node *v, int i) {
        if (!v->prev) return nullptr;
        Node *left = v->prev->left, *right = v->prev->right;
        if ((lazy >> (i + 1)) & 1) swap(left, right);
        if (v == left && right)
            return get_cur_node(right, i);
        else
            return get_next_node(v->prev, i + 1);
    }
    Node *lower_bound(INT val) {
        INT nval = val ^ lazy;
        Node *v = root;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (nval >> i) & 1;
            if (flag && v->right)
                v = v->right;
            else if (!flag && v->left)
                v = v->left;
            else if ((val >> i) & 1)
                return get_next_node(v, i);
            else
                return get_cur_node(v, i);
        }
        return v;
    }
    Node *upper_bound(INT val) { return lower_bound(val + 1); }
    size_t order_of_val(INT val) {
        Node *v = root;
        size_t res = 0;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            Node *left = v->left, *right = v->right;
            if ((lazy >> i) & 1) swap(left, right);
            bool flag = (val >> i) & 1;
            if (flag) {
                res += get_count(left);
                v = right;
            } else
                v = left;
        }
        return res;
    }

    // k-th, k is 0-indexed
    Node *get_kth(size_t k, INT val = 0) {
        Node *v = root;
        if (get_count(v) <= k) return nullptr;
        for (int i = MAX_DIGIT - 1; i >= 0; --i) {
            bool flag = (lazy >> i) & 1;
            Node *left = (flag ? v->right : v->left);
            Node *right = (flag ? v->left : v->right);
            if (get_count(left) <= k)
                k -= get_count(left), v = right;
            else
                v = left;
        }
        return v;
    }

    // debug
    void print(Node *v, const string &prefix = "") {
        if (!v) return;
        cout << prefix << ": " << v->count << endl;
        print(v->left, prefix + "0");
        print(v->right, prefix + "1");
    }
    void print() { print(root); }
};
Back to top page