-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitsetTrie.cpp
More file actions
68 lines (64 loc) · 1.58 KB
/
Copy pathBitsetTrie.cpp
File metadata and controls
68 lines (64 loc) · 1.58 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Author: Kevin Li
* Lang: C++
* Description: Trie data structure with bits instead of regular characters
*/
#include <iostream>
using namespace std;
struct bitset_trie {
#define MAXN 1e7
#define INF 1e9
#define BIT 62
typedef long long ll;
int **t, s = 0;
int *sz;
int n;
bitset_trie (int _n) : n(_n) {
t = new int*[n];
sz = new int[n];
for (int i = 0; i < n; i++) {
t[i] = new int[2]; t[i][0] = t[i][1] = 0; sz[i] = 0;
}
}
void init() {
for (int i = 0; i < n; i++) {
t[i][0] = t[i][1] = 0;
}
}
void insert(ll x, int m) {
int c = 0;
sz[c] += m;
for (int i = 0; i < BIT; i++) {
int nxt = (x & (1LL << i)) >> i;
if (!t[c][nxt]) {
s++;
t[c][nxt] = s;
}
c = t[c][nxt];
sz[c] += m;
}
}
// given x, computes maximum xor of x w/ any element in the trie
// larger bits are more significant and located closer to root
// so by the way xor works we always try to get a bit that
// doesn't correspond to the bit in x
ll max_xor(ll x) {
if (sz[0] == 0) {
return -INF;
}
int c = 0;
for (int i = 0; i < BIT; i++) {
int nxt = ((x & (1LL << i)) >> i) ^ 1;
if (!t[c][nxt] || !sz[t[c][nxt]]) {
nxt ^= 1;
}
c = t[c][nxt];
if (nxt) {
x ^= (1LL << i);
}
}
return x;
}
};
int main() {
}