-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathMaximum Xor Subarray.cpp
55 lines (49 loc) · 1.04 KB
/
Maximum Xor Subarray.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
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node *c[2];
};
int N, xum, best;
Node *root;
void update(int x){
Node *cur = root;
for(int i = 30; i >= 0; i--){
if(x&(1<<i)){
if(!cur->c[1]) cur->c[1] = new Node();
cur = cur->c[1];
} else {
if(!cur->c[0]) cur->c[0] = new Node();
cur = cur->c[0];
}
}
}
int query(int x){
int res = 0;
Node *cur = root;
for(int i = 30; i >= 0; i--){
if(x&(1<<i)){
if(cur->c[0]){
res += (1<<i);
cur = cur->c[0];
} else cur = cur->c[1];
} else {
if(cur->c[1]){
res += (1<<i);
cur = cur->c[1];
} else cur = cur->c[0];
}
}
return res;
}
int main(){
scanf("%d", &N);
root = new Node();
update(0);
for(int i = 0, x; i < N; i++){
scanf("%d", &x);
xum ^= x;
update(xum);
best = max(best, query(xum));
}
printf("%d\n", best);
}