Problem
Read problems statements in Mandarin and Russian. Translations in Vietnamese to be uploaded soon.
Nikitosh the painter has a 1-indexed array A of N elements. He wants to find the maximum value of expression
(A[l1] ⊕ A[l1 + 1] ⊕ ... ⊕ A[r1]) + (A[l2] ⊕ A[l2 + 1] ⊕ ... ⊕ A[r2]) where 1 ≤ l1 ≤ r1 < l2 ≤ r2 ≤ N.
Here, x ⊕ y means the bitwise XOR of x and y.
Because Nikitosh is a painter and not a mathematician, you need to help him in this task.
Input
The first line contains one integer N, denoting the number of elements in the array.
The second line contains N space-separated integers, denoting A1, A2, ... , AN.
Output
Output a single integer denoting the maximum possible value of the given expression.
Constraints
- 2 ≤ N ≤ 4*105
- 0 ≤ Ai ≤ 109
Subtasks
- Subtask 1 (40 points) : 2 ≤ N ≤ 104
- Subtask 2 (60 points) : Original constraints
Sample 1:
Input
Output
5 1 2 3 1 2
6
Explanation:
Choose (l1, r1, l2, r2) = (1, 2, 3, 3) or (1, 2, 4, 5) or (3, 3, 4, 5).
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,z,tot,maxx,ww;
int a[410000*32][2],b[410000*32],c[410000],d[410000];
int e[410000];
void gg(int x)
{
int u,t=0;
for(int i=30;i>=0;i--)
{
u=(x>>i)&1; //二进制输出x
if(!a[t][u])
a[t][u]=++tot; //标记
t=a[t][u];
}
b[t]=x;
}
int qq(int x)
{
int u,v,t=0,ans=0;
for(int i=30;i>=0;i--)
{
u=(x>>i)&1;;
if(a[t][!u]) //二进制中判断每一位是否为1,若为0取反置1;
t=a[t][!u];
else
t=a[t][u];
}
return b[t];
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>c[i],d[i]=d[i-1]^c[i],gg(d[i]);
for(int i=n;i>=1;i--)
e[i]=e[i+1]^c[i],gg(e[i]);
for(int i=1;i<=n;i++)
{
maxx=max(maxx,qq(d[i])^d[i]);
z=max(z,qq(e[n+1-i])^e[n+1-i]);
ww=max(ww,z+maxx);
}
cout<<ww<<endl;
}