-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum XOR Submatrix using Trie.cpp
98 lines (79 loc) · 1.79 KB
/
Maximum XOR Submatrix using Trie.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
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using llint = long long;
using ullint = unsigned long long;
using pii = pair <int,int>;
using pcc = pair <char,char>;
using pss = pair <string,string>;
using vi = vector<int>;
using vii = vi::iterator;
#define INF (1<<30)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define all(c) c.begin(),c.end()
#define ms(name,val) memset(name, val, sizeof name)
#define np nullptr
int nxt;
int A[10001][21];
int B[10001];
int T[10001*32][2];
// static binary trie
void Insert(int x)
{
int root = 1; // can be set to 0, but then we have to set nxt to 1
for (int i = 27; i > -1; --i)
{
bool b = (x&(1<<i));
if (!T[root][b]) T[root][b] = nxt++;
root = T[root][b];
}
}
int Query(int &x)
{
int ans = 0;
int root = 1; // can be set to 0, but then we have to set nxt to 1
for (int i = 27; i > -1; --i)
{
bool b = (x&(1<<i));
if (T[root][b^1])
{
ans |= (1<<i);
root = T[root][b^1];
}
else root = T[root][b];
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);
int n, m, ans = 0;
cin >> n >> m;
for (int t1 = 1; t1 <= n; ++t1)
{
for (int t2 = 1; t2 <= m; ++t2)
{
cin >> A[t1][t2];
A[t1][t2] ^= A[t1][t2-1];
}
}
for (int L = 1; L <= m; ++L)
{
for (int R = L; R <= m; ++R)
{
memset(T, 0, sizeof T);
nxt = 2;
Insert(0);
for (int t1 = 1; t1 <= n; ++t1)
{
B[t1] = A[t1][R]^A[t1][L-1]^B[t1-1];
ans = max(ans,Query(B[t1]));
Insert(B[t1]);
}
}
}
cout << ans << '\n';
return 0;
}