-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor_trie.cpp
50 lines (43 loc) · 984 Bytes
/
xor_trie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
template <typename T=ll> struct XorTrie {
struct Node {
T value;
array<shared_ptr<Node>, 2> children;
Node() : value(), children() {
for (auto &x : children)
x = shared_ptr<Node>(nullptr);
}
};
shared_ptr<Node> root;
XorTrie() : root(make_shared<Node>()) {}
static constexpr int tsize() {
if constexpr (is_same_v<int, T>) {
return 31;
} else {
return 63;
}
}
void insert(T x) {
auto cur = root;
for (int i = tsize(); ~i; --i) {
auto &child = cur->children[x>>i&1];
if (!child)
child = make_shared<Node>();
cur = child;
}
cur->value = x;
}
T query(T x) const {
T res = 0;
auto cur = root;
for (int i = tsize(); ~i; --i) {
int idx = x>>i&1;
if (cur->children[idx^1]) {
cur = cur->children[idx^1];
res |= (T)1 << i;
} else if (cur->children[idx]) {
cur = cur->children[idx];
}
}
return res;
}
};