-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie
128 lines (114 loc) · 2.18 KB
/
Trie
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Code is tested on https://codeforces.com/problemset/problem/842/D
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define sz 200009
#define pf printf
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define sf scanf
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define pb push_back
#define mp make_pair
#define ll long long
#define ff first
#define ss second
#define inf 1e18
#define INF (1LL<<62)
#define pii pair<int,int>
#define sorted(s) sort(s.begin(),s.end())
#define faster() ios_base::sync_with_stdio(false);cin.tie(NULL)
int arr[1<<19];
class trie
{
public:
trie *one,*zero;
int fn;
trie()
{
fn=0;
one =zero=NULL;
}
~trie()
{
delete one;
delete zero;
}
}*root;
void add(trie *cur,int val)
{
int i,ck;
for(i=19; i>=0; i--)
{
ck=(1<<i&val);
if(ck==0)
{
if(cur->zero==NULL)
cur->zero=new trie();
cur=cur->zero;
cur->fn++;
}
else
{
if(cur->one==NULL)
cur->one=new trie();
cur=cur->one;
cur->fn++;
}
}
}
int query(trie *cur,int val)
{
int i,ans=0,a,b,cp=0;
for(i=19; i>=0; i--)
{
a=(1<<i&val);
if(a==0)
{
if(cur->zero!=NULL)
cur=cur->zero;
else
{
ans+=(1<<i);
cur=cur->one;
}
}
else
{
if(cur->one!=NULL)
cur=cur->one;
else
{
ans+=(1<<i);
cur=cur->zero;
}
}
}
return ans;
}
int main()
{
int i,n,q,a,ans=0,axor=0;
si(n);
si(q);
root=new trie();
for(i=0; i<n; i++)
{
si(a);
arr[a]=1;
}
for(i=0; i<(1<<19); i++)
{
if(!arr[i])
add(root,i);
}
while(q--)
{
si(a);
axor^=a;
ans=query(root,axor);
pi(ans);
}
return 0;
}