Skip to content

Commit 121df08

Browse files
committed
Add kth access to binary trie
1 parent a8b9fb8 commit 121df08

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

docsrc/library/data_structure/binary_trie.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
```cpp
88
binary_trie<T, LOG> bt; // 空の集合で初期化 (T は整数型, LOG はビット幅)
9+
int n = bt.size(); // 要素数
910
bt.insert(x); // x を追加
1011
bt.erase(x); // x を 1 つ削除 (存在前提)
12+
T x = bt[k]; // k 番目 (0-indexed) に小さい値を取得
1113
int cnt = bt.count_xor_leq(val, k); // x XOR val <= k を満たす x の個数
1214
```

include/data_structure/binary_trie.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <array>
3+
#include <cassert>
34
#include <vector>
45

56
template <class T, int LOG> struct binary_trie {
@@ -11,6 +12,8 @@ template <class T, int LOG> struct binary_trie {
1112

1213
binary_trie() { nodes.emplace_back(); }
1314

15+
int size() const { return nodes[0].cnt; }
16+
1417
void insert(T x) {
1518
int cur = 0;
1619
nodes[cur].cnt++;
@@ -35,6 +38,24 @@ template <class T, int LOG> struct binary_trie {
3538
}
3639
}
3740

41+
T operator[](int k) const {
42+
assert(0 <= k && k < size());
43+
int cur = 0;
44+
T x = 0;
45+
for (int i = LOG - 1; i >= 0; --i) {
46+
int left = nodes[cur].ch[0];
47+
int left_cnt = left == -1 ? 0 : nodes[left].cnt;
48+
if (k < left_cnt) {
49+
cur = left;
50+
} else {
51+
k -= left_cnt;
52+
cur = nodes[cur].ch[1];
53+
x |= (T(1) << i);
54+
}
55+
}
56+
return x;
57+
}
58+
3859
// x XOR val <= k となる整数 x の個数
3960
int count_xor_leq(T val, T k) {
4061
int cur = 0;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
5+
#include "data_structure/binary_trie.hpp"
6+
7+
int main() {
8+
ios::sync_with_stdio(false);
9+
cin.tie(nullptr);
10+
int x;
11+
cin >> x;
12+
binary_trie<int, 30> bt;
13+
bt.insert(x);
14+
int q;
15+
cin >> q;
16+
while (q--) {
17+
int a, b;
18+
cin >> a >> b;
19+
bt.insert(a);
20+
bt.insert(b);
21+
cout << bt[(bt.size() - 1) / 2] << "\n";
22+
}
23+
}

verify/status.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"title": "Z Algorithm"
2525
},
2626
"verify/atcoder/abc451_g_minimum_xor_walk.cpp": {
27-
"bundled_hash": "sha256:d266f898b580d1197a7cf29a26e4ceb975c7bd33b2c8c50a455e51648d6770d8",
28-
"verified_at": "2026-04-03T16:55:13+09:00",
27+
"bundled_hash": "sha256:2a3f71e3976951bc89e7368b62777c2ee1d6ffbe2f6b4a075b1abb63f9d67628",
28+
"verified_at": "2026-05-26T10:58:36+09:00",
2929
"judge_url": "https://atcoder.jp/contests/abc451/tasks/abc451_g",
3030
"title": "ABC451 G - Minimum XOR Walk"
3131
},
@@ -34,5 +34,11 @@
3434
"verified_at": "2026-05-19T14:54:32+09:00",
3535
"judge_url": "https://atcoder.jp/contests/abc453/tasks/abc453_g",
3636
"title": "ABC453 G - Copy Query"
37+
},
38+
"verify/atcoder/abc458_d_chalkboard_median.cpp": {
39+
"bundled_hash": "sha256:a90e5d31eaf6074a782c025dc6e88e222e73fb9bd9112fac47907117bbc3663c",
40+
"verified_at": "2026-05-26T10:58:14+09:00",
41+
"judge_url": "https://atcoder.jp/contests/abc458/tasks/abc458_d",
42+
"title": "ABC458 D - Chalkboard Median"
3743
}
3844
}

0 commit comments

Comments
 (0)