[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4260
[算法]
字典树
时间复杂度 : O(NlogN)
[代码]
#include<bits/stdc++.h> using namespace std; const int MAXN = 4e5 + 10; int n,ans; int a[MAXN],sufmx[MAXN],premx[MAXN],pre[MAXN],suf[MAXN]; template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } struct Trie { int tot; int child[MAXN * 31][2]; inline void clear() { tot = 0; memset(child,0,sizeof(child)); } inline void insert(int x) { int now = 0; for (int i = 0; i < 31; i++) { int tmp; if (x & (1 << (30 - i))) tmp = 1; else tmp = 0; if (!child[now][tmp]) child[now][tmp] = ++tot; now = child[now][tmp]; } } inline int query(int x) { int now = 0 , ret = 0; for (int i = 0; i < 31; i++) { int tmp; if (x & (1 << (30 - i))) tmp = 1; else tmp = 0; if (child[now][tmp ^ 1]) { ret += 1 << (30 - i); now = child[now][tmp ^ 1]; } else now = child[now][tmp]; } return ret; } } T; int main() { read(n); for (int i = 1; i <= n; i++) read(a[i]); T.clear(); T.insert(0); for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] ^ a[i]; premx[i] = T.query(pre[i]); T.insert(pre[i]); } for (int i = 1; i <= n; i++) premx[i] = max(premx[i - 1],premx[i]); T.clear(); T.insert(0); for (int i = n; i >= 1; i--) { suf[i] = suf[i + 1] ^ a[i]; sufmx[i] = T.query(suf[i]); T.insert(suf[i]); } for (int i = n; i >= 1; i--) sufmx[i] = max(sufmx[i],sufmx[i + 1]); for (int i = 1; i <= n; i++) ans = max(ans,premx[i] + sufmx[i + 1]); printf("%d ",ans); return 0; }