求出前缀和, 那么以第x个元素结尾的最大异或值是max(sumx^sump)(1≤p<x), 用trie加速. 后缀同理, 然后扫一遍就OK了.时间复杂度O(31N)
-----------------------------------------------------------------------
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int maxn = 400009;
const int n = 31;
int read() {
char c = getchar(); int ret = 0;
for(; !isdigit(c); c = getchar());
for(; isdigit(c); c = getchar()) ret = ret * 10 + c - ‘0‘;
return ret;
}
int seq[maxn], L[maxn], R[maxn];
int N;
struct Node {
Node* ch[2];
} pool[maxn * n], *pt, *root;
void init() {
memset(pool, 0, sizeof pool);
pt = pool; root = pt++;
}
void Insert(int x) {
Node* t = root;
for(int i = n; i--; ) {
int v = (x >> i) & 1;
if(!t->ch[v]) t->ch[v] = pt++;
t = t->ch[v];
}
}
int Find(int x) {
Node* t = root;
int ret = 0;
for(int i = n; i--; ) {
int v = (((x >> i) & 1) ^ 1);
if(t->ch[v])
ret |= 1 << i, t = t->ch[v];
else
t = t->ch[v ^ 1];
}
return ret;
}
int main() {
N = read();
for(int i = 1; i <= N; i++) seq[i] = read();
int sum = L[0] = 0;
init(); Insert(sum);
for(int i = 1; i <= N; i++) {
sum ^= seq[i];
L[i] = max(L[i - 1], Find(sum));
Insert(sum);
}
sum = R[N + 1];
init(); Insert(sum);
for(int i = N; i; i--) {
sum ^= seq[i];
R[i] = max(R[i + 1], Find(sum));
Insert(sum);
}
int ans = 0;
for(int i = 1; i < N; i++)
ans = max(ans, L[i] + R[i + 1]);
printf("%d\n", ans);
return 0;
}
-----------------------------------------------------------------------
4260: Codechef REBXOR
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 170 Solved: 76
[Submit][Status][Discuss]
Description
Input
输入数据的第一行包含一个整数N,表示数组中的元素个数。
第二行包含N个整数A1,A2,…,AN。
Output
输出一行包含给定表达式可能的最大值。
Sample Input
5
1 2 3 1 2
Sample Output
6
HINT
满足条件的(l1,r1,l2,r2)有:(1,2,3,3),(1,2,4,5),(3,3,4,5)。
对于100%的数据,2 ≤ N ≤ 4*105,0 ≤ Ai ≤ 109。