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. 【BZOJ1458】士兵占领 最大流的模板题

    我们只要把他们可以有的限制用流量限制,再用两者关系限制一下就可以开心的跑了. #include <cstdio> #include <cstring> #include < ...

  2. 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)

    P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...

  3. [lucene系列笔记2]在eclipse里初步使用lucene的索引和查询功能

    首先,new一个java project,名字叫做LuceneTools. 然后,在project里new一个class,名字叫做IndexFiles.这个类用来给文件建索引(建好索引以后就可以高效检 ...

  4. ES6学习笔记(四)—— async 函数

    await 是 async wait 的简写, 是 generator 函数的语法糖. async 函数的特点: async 声明一个方法是异步的,await 则等待这个异步方法执行的完成 async ...

  5. shelve,xml,re模块

    一.shelve模块 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型 import shelve ...

  6. linux system函数分析

    system函数是在应用编程里面想调用外部命令时最方便的方式了,除非想要获取命令行执行的输出信息, 那system就不行了,需要用popen.但是system内部具体怎么实现及怎么处理它的返回值经常被 ...

  7. Linux设置编译器环境变量

    Linux设置编译器环境变量 https://jingyan.baidu.com/article/9f7e7ec0bb22aa6f29155453.html Linux添加环境变量与GCC编译器添加I ...

  8. Linux下mysql命令 导入 导出sql文件

    导出数据库 直接使用命令: mysqldump -u root -p abc >abc.sql 然后回车输入密码就可以了: mysqldump -u 数据库链接用户名 -p  目标数据库 > ...

  9. 表单文件上传,ajax文件上传

    原创链接:http://www.cnblogs.com/yanqin/p/5345562.html html代码  index.jsp(表单文件上传) <form action="sh ...

  10. 关于oracle的连接时ORA-12519错误的解决方案

    系统在运行时出现了ORA-12519的错误,无法连接数据库,后来在网上找了下,找到了如下的解决方法,共享下. OERR: ORA-12519 TNS:no appropriate service ha ...