-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum Xor Subarray.cpp
More file actions
executable file
·68 lines (64 loc) · 1.09 KB
/
Copy pathMaximum Xor Subarray.cpp
File metadata and controls
executable file
·68 lines (64 loc) · 1.09 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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
class Trie {
public:
Trie() {
for (int i = 0; i < 2; i++) v[i] = NULL;
end = false;
}
~Trie() {
for (int i = 0; i < 2; i++) {
if (v[i] != NULL) delete v[i];
}
}
void insert(int x) {
Trie *t = this;
for (int i = 30; i >= 0; i--) {
int pos = x & (1 << i) ? 1 : 0;
if (t->v[pos] == NULL) {
t->v[pos] = new Trie;
}
t = t->v[pos];
}
t->end = true;
}
int query(int x) {
int res = 0;
Trie *t = this;
for (int i = 30; i >= 0; i--) {
int pos = x & (1 << i) ? 1 : 0;
if (t->v[pos] != NULL) {
res |= x & (1 << i);
t = t->v[pos];
} else {
res |= (x & (1 << i)) ^ (1 << i);
t = t->v[pos ^ 1];
}
}
return res;
}
private:
Trie *v[2];
bool end;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (auto &x : v) cin >> x;
int XOR = 0;
int res = 0;
Trie trie;
trie.insert(0);
for (auto x : v) {
XOR ^= x;
res = max(res, trie.query(~XOR) ^ XOR);
trie.insert(XOR);
}
cout << res << "\n";
return 0;
}