#10051. 「一本通 2.3 例 3」Nikitosh 和异或

 

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 A1A2, ... , 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 (l1r1l2r2) = (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;
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值