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. Android tips tool 发现的性能问题(转载翻译)

    先翻译刚好在研究到的一段,其余的无限期待续. 1.ObsoleteLayoutParam不起作用的标签 Invalid layout param in a LinearLayout: layout_c ...

  2. Spring学习--使用 utility scheme 定义集合及 p命名空间

    util schema 定义集合: 使用基本的集合标签定义集合时 , 不能将集合作为独立的 Bean 定义 , 导致其他 Bean 无法引用该集合 , 所以无法在不同 Bean 之间共享集合. 可以用 ...

  3. [Evernote]印象笔记使用经验技巧

    一    软件使用      现在使用Windows客户端的印象笔记 + iPhone移动端印象笔记 + chrome浏览器剪藏插件.      在试用了很多云笔记后,还是选择了印象笔记,并且有许多的 ...

  4. 图片上传是否为空,以及类型的js验证

    function check2() { var file = document.getElementsByName("file").value; if(file=="&q ...

  5. Python基础(1)_初识Python

    一.为什么要编程 解放人力:让机器按照人们事先为其编写好的程序自发地去工作 二.什么是编程语言 编程语言就是程序员与计算机之间沟通的介质:程序员把自己想说的话用编程语言写到文件里,这其实就开发了一个程 ...

  6. 【CF1027E】Inverse Coloring(DP)

    题意:给出一个n*n的矩阵,要求在每个位置涂上黑/白色, 要求满足:任意相邻的两行,其颜色要么完全相同,要么完全相反 任意相邻的两列,其颜色也要么相同要么完全相反 且这个矩形中,不存在任意一个大小大于 ...

  7. MySql binlog(理论篇)

    1.什么是binlog? binlog日志用于记录所有更新了数据的sql语句或保存被修改的记录Row: 有了binlog,可以用于实时备份,master/slave主从同步: 在5.0版本前支持文本格 ...

  8. C#后台调用js方法无效果,未解决。

    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<script> ...

  9. Hibernate多对多两种情况

    Hibernate在做多对多映射的时候,除了原先的两张表外,会多出一个中间表做关联,根据中间表的会有两种不同的配置情况: 1.中间表不需要加入额外数据. 2.中间表有其他字段,需记录额外数据. 下面, ...

  10. 《Java编程思想》笔记 第一章 对象导论

    1.抽象过程 Q:什么是对象??? A:   1) 万物皆对象 --- 对象具有状态,行为和标识 2)程序是对象的集合,他们通过发送消息来告诉彼此要做的 3)通过创建包含现有对象的包的方式来创建新类型 ...