【BZOJ】4260: Codechef REBXOR【Trie树】【前后缀异或最大】
4260: Codechef REBXOR
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 2218 Solved: 962
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2 3 1 2
Sample Output
HINT
Solution
用trie树处理出以$i$结尾的最大区间异或,前后分别扫一遍处理前缀和后缀。具体做法是将前缀或后缀插入trie树中,每次用前缀或后缀在trie树中查询异或最大,就是查询以$i$结尾的区间异或最大。
然后每个位置与前面取max,最后合并统计答案即可。
Code
#include<bits/stdc++.h>
using namespace std; int n, a[], l[], r[];
int son[*][], tail; void read(int &x) {
x = ; char ch = getchar();
while(ch > '' || ch < '') ch = getchar();
while(ch >= '' && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
} void insert(int x) {
int nd = ;
for(int i = ; ~i; i --) {
int t = & (x >> i);
if(!son[nd][t]) son[nd][t] = ++ tail;
nd = son[nd][t];
}
} int query(int x) {
int nd = , ans = ;
for(int i = ; ~i; i --) {
int t = & (x >> i);
if(son[nd][!t]) {
ans += ( << i); nd = son[nd][!t];
} else if(son[nd][t]) nd = son[nd][t];
else break;
}
return ans;
} int main() {
read(n);
int now = ;
insert();
for(int i = ; i <= n; i ++) {
read(a[i]); now ^= a[i];
insert(now);
l[i] = query(now);
}
for(int i = ; i <= n; i ++) l[i] = max(l[i], l[i - ]);
now = ;
memset(son, , sizeof(son)); tail = ;
insert();
for(int i = n; i >= ; i --) {
now ^= a[i];
insert(now);
r[i] = query(now);
}
for(int i = n; i >= ; i --) r[i] = max(r[i], r[i + ]);
long long ans = ;
for(int i = ; i <= n; i ++)
ans = max(ans, 1ll * (l[i] + r[i + ]));
printf("%lld", ans);
return ;
}
【BZOJ】4260: Codechef REBXOR【Trie树】【前后缀异或最大】的更多相关文章
- 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 ...
- 【BZOJ4260】Codechef REBXOR Trie树+贪心
[BZOJ4260]Codechef REBXOR Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output ...
- BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)
<题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...
- 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 ...
随机推荐
- listen系统调用
/* * Perform a listen. Basically, we allow the protocol to do anything * necessary for a listen, and ...
- 使用脚本实现killproc的功能
在shell提示符号下输入type killproc,会发现killproc实在 /sbin/目录下,通过man killproc可以查看这个脚本(姑且这么称为脚本)的用法,现在,把这个脚本的实现过程 ...
- npm 下载node-zookeeper包
环境:centos7(lunix) 1.安装nvm curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install. ...
- 栈应用之 背包问题(Python 版)
栈应用之 背包问题 背包问题描述:一个背包里可以放入重量为weight的物品,现有n件物品的集合s,其中物品的重量为别为w0,w1,...,wn-1.问题是能否从中选出若干件物品,其重量之和正好等于w ...
- Vue 实现loading进度条
项目中遇到的,用vue实现下: <template> <div class="plLoading"> <div class="plLoadi ...
- Hexo命令无法找到 -问题修复
本人PC安装hexo按照官方npm方式下载: npm install -g hexo-cli 但是到了控制台,输入hexo总是无法找到该命令,提示:Command not Found!!!,无论git ...
- Elasticsearch: 权威指南---基础入门
1.查看方式:GETURL:http://10.10.6.225:9200/?pretty pretty 在任意的查询字符串中增加pretty参数.会让Elasticsearch美化输出JSON结果以 ...
- springboot 1.5.X junit测试
import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; impo ...
- day7 socket网络编程
Python Socket网络编程 Socket是进程间通信的一种方式,它与其他进程间通信的一个主要不同是:它能实现不同主机间的进程间通信,我们网络上各种各样的服务大多都是基于Socket来完成通信的 ...
- USACO 5.5 Hidden Password
Hidden Password ACM South Eastern Europe -- 2003 Sometimes the programmers have very strange ways of ...