Codechef REBXOR
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
Example
Input:
5
1 2 3 1 2 Output:
6
Explanation
Choose (l1, r1, l2, r2) = (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的更多相关文章
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
- BZOJ 4260: Codechef REBXOR( trie )
求出前缀和, 那么以第x个元素结尾的最大异或值是max(sumx^sump)(1≤p<x), 用trie加速. 后缀同理, 然后扫一遍就OK了.时间复杂度O(31N) ------------- ...
- bzoj 4260: Codechef REBXOR (01 Trie)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4260 题面: 4260: Codechef REBXOR Time Limit: 10 S ...
- 【BZOJ4260】Codechef REBXOR (Trie树)
[BZOJ4260]Codechef REBXOR (Trie树) 题面 BZOJ 题解 两眼题.第一眼不会做,第二眼好简单... 前缀异或和一下,拿\(Trie\)树维护求一个在这个端点以左的最大值 ...
- 【BZOJ】4260: Codechef REBXOR【Trie树】【前后缀异或最大】
4260: Codechef REBXOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2218 Solved: 962[Submit][Stat ...
- 【BZOJ4260】Codechef REBXOR Trie树+贪心
[BZOJ4260]Codechef REBXOR Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output ...
- [Bzoj4260]Codechef REBXOR(trie树)
4260: Codechef REBXOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1534 Solved: 669[Submit][Stat ...
- BZOJ4260 Codechef REBXOR 题解
题目大意: 有一个长度为n的序列,求1≤l1≤r1<l2≤r2≤n使得(⊕r1i=l1ai)+(⊕r2i=l2ai)最大,输出这个最大值. 思路: 用Trie求出前缀异或和以及后缀异或和,再求出 ...
- BZOJ4260: Codechef REBXOR
Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. S ...
- BZOJ 4260 Codechef REBXOR
Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. Sample ...
随机推荐
- CentOS下SVN服务器的搭建使用
转载自:http://ailurus.blog.51cto.com/4814469/1168481 SVN作为新一代代码版本管理工具,有很多优点,管理方便,逻辑明确,安全性高,代码一致性高.SVN数据 ...
- java.sql.Date和java.util.Date的不同和相互转换方式
一:前言 这是我在新的公司写的第一份博客吧,来了又一个星期了吧,但是在来的那几天我真的很迷茫的感觉这里是很不适合我的样子,而且我又是来实习的,我很不愿意啊,自己做的又是java web,最原始的ser ...
- 【CF1027D】Mouse Hunt(拓扑排序,环)
题意:给定n个房间,有一只老鼠可能从其中的任意一个出现, 在第i个房间设置捕鼠夹的代价是a[i],若老鼠当前在i号房间则下一秒会移动到b[i]号, 问一定能抓住老鼠的最小的总代价 n<=2e5, ...
- [POJ1423]Stirling公式的应用
Stirling公式: n!约等于sqrt(2*pi*n)*(n/e)^n 另外,e约等于2.71828182845409523... 试了一下发现math库里面并不能像pi一样直接调e但是发现挺好记 ...
- 【比赛】百度之星2017 初赛Round B
第一题 题意:给定n*m网络,定义两个棋子在同行同列则相互攻击,同时要求两个棋子的行和列不能一小一大,求满足条件的最大摆放的方案数. 题解:ans=C(max(n,m),min(n,m)),就是在ma ...
- 关于UML
http://www.cnblogs.com/zfc2201/archive/2011/08/16/2141433.html
- 会议中心[APIO2009]
会议中心 思路 这一题的正解是倍增的,但是由于我太蒟蒻了,所以我选了一个不算正解但是有可以拿满分的题目学习 思路和我考场上其实差不多,只是我无法实现.... 下面我先来介绍几个数组的用途 1.s,s数 ...
- Django-models class Meta:元类
Django模型之Meta选项详解 Model 元数据就是 "不是一个字段的任何数据" -- 比如排序选项, admin 选项等等. Django模型类的Meta是一个内部类, ...
- ARM-Linux (临时,正式) 建立页表的比较【转】
转自:http://blog.csdn.net/edwardlulinux/article/details/38967521 版权声明:本文为博主原创文章,未经博主允许不得转载. 很久没有写博客了 ...
- springboot 404返回自定义json(只进入过滤器)
今天在公司没事干,记一次springboot遇到的一些坑,在百度上也没有搜到类似的问题和答案(或者说 答案不是我想要的) 当我们在SpringBoot遇到了404或者500的错误的时候,你们会怎么办? ...