题面

Description

给定一个含N个元素的数组A,下标从1开始。请找出下面式子的最大值。

(A[l1]xorA[l2+1]xor…xorA[r1])+(A[l2]xorA[l2+1]xor…xorA[r2])

1<=l1<=r1<l2<=r2<=N

Input

输入数据的第一行包含一个整数N,表示数组中的元素个数。

第二行包含N个整数A1,A2,…,AN。

Output

输出一行包含给定表达式可能的最大值。

Sample Input

5
1 2 3 1 2

Sample Output

6

HINT

满足条件的(l1,r1,l2,r2)有:(1,2,3,3),(1,2,4,5),(3,3,4,5)。

对于100%的数据,2 ≤ N ≤ 4*105,0 ≤ Ai ≤ 109。

题目大意

找出两个有序数对\((L_1, R_1)\), \((L_2, R_2)\)满足\(L_1 <= R_1 < L_2 <= R_2\), 使得\(\oplus_{i = L_1}^{R_1} a_i + \oplus_{i = L_2}^{R_2}\)又最大值. 求这个最大值.

题解

我们先求出前缀异或和\(\{A_n = \oplus_{i = 1}^n a_i\}\), 则\(\oplus_{i = L}^R a_i = A_R \oplus A_{L - 1}\). 对于每一个右端点\(A_i\), 我们可以在trie上找到最大的\(A_j : j < i\)使得\(A_i \oplus A_j\)有最大值.

我们把序列反过来从右到左同样方法跑一次, 就能得到以某个位置为分界, 左边取一段异或和, 右边取一段异或和可以得到的最大值.

#include <cstdio>
#include <cctype>
#include <algorithm> const int N = (int)4e5; namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
sgn *= -1;
while(isdigit(c))
a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
} struct trieTree
{
struct node
{
node *suc[2]; inline node()
{
suc[0] = suc[1] = NULL;
}
}; node *rt; inline void insert(int w)
{
node *u = rt;
for(int i = 30; ~ i; -- i)
{
int k = w >> i & 1;
if(u->suc[k] == NULL)
u->suc[k] = new node;
u = u->suc[k];
}
} void Delete(node *u)
{
for(int i = 0; i < 2; ++ i)
if(u->suc[i] != NULL)
Delete(u->suc[i]);
delete u;
} inline void clear()
{
if(rt != NULL)
Delete(rt);
rt = new node();
insert(0);
} inline int query(int w)
{
int res = 0;
node *u = rt;
for(int i = 30; ~ i; -- i)
{
int k = w >> i & 1;
if(u->suc[k ^ 1] == NULL)
u = u->suc[k];
else
u = u->suc[k ^ 1], res |= 1 << i;
}
return res;
}
}trie; int main()
{
#ifndef ONLINE_JUDGE
freopen("REBXOR.in", "r", stdin);
#endif
using namespace Zeonfai;
int n = getInt();
static int a[N];
for(int i = 0; i < n; ++ i)
a[i] = getInt();
static int A[N];
A[0] = a[0];
for(int i = 1; i < n; ++ i)
A[i] = A[i - 1] ^ a[i];
static int mx[N][2];
trie.clear();
for(int i = 0; i < n; ++ i)
mx[i][0] = trie.query(A[i]), trie.insert(A[i]);
for(int i = 1; i < n; ++ i)
mx[i][0] = std::max(mx[i][0], mx[i - 1][0]);
A[n - 1] = a[n - 1];
for(int i = n - 2; ~ i; -- i)
A[i] = A[i + 1] ^ a[i];
trie.clear();
for(int i = n - 1; ~ i; -- i)
mx[i][1] = trie.query(A[i]), trie.insert(A[i]);
for(int i = n - 2; ~ i; -- i)
mx[i][1] = std::max(mx[i][1], mx[i + 1][1]);
int ans = 0;
for(int i = 0; i < n; ++ i)
ans = std::max(ans, mx[i][0] + mx[i][1]);
printf("%d\n", ans);
}

REBXOR的更多相关文章

  1. 【BZOJ4260】 Codechef REBXOR 可持久化Trie

    看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...

  2. BZOJ 4260: Codechef REBXOR( trie )

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

  3. bzoj 4260: Codechef REBXOR (01 Trie)

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

  4. 【BZOJ4260】Codechef REBXOR (Trie树)

    [BZOJ4260]Codechef REBXOR (Trie树) 题面 BZOJ 题解 两眼题.第一眼不会做,第二眼好简单... 前缀异或和一下,拿\(Trie\)树维护求一个在这个端点以左的最大值 ...

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

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

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

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

  7. [Bzoj4260]Codechef REBXOR(trie树)

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

  8. BZOJ4260 Codechef REBXOR 题解

    题目大意: 有一个长度为n的序列,求1≤l1≤r1<l2≤r2≤n使得(⊕r1i=l1ai)+(⊕r2i=l2ai)最大,输出这个最大值. 思路: 用Trie求出前缀异或和以及后缀异或和,再求出 ...

  9. BZOJ4260: Codechef REBXOR

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

随机推荐

  1. 水题:51Nod1432-独木舟

    1432 独木舟 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 Problem Description n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两 ...

  2. /dev/sda is apparently in use by the system; will not make a filesystem here!解决方法

    /dev/sda  is apparently in use by the system; will not make a filesystem here! 翻译:系统显然在使用,不会在这里做文件系统 ...

  3. day02 Python 的模块,运算,数据类型以及方法

    初识pyhton的模块: 什么是模块: 我的理解就是实现一个功能的函数,把它封装起来,在你需要使用的时候直接调用即可,我的印象里类似于shell 的单独函数脚本. python 的模块分为标准的和第三 ...

  4. wordpress 获取站点的所有链接

    <?php include "wp-load.php"; $posts = new WP_Query('post_type=any&posts_per_page=-1 ...

  5. Leetcode 446.等差数列划分II 子序列

    等差数列划分II 子序列 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为等差数列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, ...

  6. redis安装、配置和启动

    一.运行环境 1.vmware虚拟机上的centos7系统,安装步骤略,网上搜搜就有,连接工具:secureCRT 2.新安装的linux,是没有wget命令,所以先执行这个命令安装下:yum -y ...

  7. 2.启动ABP ASP.NET ZERO

    1.使用VS2017打开项目,等待自动还原程序包结束 2.生成项目,确保项目全部生成成功 3.生成数据库 (1).将项目“MyCompanyName.AbpZeroTemplate.EntityFra ...

  8. DS博客作业06—图

    1.本周学习总结 1.1思维导图 1.2学习体会 2.PTA实验作业 2.1 图着色问题 图着色问题是一个著名的NP完全问题.给定无向图G=(V,E),问可否用K种颜色为V中的每一个顶点分配一种颜色, ...

  9. HDU——1874畅通工程续(邻接矩阵弗洛伊德)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. 算法复习——1D/1Ddp优化

    搬讲义~~~~ 题目1:玩具装箱(bzoj1010) Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一 ...