bzoj 4260 Codechef REBXOR——trie树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4260
一段的异或和就是两个前缀的异或和。正反扫两边,用trie树算每个位置为左/右端点时最大异或和,并维护前/后缀max;然后枚举分界线即可。
注意&出来是值,要弄一弄才能变成bool。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=4e5+,M=;
int n,a[N],bin[M+],tot,c[N*M][],s[N][],mx,ans;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
void init()
{
bin[]=;for(int i=;i<=n;i++)bin[i]=bin[i-]<<;
}
int calc(int sm)
{
int cr=,ret=;
for(int i=mx,d;i>=;i--)
{
d=((sm&bin[i])>);//((__)>0)
if(c[cr][!d])cr=c[cr][!d],ret|=bin[i];
else cr=c[cr][d];
}
return ret;
}
void insert(int sm)
{
int cr=;
for(int i=mx,d;i>=;i--)
{
d=((sm&bin[i])>);//
if(!c[cr][d])c[cr][d]=++tot;
cr=c[cr][d];
}
}
int main()
{
n=rdn(); init();
for(int i=;i<=n;i++)a[i]=rdn(),mx=max(mx,a[i]);
for(int i=;i>=;i--)if(mx&bin[i]){mx=i;break;}
int sm=; insert();//after cal mx!
for(int i=;i<=n;i++)
{
sm^=a[i]; s[i][]=max(s[i-][],calc(sm));
insert(sm);
}
tot=;memset(c,,sizeof c);insert();
sm=;
for(int i=n;i;i--)
{
sm^=a[i]; s[i][]=max(s[i+][],calc(sm));
insert(sm);
}
for(int i=;i<n;i++) ans=max(ans,s[i][]+s[i+][]);
printf("%d\n",ans);
}
bzoj 4260 Codechef REBXOR——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, ...
- 【BZOJ4260】Codechef REBXOR Trie树+贪心
[BZOJ4260]Codechef REBXOR Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output ...
- BZOJ 4260 Codechef REBXOR
Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. Sample ...
- 【bzoj4260】Codechef REBXOR Trie树
题目描述 输入 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. 输出 输出一行包含给定表达式可能的最大值. 样例输入 5 1 2 3 1 2 样例输出 ...
- BZOJ 4260 Codechef REBXOR(字典树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4260 [题目大意] 给出一个数列,请找出两段连续且不相交的数段,使得其分别异或和的和 ...
- 【bzoj4260】 Codechef REBXOR trie树
Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. Sample Input ...
随机推荐
- Free Pascal 的安装
Free Pascal 的安装 https://www.cnblogs.com/cnssc/p/6110492.html https://wenku.baidu.com/view/ee80cc8eed ...
- urllib(最基本的)库的应用
Urllib库 python内置的http请求库 1.urllib.request 请求模块 2.urllib.error 异常处理模块(try,catch) 3.urllib.parse url解析 ...
- ionic 修改应用名称 、启动页出现黑白屏 及 修改百度离线地图 点聚合 图标
1.ionic 修改应用名称 2.启动页打开后会在图片消失会出现一小段黑屏的时间 解决方法: 首先,启动页的图片消失时间默认是在config.xml配置的 <preference name=&q ...
- PJSIP 调用的GUID库
PJSIP库产生随机序列串用到GUID库,针对不同的平台使用的方式不同:Windows平台下使用的是Windows系统API CoCreateGuid,在方法 pj_generate_unique_s ...
- ORA-00600: internal error code, arguments: [2662], [0], [1106971], [0], [1107731], [12583040]
今天是2014-06-06,在进行数据库恢复的时候出现了一个久违的ora-600 [2662]错误.特整理例如以下: 问题描写叙述: system及数据文件误删,採用恢复数据文件的方式将数据库恢复到開 ...
- php中屏蔽date的错误
php中添加 date_default_timezone_set('asia/shanghai'); 可以屏蔽 <?php echo date('Y-m-d',$row3['time']); ...
- Android自动折行TextView Group
package com.test.testview; import java.util.ArrayList; import android.content.Context; import androi ...
- codeforces Looksery Cup 2015 H Degenerate Matrix
The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...
- ContentPresenter理解
这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...
- 【BZOJ1520】[POI2006]Szk-Schools KM算法
[BZOJ1520][POI2006]Szk-Schools Description Input Output 如果有可行解, 输出最小代价,否则输出NIE. Sample Input 5 1 1 2 ...