Skip to content

Commit f0c9133

Browse files
committed
nits to binary trie
1 parent 5b2c00a commit f0c9133

File tree

5 files changed

+53
-67
lines changed

5 files changed

+53
-67
lines changed
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+
//! @endco
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
}

0 commit comments

Comments
 (0)