forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.cpp
63 lines (55 loc) · 1.27 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
typedef struct data
{
data* bit[2];
int cnt=0;
}trie;
trie* head;
void insert(int x)
{
trie* node = head;
for(int i=30;i>=0;i--)
{
int curbit=(x>>i)&1;
if(node->bit[curbit]==NULL)
{
node->bit[curbit]=new trie();
}
node=node->bit[curbit];
node->cnt++;
}
}
void remove(int x)
{
trie* node = head;
for(int i=30;i>=0;i--)
{
int curbit=(x>>i)&1;
node=node->bit[curbit];
node->cnt--;
}
}
int maxxor(int x)
{
trie* node = head;
int ans=0;
for(int i=30;i>=0;i--)
{
int curbit=(x>>i)&1;
if(node->bit[curbit^1]!=NULL && node->bit[curbit^1]->cnt>0)
{
ans+=(1LL<<i);
node=node->bit[curbit^1];
}
else
node=node->bit[curbit];
}
return ans;
}
//Sample Problem 1: http://codeforces.com/contest/706/problem/D
//Sample Solution 1: http://codeforces.com/contest/706/submission/39515647 (Maxxor)
//Sample Problem 2: http://codeforces.com/problemset/problem/948/D
//Sample Solution 2: http://codeforces.com/contest/948/submission/39663985 (Minxor)
//Sample Problem 3: http://codeforces.com/contest/665/problem/E
//Sample Solution 3: http://codeforces.com/contest/665/submission/39664021 (Subarray Count)
//Sample Problem 4: http://codeforces.com/contest/282/problem/E
//Sample Solution 4: http://codeforces.com/contest/282/submission/39664030 (Maxxor)