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

Example

Input:
5
1 2 3 1 2 Output:
6

Explanation

Choose (l1r1l2r2) = (1, 2, 3, 3) or (1, 2, 4, 5) or (3, 3, 4, 5).

很久之前做的一道Trie树搞异或的题啦。。。。

大致就是求一下前缀和后缀异或最大值然后更新答案就好了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#define ll int
#define maxn 400005
using namespace std; long long ans=;
ll ci[];
struct Trie{
ll ch[maxn*][];
ll tot,rot; void ins(ll x){
ll now=rot;
for(int i=;i>=;i--){
ll c=(ci[i]&x)?:;
if(!ch[now][c]) ch[now][c]=++tot;
now=ch[now][c];
}
} ll find_max(ll x){
ll now=rot,alr=;
for(int i=;i>=;i--){
ll c=(ci[i]&x)?:;
if(ch[now][c^]) alr+=ci[i],now=ch[now][c^];
else now=ch[now][c];
}
return alr;
}
}tr; ll a[maxn],w[maxn],n;
ll lef[maxn],rig[maxn]; inline void init(){
ci[]=;
for(int i=;i<=;i++) ci[i]=ci[i-]<<;
tr.rot=tr.tot=;
} int main(){
init(); scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",a+i),w[i]=a[i]^w[i-]; tr.ins();
for(int i=;i<=n;i++){
lef[i]=max(lef[i-],tr.find_max(w[i]));
tr.ins(w[i]);
} memset(tr.ch,,sizeof(tr.ch));
init(); for(int i=n;i;i--){
rig[i]=max(rig[i+],tr.find_max(w[i]));
tr.ins(w[i]);
} for(int i=;i<=n;i++) ans=max(ans,(long long)(lef[i]+rig[i])); printf("%lld\n",ans);
return ;
}

Codechef REBXOR的更多相关文章

  1. 【BZOJ4260】 Codechef REBXOR 可持久化Trie

    看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...

  2. BZOJ 4260: Codechef REBXOR( trie )

    求出前缀和, 那么以第x个元素结尾的最大异或值是max(sumx^sump)(1≤p<x), 用trie加速. 后缀同理, 然后扫一遍就OK了.时间复杂度O(31N) ------------- ...

  3. bzoj 4260: Codechef REBXOR (01 Trie)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4260 题面: 4260: Codechef REBXOR Time Limit: 10 S ...

  4. 【BZOJ4260】Codechef REBXOR (Trie树)

    [BZOJ4260]Codechef REBXOR (Trie树) 题面 BZOJ 题解 两眼题.第一眼不会做,第二眼好简单... 前缀异或和一下,拿\(Trie\)树维护求一个在这个端点以左的最大值 ...

  5. 【BZOJ】4260: Codechef REBXOR【Trie树】【前后缀异或最大】

    4260: Codechef REBXOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2218  Solved: 962[Submit][Stat ...

  6. 【BZOJ4260】Codechef REBXOR Trie树+贪心

    [BZOJ4260]Codechef REBXOR Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output ...

  7. [Bzoj4260]Codechef REBXOR(trie树)

    4260: Codechef REBXOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1534  Solved: 669[Submit][Stat ...

  8. BZOJ4260 Codechef REBXOR 题解

    题目大意: 有一个长度为n的序列,求1≤l1≤r1<l2≤r2≤n使得(⊕r1i=l1ai)+(⊕r2i=l2ai)最大,输出这个最大值. 思路: 用Trie求出前缀异或和以及后缀异或和,再求出 ...

  9. BZOJ4260: Codechef REBXOR

    Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN.     Output 输出一行包含给定表达式可能的最大值.   S ...

  10. BZOJ 4260 Codechef REBXOR

    Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. Sample ...

随机推荐

  1. windows 系统下git 的使用

    前言: 最新版本的git for windows也是有界面的,不再是以前的纯命令行操作,但是我习惯了乌龟,所以感觉还是直接用乌龟比较方便点~~ 前提,已安装以下: git for windows,未安 ...

  2. Xamarin+vs2010部署错误:error MSB6004: 指定的任务可执行文件位置\sdk\\tools\zipalign.exe”无效

    好不容易配好了Xamarin和vs2010,也搞好了GenyMotion的虚拟机配置,开始调试的时候又报出了这样的错误: error MSB6004: 指定的任务可执行文件位置"C:\Use ...

  3. Go 实现 soundex 算法

    [转]http://www.syyong.com/Go/Go-implements-the-soundex-algorithm.html SOUNDEX 返回由四个字符组成的代码 (SOUNDEX) ...

  4. Python学习笔记 - day1 - 概述及安装

    Python概述 Python是一种计算机程序设计语言.我们平时已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合网页编程的JavaScript语言等等. Python ...

  5. Golang在视频直播平台的高性能实践(含PPT下载)

    熊猫 TV 是一家视频直播平台,先介绍下我们系统运行的环境,下面这 6 大服务只是我们几十个服务中的一部分,由于并发量与重要性比较高,所以成为 golang 小试牛刀的首批高性能高并发服务. 把大服务 ...

  6. I2C和SPI总线对比【转】

    转自:http://blog.csdn.net/skyflying2012/article/details/8237881/ 最近2周一直在调试IIC和SPI总线设备,这里记录一下2种总线,以备后忘. ...

  7. linux环境下的GUN make学习笔记(一)

    第一章:概述 1.1:make概述 在linux环境下使用make工具能够比较容易的构建一个属于自己的工程,整个工程的编译只需要一个命令就可以完成编译.连接以至于最后的执行.不过我们需要投入一些时间去 ...

  8. 慎用Java递归调用

    总结:慎用Java递归调用,测试时可以尝试该方法,否则尽量不要使用递归!递归过多调用时,最好改为for或者whlie来代替. 在java语言中,使用递归调用时,如果过多的调用容易造成java.lang ...

  9. tarjan算法和Kosaraju算法

    tarjan算法和Kosaraju算法是求有向图的强连通分量的算法: #include<iostream> #include<cstring> using namespace ...

  10. webupload在IE9-出现的问题解决

    1,点击选择按钮无反应 原因:IE9调用的是flash上传,Upload.swf路径错误!!导致 解决:检查Upload.swf路径是否准确!!!!直接改成cdn地址,是最直接有效的办法. 2,IE9 ...