Skip to content

Commit cdc9582

Browse files
lrvideckisweb-flow
andauthored
nits to binary trie (#156)
* nits to binary trie * [auto-verifier] verify commit f0c9133 * fix * fix --------- Co-authored-by: GitHub <[email protected]>
1 parent 5b2c00a commit cdc9582

File tree

7 files changed

+55
-69
lines changed

7 files changed

+55
-69
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"tests/library_checker_aizu_tests/convolution/lcm_convolution.test.cpp": "2025-06-25 15:06:18 -0600",
44
"tests/library_checker_aizu_tests/convolution/min_plus_convolution.test.cpp": "2024-11-17 14:04:03 -0600",
55
"tests/library_checker_aizu_tests/data_structures/binary_search_example.test.cpp": "2024-11-18 10:51:39 -0600",
6-
"tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp": "2024-11-17 14:04:03 -0600",
6+
"tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp": "2025-08-06 18:38:52 -0600",
77
"tests/library_checker_aizu_tests/data_structures/bit.test.cpp": "2024-12-14 19:50:29 -0600",
88
"tests/library_checker_aizu_tests/data_structures/bit_inc.test.cpp": "2024-11-18 09:44:22 -0600",
99
"tests/library_checker_aizu_tests/data_structures/bit_ordered_set.test.cpp": "2024-12-15 14:34:10 -0600",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
//! @code
3+
//! binary_trie bt;
4+
//! bt.update(num, 1); // insert
5+
//! bt.update(num, -1); // erase
6+
//! @endcode
7+
//! @time O(q * mx_bit)
8+
//! @space O(q * mx_bit)
9+
const ll mx_bit = 1LL << 60;
10+
struct binary_trie {
11+
struct node {
12+
int siz = 0;
13+
array<int, 2> next = {-1, -1};
14+
};
15+
deque<node> t;
16+
binary_trie(): t(1) {}
17+
void update(ll num, int delta) {
18+
int v = 0;
19+
for (ll bit = mx_bit; bit; bit /= 2) {
20+
bool b = num & bit;
21+
if (t[v].next[b] == -1) {
22+
t[v].next[b] = sz(t);
23+
t.emplace_back();
24+
}
25+
v = t[v].next[b];
26+
t[v].siz += delta;
27+
}
28+
}
29+
ll walk(ll num) {
30+
int v = 0;
31+
ll res = 0;
32+
for (ll bit = mx_bit; bit; bit /= 2) {
33+
bool b = num & bit;
34+
int u = t[v].next[b];
35+
if (u != -1 && t[u].siz > 0) v = u, res |= num & bit;
36+
else v = t[v].next[!b], res |= (~num) & bit;
37+
}
38+
return res;
39+
}
40+
};

library/data_structures/binary_trie/binary_trie.hpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

library/data_structures/binary_trie/count.hpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

library/data_structures/binary_trie/walk.hpp

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/set_xor_min"
33
#include "../template.hpp"
4-
#include "../../../library/data_structures/binary_trie/binary_trie.hpp"
4+
#include "../../../library/data_structures/binary_trie.hpp"
55
int main() {
66
cin.tie(0)->sync_with_stdio(0);
77
int q;
88
cin >> q;
99
{
10-
binary_trie<int64_t> bt_ll;
11-
assert(bt_ll.mx_bit == 62);
12-
bt_ll.update(61238612412983LL, 5);
13-
int cnt = bt_ll.count(61238612412983LL);
14-
assert(cnt == 5);
15-
cnt = bt_ll.count(54289162783746217LL);
16-
assert(cnt == 0);
17-
int64_t res = bt_ll.walk(54289162783746217LL);
18-
assert(res == 61238612412983LL);
10+
binary_trie bt;
11+
bt.update(61238612412983LL, 5);
12+
assert(
13+
bt.walk(54289162783746217LL) == 61238612412983LL);
1914
}
20-
binary_trie<int> bt_int;
21-
assert(bt_int.mx_bit == 30);
15+
binary_trie bt;
16+
bt.update(0, 0);
2217
while (q--) {
2318
int type, x;
2419
cin >> type >> x;
2520
if (type == 0) {
26-
if (bt_int.count(x) == 0) bt_int.update(x, 1);
21+
if (
22+
bt.t[bt.t[0].next[0]].siz == 0 || bt.walk(x) != x)
23+
bt.update(x, 1);
2724
} else if (type == 1) {
28-
if (bt_int.count(x) == 1) bt_int.update(x, -1);
25+
if (bt.t[bt.t[0].next[0]].siz > 0 && bt.walk(x) == x)
26+
bt.update(x, -1);
2927
} else {
3028
assert(type == 2);
31-
int val = bt_int.walk(x);
29+
int val = bt.walk(x);
3230
cout << (val ^ x) << '\n';
3331
}
3432
}

tests/scripts/compile_commented_snippets.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ git submodule update
2323
echo "vector<mint> rhs;"
2424
echo "vector<vector<mint>> mat;"
2525
echo "vector<vector<bool>> grid;"
26-
echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,lsz,rsz,cols,cap;"
26+
echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,lsz,rsz,cols,cap,num;"
2727
} >entire_library_without_main
2828

2929
{

0 commit comments

Comments
 (0)