競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
Compressed_Binary_Trie.cppはCompressedBinaryTrie<T,MAX_LOG,NODES>を提供します。値が分かれるビットだけを保持する圧縮木で、非負整数の多重集合・順位・全体XOR・XOR値の最小と最大を扱います。このファイルに通常版のBinaryTrieは含まれません。
以下、B=MAX_LOG+1、size_type=std::size_tです。FindResultは{件数,ID一覧のconst参照}、ElementResultは{値,ID一覧のconst参照}です。
CompressedBinaryTrie() = default;
CompressedBinaryTrie(const CompressedBinaryTrie &) = delete;
CompressedBinaryTrie(CompressedBinaryTrie &&) = delete;
空の多重集合を作ります。コピー・ムーブはできません。
制約: 型・ビット幅・容量の共通前提を満たすこと。
計算量: O(1)。
reservevoid reserve(size_type distinct);
異なる値distinct個分の圧縮ノード領域を上限内で予約します。
制約: 型・容量・返却参照の有効期間は共通の説明に従います。
計算量: 再配置時は保持ノード数に比例。未圧縮のNODES上限は緩めません。
sizesize_type size() const noexcept;
要素数を返します。
制約: 型・容量・返却参照の有効期間は共通の説明に従います。
計算量: O(1)。
emptybool empty() const noexcept;
空ならtrueを返します。
制約: 型・容量・返却参照の有効期間は共通の説明に従います。
計算量: O(1)。
addvoid add(T x, int id = -1);
xを1個追加します。id=-1は匿名で、ID一覧には含みません。
制約: キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: 償却O(B)。IDの追加には配列拡張の費用もかかります。 再配置時は保持ノード数に比例します。
insert_uniquebool insert_unique(T x, int id = -1);
未登録のときだけxを追加してtrueを返します。登録済みなら件数・IDを変更せずfalseです。
制約: キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: 償却O(B)。IDの追加には配列拡張の費用もかかります。 再配置時は保持ノード数に比例します。
delbool del(T x, int id = -1);
xを1個削除してtrueを返します。値や指定IDがなければfalseです。ID省略時は匿名を優先します。
制約: キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: O(B)。ID指定時はその値のID数に比例する探索も加わります。
findFindResult find(T x) const;
{xの件数,ID一覧のconst参照}を返します。未登録なら{0,空配列}です。
制約: キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: O(B)。
countsize_type count(T x) const;
xの個数を匿名・重複も含めて返します。
制約: キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: O(B)。
containsbool contains(T x) const;
xが1個以上あればtrueを返します。
制約: キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: O(B)。
min_elementElementResult min_element() const;
{最小値,ID一覧のconst参照}を返します。
制約: 空でないこと。XOR引数は0<=x<2^B。
計算量: O(B)。
max_elementElementResult max_element() const;
{最大値,ID一覧のconst参照}を返します。
制約: 空でないこと。XOR引数は0<=x<2^B。
計算量: O(B)。
get_kthElementResult get_kth(std::int64_t k) const;
要素を重複も含めて昇順に数え、1始まりでk番目の{値,ID一覧のconst参照}を返します。
制約: 重複込み・1始まりで1<=k<=size()。
計算量: O(B)。
operate_xorvoid operate_xor(T x);
全要素へXORを一括適用します。後から追加する公開値の意味は変えません。
制約: キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: O(1)。
count_lesstemplate <class Bound> size_type count_less(Bound upper) const noexcept;
upper未満の要素数を返します。整数型のupper<=0なら0、キー範囲を超えればsize()です。
制約: upperは整数型。型・容量・返却参照の有効期間は共通の説明に従います。
計算量: O(B)。
min_xorElementResult min_xor(T x) const;
{x XOR yの最小値,yのID一覧のconst参照}を返します。
制約: 空でないこと。XOR引数は0<=x<2^B。
計算量: O(B)。
max_xorElementResult max_xor(T x) const;
{x XOR yの最大値,yのID一覧のconst参照}を返します。
制約: 空でないこと。XOR引数は0<=x<2^B。
計算量: O(B)。
count_xor_lesstemplate <class Bound> size_type count_xor_less(T x, Bound upper) const;
x XOR y<upperとなる要素数を返します。upperの境界動作はcount_lessと同じです。
制約: upperは整数型。キー・XORマスクは0<=x<2^B。符号付き型では非負。
計算量: O(B)。
CompressedBinaryTrie<std::uint32_t, 29> values;
values.reserve(100); // 異なる値100個分の領域を予約。
values.add(7);
values.add(3);
values.add(7);
const auto count = values.count(7); // 2。
const auto smallest = values.get_kth(1).first; // 3。
values.operate_xor(2);
const auto nearest = values.min_xor(0).first; // 1。
Tはbool・const・volatileを除く整数型です。B=MAX_LOG+1として、キーとXORマスクは0 <= x < 2^B、符号付き型では非負の範囲です。MAX_LOG省略時はstd::numeric_limits<T>::digits-1です。順位は重複込み・1始まりで、空集合の最小・最大照会はできません。
size・empty・operate_xorはO(1)、検索・順位・XOR照会・削除はO(B)、追加は償却O(B)です。ノード配列の再配置時には保持ノード数に比例する処理が発生します。IDを指定して削除するには、その値のID数に比例する探索も必要です。
非空で異なる値がD個なら、生存する実ノードは2D-1個です。確保した領域は削除で縮めず再利用します。reserve(n)は異なる値n個分を予約し、再配置時は保持ノード数に比例します。NODESは圧縮後のノード数ではなく、未圧縮の二分木に展開した根込みのノード上限です。
領域とID一覧はライブラリが所有し、コピー・ムーブはできません。IDを省略した要素は匿名で、ID一覧には含めません。ID一覧への参照は次の更新または破棄まで有効です。追加に失敗しても値・個数・IDを保ちます。
既定の入力検査はSKIP_RUNTIME_CHECKSで除けますが、容量・オーバーフローの保護は残ります。共通契約と方式の比較は二分トライの説明、完全な例は圧縮版の使用例を参照してください。
[CompressedBinaryTrie<T,MAX_LOG,NODES>]
C++20以降。重複を許す非負整数の圧縮二分トライ。B = MAX_LOG + 1。
Tはbool・const・volatileを除く整数型。MAX_LOGは最高位ビット番号。
MAX_LOGの既定値はstd::numeric_limits<T>::digits - 1。第2テンプレート引数で手動指定も可。
SKIP_RUNTIME_CHECKSで入力検査を除く。容量・オーバーフロー対策は常に有効。
キー・XORマスクは 0 <= x < 2^B。NODESは未圧縮時の根込みノード上限。
IDは重複可、-1は匿名。ID一覧は順序不定、次の更新または破棄まで有効。
CompressedBinaryTrie(): 空集合を構築する。コピー・ムーブは不可。
reserve(n): 異なる値n個分の圧縮ノード領域を上限内で事前確保。再配置時O(現在のノード数)。
size(): 重複込みの要素数をO(1)で返す。
empty(): 空集合ならtrueをO(1)で返す。
add(x,id=-1): xを1個追加する。id=-1は匿名。失敗時は値・件数・IDを保持。
insert_unique(x,id=-1): 未登録時だけ追加しtrue。登録済みならIDも変えずfalse。
del(x,id=-1): 1個削除してtrue。値・指定IDがなければfalse。ID省略時は匿名優先、なければ任意のIDを削除。
find(x): {xの件数, ID一覧}。未登録は{0,空配列}。
min_element(): {最小値, ID一覧}。空でないこと。
max_element(): {最大値, ID一覧}。空でないこと。
get_kth(k): {重複込み昇順1始まりk番目の値, ID一覧}。1 <= k <= size()。
operate_xor(x): 全要素にXORをO(1)で適用する。
count(x): 匿名・重複を含むxの件数を返す。
contains(x): xが存在すればtrueを返す。
count_less(upper): upper未満の要素数。upper<=0なら0、範囲外ならsize()。
min_xor(x): {x^yの最小値, yのID一覧}。空でないこと。
max_xor(x): {x^yの最大値, yのID一覧}。空でないこと。
count_xor_less(x,upper): x^y<upperの件数。upperはcount_lessと同様。
検索・削除はO(B)、追加は償却O(B)(再配置時O(ノード数))。
指定IDの削除はさらにその値のID数に比例する。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Set Xor-Min | compressed_binary_trie.cpp | 現行未照合・要再提出 | AC |
| 独立参照による補足検査 | compressed_binary_trie_local.cpp | ローカル補足 | — |
| Ordered Set (CompressedBinaryTrie) | coverage_structure_compressed_ordered.cpp | 現行未照合・要再提出 | AC |
| Set Xor-Min (global XOR / IDs, CompressedBinaryTrie) | coverage_structure_compressed_xor_ids.cpp | 現行未照合・要再提出 | AC |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 未測定 |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考: https://nyaannyaan.github.io/library/data-structure/binary-trie.hpp
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/binary_trie.md
//
// API一覧
// [CompressedBinaryTrie<T,MAX_LOG,NODES>]
// C++20以降。重複を許す非負整数の圧縮二分トライ。B = MAX_LOG + 1。
// Tはbool・const・volatileを除く整数型。MAX_LOGは最高位ビット番号。
// MAX_LOGの既定値はstd::numeric_limits<T>::digits - 1。第2テンプレート引数で手動指定も可。
// SKIP_RUNTIME_CHECKSで入力検査を除く。容量・オーバーフロー対策は常に有効。
// キー・XORマスクは 0 <= x < 2^B。NODESは未圧縮時の根込みノード上限。
// IDは重複可、-1は匿名。ID一覧は順序不定、次の更新または破棄まで有効。
// CompressedBinaryTrie(): 空集合を構築する。コピー・ムーブは不可。
// reserve(n): 異なる値n個分の圧縮ノード領域を上限内で事前確保。再配置時O(現在のノード数)。
// size(): 重複込みの要素数をO(1)で返す。
// empty(): 空集合ならtrueをO(1)で返す。
// add(x,id=-1): xを1個追加する。id=-1は匿名。失敗時は値・件数・IDを保持。
// insert_unique(x,id=-1): 未登録時だけ追加しtrue。登録済みならIDも変えずfalse。
// del(x,id=-1): 1個削除してtrue。値・指定IDがなければfalse。ID省略時は匿名優先、なければ任意のIDを削除。
// find(x): {xの件数, ID一覧}。未登録は{0,空配列}。
// min_element(): {最小値, ID一覧}。空でないこと。
// max_element(): {最大値, ID一覧}。空でないこと。
// get_kth(k): {重複込み昇順1始まりk番目の値, ID一覧}。1 <= k <= size()。
// operate_xor(x): 全要素にXORをO(1)で適用する。
// count(x): 匿名・重複を含むxの件数を返す。
// contains(x): xが存在すればtrueを返す。
// count_less(upper): upper未満の要素数。upper<=0なら0、範囲外ならsize()。
// min_xor(x): {x^yの最小値, yのID一覧}。空でないこと。
// max_xor(x): {x^yの最大値, yのID一覧}。空でないこと。
// count_xor_less(x,upper): x^y<upperの件数。upperはcount_lessと同様。
// 検索・削除はO(B)、追加は償却O(B)(再配置時O(ノード数))。
// 指定IDの削除はさらにその値のID数に比例する。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_COMPRESSED_BINARY_TRIE_DEFINED
#define SIRKIT_COMPRESSED_BINARY_TRIE_DEFINED
template <typename T, int MAX_LOG = std::numeric_limits<T>::digits - 1,
std::size_t NODES = 16777216>
class CompressedBinaryTrie
{
static_assert(std::is_integral_v<T> && !std::is_same_v<T, bool>);
static_assert(std::is_same_v<T, std::remove_cv_t<T>>);
static_assert(MAX_LOG >= 0 && MAX_LOG < std::numeric_limits<T>::digits);
static_assert(NODES > 0);
public:
using Container = std::vector<int>;
using size_type = std::size_t;
using FindResult = std::pair<size_type, const Container &>;
using ElementResult = std::pair<T, const Container &>;
private:
using U = std::make_unsigned_t<T>;
using Index = std::conditional_t<(NODES <= std::numeric_limits<std::uint32_t>::max()), std::uint32_t, size_type>;
static constexpr Index nil = std::numeric_limits<Index>::max();
static constexpr size_type kBits = MAX_LOG + 1;
struct Node {
size_type count = 0;
Index child[2] = {nil, nil}; // 葉のchild[0]はID配列の添字。
U key = 0; // 葉の値。内点では分岐より上位の共通接頭辞。
int split = -1;
};
struct IdSlot {
std::unique_ptr<Container> ids;
Index next = nil;
};
std::vector<Node> nodes_;
std::vector<IdSlot> id_slots_;
Index root_ = nil, free_node_ = nil, free_id_ = nil;
size_type free_count_ = 0, logical_nodes_ = 1;
U lazy_ = 0;
static U checked_key(T x) {
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (std::is_signed_v<T>)
if (x < 0) throw std::out_of_range("CompressedBinaryTrie: negative key or XOR mask");
if ((static_cast<U>(x) >> MAX_LOG) > U{1})
throw std::out_of_range("CompressedBinaryTrie: key or XOR mask exceeds MAX_LOG");
#endif
return static_cast<U>(x);
}
static unsigned bit(U x, int i) noexcept { return static_cast<unsigned>((x >> i) & U{1}); }
static int high_bit(U x) noexcept { return static_cast<int>(std::bit_width(x)) - 1; }
const Container &ids_of(Index n) const noexcept {
static constexpr Container empty;
return n == nil || nodes_[n].child[0] == nil ? empty : *id_slots_[nodes_[n].child[0]].ids;
}
Index leaf_for(U key) const noexcept {
Index p = root_;
if (p == nil) return nil;
const int split = nodes_[p].split;
// 根の分岐より上で異なるキーは、この部分木に存在しない。
if (split >= 0 && ((key ^ nodes_[p].key) >> split) > U{1}) return nil;
while (nodes_[p].split >= 0)
p = nodes_[p].child[bit(key, nodes_[p].split)];
return p;
}
Index find_leaf(U key) const noexcept {
const auto p = leaf_for(key);
return p != nil && nodes_[p].key == key ? p : nil;
}
// 確保を先に完了し、以後のノード操作は例外を送出しない。
void reserve_nodes(size_type amount) {
if (amount <= free_count_) return;
const auto additional = amount - free_count_;
const auto limit = std::min(NODES, static_cast<size_type>(nil));
// 追加後の圧縮ノード数は、検査済みの論理ノード数以下。
const auto required = nodes_.size() + additional;
if (required > nodes_.capacity()) {
const auto cap = nodes_.capacity();
const auto grown = cap > limit / 2 ? limit : std::max<size_type>(8, cap * 2);
nodes_.reserve(std::min(limit, std::max(required, grown)));
}
}
Index acquire_node() noexcept {
if (free_node_ != nil) {
const Index p = free_node_;
free_node_ = nodes_[p].child[0];
--free_count_;
nodes_[p] = Node{};
return p;
}
const auto p = static_cast<Index>(nodes_.size());
nodes_.emplace_back();
return p;
}
void release_node(Index p) noexcept {
nodes_[p].child[0] = free_node_;
free_node_ = p;
++free_count_;
}
void reserve_id() {
if (free_id_ != nil) return;
if (id_slots_.size() == id_slots_.capacity()) {
// 既存枠と追加する1枠は別々の葉を指す。葉がL個なら圧縮ノードは2L-1個。
// 総ノード数の上限を確認済みなので、必要時の枠数倍増は添字型の範囲内。
id_slots_.reserve(std::max<size_type>(4, id_slots_.capacity() * 2));
}
}
Index acquire_id(std::unique_ptr<Container> ids) noexcept {
Index p;
if (free_id_ != nil) {
p = free_id_;
free_id_ = id_slots_[p].next;
} else {
p = static_cast<Index>(id_slots_.size());
id_slots_.emplace_back();
}
id_slots_[p].ids = std::move(ids);
return p;
}
void release_id(Index p) noexcept {
id_slots_[p].ids.reset();
id_slots_[p].next = free_id_;
free_id_ = p;
}
bool insert(U key, int id, bool unique) {
std::array<Index, kBits> path;
size_type depth = 0;
Index leaf = root_;
while (leaf != nil && nodes_[leaf].split >= 0) {
path[depth++] = leaf;
leaf = nodes_[leaf].child[bit(key, nodes_[leaf].split)];
}
const bool duplicate = leaf != nil && nodes_[leaf].key == key;
if (duplicate && unique) return false;
if (size() == std::numeric_limits<size_type>::max())
throw std::overflow_error("CompressedBinaryTrie: element count overflow");
if (duplicate) {
if (id != -1) {
const Index ids = nodes_[leaf].child[0];
if (ids != nil) id_slots_[ids].ids->push_back(id);
else {
auto owned = std::make_unique<Container>(1, id);
reserve_id();
nodes_[leaf].child[0] = acquire_id(std::move(owned));
}
}
++nodes_[leaf].count;
for (size_type i = 0; i < depth; ++i) ++nodes_[path[i]].count;
return true;
}
const int split = leaf == nil ? MAX_LOG : high_bit(key ^ nodes_[leaf].key);
const size_type increment = leaf == nil ? kBits : static_cast<size_type>(split + 1);
if (increment > NODES - logical_nodes_)
throw std::length_error("CompressedBinaryTrie: node limit exceeded");
std::unique_ptr<Container> owned;
if (id != -1) owned = std::make_unique<Container>(1, id);
reserve_nodes(leaf == nil ? 1 : 2);
if (owned) reserve_id();
const Index fresh = acquire_node();
nodes_[fresh].count = 1;
nodes_[fresh].key = key;
if (owned) nodes_[fresh].child[0] = acquire_id(std::move(owned));
if (leaf == nil) root_ = fresh;
else {
size_type ancestors = 0;
while (ancestors < depth && nodes_[path[ancestors]].split > split) ++ancestors;
const Index old = ancestors == depth ? leaf : path[ancestors];
const Index branch = acquire_node();
nodes_[branch].split = split;
nodes_[branch].key = key;
nodes_[branch].count = nodes_[old].count + 1;
const auto b = bit(key, split);
nodes_[branch].child[b] = fresh;
nodes_[branch].child[b ^ 1u] = old;
if (ancestors == 0) root_ = branch;
else {
const Index parent = path[ancestors - 1];
nodes_[parent].child[bit(key, nodes_[parent].split)] = branch;
for (size_type i = 0; i < ancestors; ++i) ++nodes_[path[i]].count;
}
}
logical_nodes_ += increment;
return true;
}
ElementResult xor_extreme(U mask, bool maximum) const {
#ifndef SKIP_RUNTIME_CHECKS
if (empty()) throw std::out_of_range("CompressedBinaryTrie: min/max of an empty trie");
#endif
Index p = root_;
while (nodes_[p].split >= 0)
p = nodes_[p].child[bit(mask, nodes_[p].split) ^ static_cast<unsigned>(maximum)];
return {static_cast<T>(nodes_[p].key ^ mask), ids_of(p)};
}
template <class Bound>
size_type xor_count_less(U mask, Bound upper) const noexcept {
static_assert(std::is_integral_v<Bound>);
if constexpr (std::is_signed_v<Bound>) if (upper <= 0) return 0;
const auto limit = static_cast<std::uintmax_t>(upper);
if (limit == 0) return 0;
if ((limit >> MAX_LOG) > std::uintmax_t{1}) return size();
const U bound = static_cast<U>(limit);
Index p = root_;
size_type result = 0;
while (p != nil) {
const Node &node = nodes_[p];
const U value = node.key ^ mask;
if (node.split < 0 || high_bit(value ^ bound) > node.split)
return result + (value < bound ? node.count : 0);
const unsigned zero = bit(mask, node.split);
if (bit(bound, node.split)) {
result += nodes_[node.child[zero]].count;
p = node.child[zero ^ 1u];
} else p = node.child[zero];
}
return result;
}
public:
CompressedBinaryTrie() = default;
CompressedBinaryTrie(const CompressedBinaryTrie &) = delete;
CompressedBinaryTrie &operator=(const CompressedBinaryTrie &) = delete;
CompressedBinaryTrie(CompressedBinaryTrie &&) = delete;
CompressedBinaryTrie &operator=(CompressedBinaryTrie &&) = delete;
void reserve(size_type distinct) {
if (distinct == 0) return;
const auto limit = std::min(NODES, static_cast<size_type>(nil));
const auto required = distinct > limit / 2 ? limit : distinct * 2 - 1;
if (required > nodes_.capacity()) nodes_.reserve(required);
}
[[nodiscard]] size_type size() const noexcept { return root_ == nil ? 0 : nodes_[root_].count; }
[[nodiscard]] bool empty() const noexcept { return root_ == nil; }
void add(T x, int id = -1) { insert(checked_key(x) ^ lazy_, id, false); }
bool insert_unique(T x, int id = -1) { return insert(checked_key(x) ^ lazy_, id, true); }
bool del(T x, int id = -1) {
const U key = checked_key(x) ^ lazy_;
std::array<Index, kBits> path;
size_type depth = 0;
Index p = root_;
if (p == nil) return false;
const int split = nodes_[p].split;
// 根の分岐より上で異なるキーは、この部分木に存在しない。
if (split >= 0 && ((key ^ nodes_[p].key) >> split) > U{1}) return false;
while (nodes_[p].split >= 0) {
path[depth++] = p;
p = nodes_[p].child[bit(key, nodes_[p].split)];
}
if (nodes_[p].key != key) return false;
const Index ids = nodes_[p].child[0];
if (id != -1) {
if (ids == nil) return false;
auto &values = *id_slots_[ids].ids;
const auto it = std::find(values.begin(), values.end(), id);
if (it == values.end()) return false;
*it = values.back();
values.pop_back();
} else if (ids != nil && nodes_[p].count == id_slots_[ids].ids->size())
id_slots_[ids].ids->pop_back();
if (--nodes_[p].count != 0) {
for (size_type i = 0; i < depth; ++i) --nodes_[path[i]].count;
return true;
}
if (ids != nil) release_id(ids);
if (depth == 0) {
root_ = nil;
logical_nodes_ -= kBits;
} else {
const Index parent = path[depth - 1];
const Index sibling = nodes_[parent].child[bit(key, nodes_[parent].split) ^ 1u];
if (depth == 1) root_ = sibling;
else {
const Index grandparent = path[depth - 2];
nodes_[grandparent].child[bit(key, nodes_[grandparent].split)] = sibling;
for (size_type i = 0; i + 1 < depth; ++i) --nodes_[path[i]].count;
}
logical_nodes_ -= static_cast<size_type>(nodes_[parent].split + 1);
release_node(parent);
}
release_node(p);
return true;
}
[[nodiscard]] FindResult find(T x) const {
const auto p = find_leaf(checked_key(x) ^ lazy_);
return {p == nil ? 0 : nodes_[p].count, ids_of(p)};
}
[[nodiscard]] ElementResult min_element() const { return xor_extreme(lazy_, false); }
[[nodiscard]] ElementResult max_element() const { return xor_extreme(lazy_, true); }
[[nodiscard]] ElementResult get_kth(std::int64_t k) const {
#ifndef SKIP_RUNTIME_CHECKS
if (k <= 0 || static_cast<std::uint64_t>(k) > size())
throw std::out_of_range("CompressedBinaryTrie: k must satisfy 1 <= k <= size()");
#endif
auto rank = static_cast<size_type>(k);
Index p = root_;
while (nodes_[p].split >= 0) {
const auto zero = bit(lazy_, nodes_[p].split);
const auto left = nodes_[p].child[zero];
if (rank <= nodes_[left].count) p = left;
else { rank -= nodes_[left].count; p = nodes_[p].child[zero ^ 1u]; }
}
return {static_cast<T>(nodes_[p].key ^ lazy_), ids_of(p)};
}
void operate_xor(T x) { lazy_ ^= checked_key(x); }
[[nodiscard]] size_type count(T x) const {
const auto p = find_leaf(checked_key(x) ^ lazy_);
return p == nil ? 0 : nodes_[p].count;
}
[[nodiscard]] bool contains(T x) const { return find_leaf(checked_key(x) ^ lazy_) != nil; }
template <class Bound> [[nodiscard]] size_type count_less(Bound upper) const noexcept {
return xor_count_less(lazy_, upper);
}
[[nodiscard]] ElementResult min_xor(T x) const { return xor_extreme(checked_key(x) ^ lazy_, false); }
[[nodiscard]] ElementResult max_xor(T x) const { return xor_extreme(checked_key(x) ^ lazy_, true); }
template <class Bound> [[nodiscard]] size_type count_xor_less(T x, Bound upper) const {
return xor_count_less(checked_key(x) ^ lazy_, upper);
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1