Codechef REBXOR
2024-09-04 11:35:52
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 ;
}
最新文章
- 如何在ASP.NET 5上搭建基于TypeScript的Angular2项目
- TBB 学习笔记
- MYSQL界面操作系统之phpMyAdmin
- 关于TCP传输速率的测量方法
- 控制GridView中字段的长度,规范数据
- Inno Setup入门(二)&mdash;&mdash;修改安装过程中的图片
- iptables禁止ping入
- 深度学习的2016: NIPS 2016速览
- Cocoapods安装过程
- RTMPdump(libRTMP)源代码分析 4: 连接第一步——握手(Hand Shake)
- LeetCode刷题专栏第一篇--思维导图&;时间安排
- 【转载】KETTLE集群搭建
- JSP动态页面技术
- 用MyEclipse JPA创建项目(三)
- 为什么说 LINQ 要胜过 SQL
- SSM框架之RestFul示例
- WPF实现的简单饼图
- # 20145314《信息安全系统设计基础》期中复习总结 Part B
- 牛客小白月赛4C——病菌感染
- Coconuts HDU - 5925 二维离散化 自闭了