BZOJ 4260 Codechef REBXOR(字典树)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=4260
【题目大意】
给出一个数列,请找出两段连续且不相交的数段,使得其分别异或和的和最大。
【题解】
在字典树上进行贪心我们可以得到当前数与已经插入的数的最大异或值,
我们将插入值转为前缀和,即可得到固定区间右端点的区段异或最大值。
同理我们可以计算后缀和的字典树,得到固定区间左端的区段异或最大值,
枚举断点,用前缀最大值与后缀最大值的和更新答案即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=12000000;
int xorsum,n,tot,son[N][2],a[N],l[N],r[N];
void insert(int x){
for(int u=0,i=1<<30;i;i>>=1){
bool w=(x&i);
if(!son[u][w])son[u][w]=++tot;
u=son[u][w];
}
}
int query(int x){
int res=0;
for(int u=0,i=1<<30;i;i>>=1){
bool w=x&i;
if(son[u][w^1])res+=i,u=son[u][w^1];
else u=son[u][w];
}return res;
}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
memset(son,0,sizeof(son)); insert(tot=l[0]=xorsum=0);
for(int i=1;i<=n;i++){
insert(xorsum^=a[i]);
l[i]=max(query(xorsum),l[i-1]);
}memset(son,0,sizeof(son)); insert(tot=r[n+1]=xorsum=0);
for(int i=n;i;i--){
insert(xorsum^=a[i]);
r[i]=max(query(xorsum),r[i+1]);
}int ans=0;
for(int i=0;i<=n;i++)ans=max(ans,l[i]+r[i+1]);
printf("%d\n",ans);
}return 0;
}
BZOJ 4260 Codechef REBXOR(字典树)的更多相关文章
- bzoj 4260 Codechef REBXOR——trie树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4260 一段的异或和就是两个前缀的异或和.正反扫两边,用trie树算每个位置为左/右端点时最大 ...
- BZOJ 4260: Codechef REBXOR (trie树维护异或最大值)
题意 分析 将区间异或和转化为前缀异或和.那么[L,R][L,R][L,R]的异或和就等于presum[R] xor presum[L−1]presum[R]\ xor \ presum[L-1]pr ...
- 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 ...
- BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)
<题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...
- BZOJ 4260 Codechef REBXOR
Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. Sample ...
- BZOJ 4260 Codechef REBXOR 01trie
好题...开阔思路 把每个前缀异或和依次插入$01trie$,插之前找一个最优的(就是从高位向低位贪心,尽量走相反方向)看看能不能更新答案,此时相当于找到了区间右端点不超过某个点$r$的最大或和$f[ ...
- 【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 ...
随机推荐
- Go从入门到精通(持续更新)
1.0 搭建环境 由于我们 Go官方网站 在我大天朝被和谐了,所以我们只能去 Go语言中文网 来下载了.Go的安装很简单,不像Java还要配置一大堆的东西,选择自己系统的对应版本,下载安装,像安装QQ ...
- C题 hdu 1408 盐水的故事
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1408 盐水的故事 Time Limit: 2000/1000 MS (Java/Others) ...
- AndroidStudio创建jinLibs文件夹
在文件中的buildTypes节点下添加 sourceSets.main { jniLibs.srcDir 'libs' } 如图
- response.getWriter().write()和 response.getWriter().print()的区别
异步上传图片的代码.发现里面用了response.getWriter().print(),故联想到response.getWriter().writer(),经过一番api的查找与实操,总结如下: r ...
- python实战===itchat
import itchat itchat.login() friends=itchat.get_friends(update=True)[0:] male=female=other=0 for i i ...
- Linux内核基础--事件通知链(notifier chain)【转】
转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...
- 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...
- Java Redis 连接池 Jedis 工具类
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import re ...
- 学习1:python输入输出
1. 输出 >>> print "hello world" hello world >>> print 'hello world' hello ...
- SQL中判断值是否为NULL
在 SQL 中,我们如果在操作数据库时使用 WHERE 子句判断一个列的值是否为 NULL,我们不能够使用 column_name=null 来进行判断,这是不正确的,我们应该使用 is null 来 ...