题面

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. apicloud入门学习笔记1:简单介绍

    官网地址:https://www.apicloud.com/ 新手开发指南:https://docs.apicloud.com/APICloud/junior-develop-guide 开发语言:H ...

  2. PTA 7-2 符号配对

    直接用栈模拟即可,数组可做,但因为这节数据结构是栈,为了期末考试还是手写一下栈的操作,值得注意的是,这道题用gets函数在PTA上会编译错误,用scanf("%[^\n]", st ...

  3. POJ:3041-Asteroids(匈牙利算法模板)

    传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...

  4. 二叉排序树:POJ2418-Hardwood Species(外加字符串处理)

    Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Description Hardwoods are the botanical gr ...

  5. Linux学习-开放源码的软件安装与升级简介

    什么是开放源码.编译程序与可执行文件 我们说过,在 Linux 系统上面,一个文件能不能被执行看的是有没有可执行的那个权限 (具有 x permission),不过,Linux 系统上真 正认识的可执 ...

  6. 令人惊叹的Visual Studio Code插件

    vscode是一款开源且优秀的编辑器,接下来让我吐血推荐一下我工作使用过的令人惊叹的Visual Studio Code插件. 代码编辑插件 vscode-color-highlight ------ ...

  7. python - unittest - testsuite and runner

    前置条件: 测试用例部分或全部编写完成 一.  生成测试集 1. 方法1 - 通过加载函数来加载测试用例 import unittest from TestCase.test_login import ...

  8. CodeM美团点评编程大赛初赛A轮

    因为语文太差弃赛,第一个追及问题看不懂我就弃赛了.打进复赛确实挺难的,补一下题,锻炼下就行了. 身体训练 时间限制:1秒 空间限制:32768K 美团外卖的配送员用变速跑的方式进行身体训练.他们训练的 ...

  9. javascript基础2 判断 数据类型

    js中的数据类型: ------------------------------------------------------------------------------- 返回undefine ...

  10. iOS------主题设置-->Appearance

    一.简述UIAppearance 是什么? 1.UIAppearance是一个协议 @protocol UIAppearance <NSObject> 只要遵守了UIAppearance协 ...