【题目链接】  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(字典树)的更多相关文章

  1. bzoj 4260 Codechef REBXOR——trie树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4260 一段的异或和就是两个前缀的异或和.正反扫两边,用trie树算每个位置为左/右端点时最大 ...

  2. BZOJ 4260: Codechef REBXOR (trie树维护异或最大值)

    题意 分析 将区间异或和转化为前缀异或和.那么[L,R][L,R][L,R]的异或和就等于presum[R] xor presum[L−1]presum[R]\ xor \ presum[L-1]pr ...

  3. BZOJ 4260: Codechef REBXOR( trie )

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

  4. bzoj 4260: Codechef REBXOR (01 Trie)

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

  5. BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)

    <题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...

  6. BZOJ 4260 Codechef REBXOR

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

  7. BZOJ 4260 Codechef REBXOR 01trie

    好题...开阔思路 把每个前缀异或和依次插入$01trie$,插之前找一个最优的(就是从高位向低位贪心,尽量走相反方向)看看能不能更新答案,此时相当于找到了区间右端点不超过某个点$r$的最大或和$f[ ...

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

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

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

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

随机推荐

  1. 【洛谷 P1073】 最优贸易 (Tarjan缩点+拓扑排序)

    题目链接 先\(Tarjan\)缩点,记录每个环内的最大值和最小值. 然后跑拓扑排序,\(Min[u]\)表示到\(u\)的最小值,\(ans[u]\)表示到\(u\)的答案,\(Min\)和\(an ...

  2. jqgrid 翻页记录选中行

    简单的jqgrid列表 $("#list").jqGrid({ url:contextPath + "/getList", postData: data, da ...

  3. hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/1000 ...

  4. 64_l5

    libsmartcols-2.29.1-2.fc26.x86_64.rpm 13-Feb-2017 07:04 150590 libsmartcols-devel-2.29.1-2.fc26.i686 ...

  5. 64_d1

    DSDP-5.8-15.fc26.i686.rpm 13-Feb-2017 22:06 658926 DSDP-5.8-15.fc26.x86_64.rpm 13-Feb-2017 22:09 653 ...

  6. ogre3d环境配置 SDK安装配置及简单事例教程

    ogre3d环境配置 SDK安装配置及简单事例教程 http://www.cr173.com/html/22594_1.html ogre3d环境配置 SDK安装配置及简单事例教程 http://ww ...

  7. C#通过反射获取类中的方法和参数个数,反射调用方法带参数

    using System; using System.Reflection; namespace ConsoleApp2 { class Program { static void Main(stri ...

  8. JS如何获取Input的name或者ID?

    <input name="music" type="image" id="music" onclick="loadmusic ...

  9. Web开发中,页面渲染方案

    转载自:http://www.jianshu.com/p/d1d29e97f6b8 (在该文章中看到一段感兴趣的文字,转载过来) 在Web开发中,有两种主流的页面渲染方案: 服务器端渲染,通过页面渲染 ...

  10. Search a 2D Matrix——两度二分查找

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...