-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie - XOR Sum Min & Max.cpp
99 lines (88 loc) · 1.75 KB
/
Trie - XOR Sum Min & Max.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
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
#include<bits/stdc++.h>
using namespace std;
int node[50005*32][3], node_no;
void init()
{
node_no = 0;
for(int i = 0; i < 3; ++i)
node[0][i] = -1;
}
void insert(int mask)
{
int now = 0, next;
for(int i = 31; i >= 0; --i)
{
next = (mask >> i) & 1;
if(node[now][next] == -1)
{
node[now][next] = ++node_no;
for(int j = 0; j < 3; ++j)
node[node_no][j] = -1;
}
now = node[now][next];
}
}
int minn(int mask)
{
int now = 0, ans = 0;
for(int i = 31; i >= 0; --i)
{
int next = (mask >> i) & 1;
if(node[now][next] != -1)
{
ans <<= 1;
now = node[now][next];
}
else
{
ans <<= 1;
++ans;
now = node[now][next ^ 1];
}
}
return ans;
}
int maxx(int mask)
{
int now = 0, ans = 0;
for(int i = 31; i >= 0; --i)
{
int next = ((mask >> i) & 1) ^ 1;
if (node[now][next] != -1)
{
ans <<= 1;
++ans;
now = node[now][next];
}
else
{
ans <<= 1;
now = node[now][next ^ 1];
}
}
return ans;
}
int main()
{
int t, n, x, mx, mn, temp;
scanf("%d", &t);
for(int tc = 1; tc <= t; ++tc)
{
init();
insert(0);
scanf("%d %d", &n, &x);
mx = x;
mn = x;
temp = x;
for(int i = 1; i < n; ++i)
{
scanf("%d", &x);
temp ^= x;
mx = max(mx, maxx(temp));
mn = min(mn, minn(temp));
insert(temp);
}
printf("Case %d: %d %d\n", tc, mx, mn);
}
return 0;
}