-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAll_Subarray_Xors.cpp
More file actions
70 lines (62 loc) · 1.61 KB
/
Copy pathAll_Subarray_Xors.cpp
File metadata and controls
70 lines (62 loc) · 1.61 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
#include <bits/stdc++.h>
using namespace std;
const int B = 22;
struct node {
node *child[2];
node() {
child[0] = 0;
child[1] = 0;
}
void insert(int x) {
node *curr = this;
for(int i=B-1;i>=0;i--) {
int xb = (x >> i) & 1;
if(!curr->child[xb]) curr->child[xb] = new node();
curr = curr->child[xb];
}
}
void rec(node *curr, int val, int bit, vector<int> &ans) {
if (!curr) return;
if (bit < 0) {
ans.push_back(val);
return;
}
if (curr->child[0]) rec(curr->child[0], val, bit - 1, ans);
if (curr->child[1]) rec(curr->child[1], val | (1 << bit), bit - 1, ans);
}
void get(vector<int> &ans) {
int res = 0;
rec(this, res, B - 1, ans);
}
};
int32_t main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> v(n);
for(int i=0;i<n;i++) cin >> v[i];
// vector<int> pre = v;
// for(int i=1;i<n;i++) pre[i] ^= pre[i-1];
// vector<int> ans;
// for(int i=0;i<n;i++) {
// for(int j=0;j<=i;j++) {
// int temp = pre[i] ^ pre[j];
// ans.push_back(temp);
// }
// }
// sort(ans.begin(), ans.end());
// ans.erase(unique(ans.begin(), ans.end()), ans.end());
// for(auto it : ans) cout << it << " ";
node *trie = new node();
trie->insert(0);
int pref = 0;
for(int i=0;i<n;i++) {
pref ^= v[i];
trie->insert(pref);
}
vector<int> ans;
trie->get(ans);
for(auto it : ans) cout << it << " ";
return 0;
}