forked from rachitiitr/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie-xor.cpp
More file actions
98 lines (93 loc) · 1.71 KB
/
trie-xor.cpp
File metadata and controls
98 lines (93 loc) · 1.71 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
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
//http://codeforces.com/contest/282/submission/21362419
void bin(ll n, vi &a){
vi b;
int i;
while(n){
b.pb(n%2);
n /= 2;
}
a.clear();
while(b.size()!=41) b.pb(0);
fo(i, b.size()){
a.pb(b[b.size()-i-1]);
}
}
struct node{
int val, to;
void init(){
val = to = 0;
}
}trie[N][2];
static int t = 0;
void add(ll x){
vi a;
bin(x, a);
int rt = 0;
int pos = 0;
while(pos<a.size()){
if (trie[rt][a[pos]].val == 0){
trie[rt][a[pos]].to = ++t;
// cout<<"not found "<<a[pos]<<",";
}
// else cout<<" found "<<a[pos]<<",";
trie[rt][a[pos]].val++;
rt = trie[rt][a[pos]].to;
pos++;
}
}
ll get(ll val){
vi a; bin(val, a);
vi res;
int pos = 0, rt = 0;
while(pos<a.size()){
int find = 1-a[pos];
if (trie[rt][find].val != 0){
res.pb(find);
// cout<<"found "<<find<<" "<<"for "<<a[pos]<<endl;
rt = trie[rt][find].to;
}
else{
res.pb(1-find);
rt = trie[rt][1-find].to;
// cout<<"not found "<<find<<" "<<"for "<<a[pos]<<endl;
}
pos++;
}
ll ans = 0;
// cout<<res.size()<<endl;
int i;
fo(i, res.size()){
ans = 2*ans+res[i];
}
return ans;
}
void del(int val){
vi a; bin(val, a);
vi res;
int pos = 0, rt = 0;
while(pos<a.size()){
trie[rt][a[pos]].val--;
rt = trie[rt][a[pos]].to;
pos++;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i,n;
cin>>n;
fo(i, N) trie[i][0].init(), trie[1][0].init();
fo(i, n) cin>>a[i];
add(0);
ll res1 = 0;
ll ans = 0;
ll res = 0;
a[n] = 0;
for(i=n-1; i>=0; i--) res1 ^= a[i];
for(i=n-1; i>=0; i--) ans = max(ans, res1^get(res1)), res1 ^= a[i], res ^= a[i], add(res);
// fo(i, n) res ^= a[i], ans = max(ans, res^get(res)), del(res1), res1 ^= a[i+1];
ans = max(ans, 0LL+get(0));
cout<<ans<<endl;
return 0;
}