“字典树”的变形,任意两数异或最大值,处理字典树的时候可以用递归,也可以用循环,下面有两个版本。

C - Sausage Maximization

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64uSubmit Status

Practice CodeForces 282E

Description

The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages! In Bitland a sausage is an array of integers! A sausage's deliciousness is equal to the bitwise excluding OR (the xor operation) of all integers in that sausage. One day, when Mr. Bitkoch (the local cook) was going to close his BitRestaurant, BitHaval and BitAryo, the most famous citizens of Bitland, entered the restaurant and each ordered a sausage. But Mr. Bitkoch had only one sausage left. So he decided to cut a prefix (several, may be zero, first array elements) of the sausage and give it to BitHaval and a postfix (several, may be zero, last array elements) of the sausage and give it to BitAryo. Note that one or both pieces of the sausage can be empty. Of course, the cut pieces mustn't intersect (no array element can occur in both pieces). The pleasure of BitHaval and BitAryo is equal to the bitwise XOR of their sausages' deliciousness. An empty sausage's deliciousness equals zero. Find a way to cut a piece of sausage for BitHaval and BitAryo that maximizes the pleasure of these worthy citizens.

Input

The first line contains an integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 1012) — Mr. Bitkoch's sausage. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Output

Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.

Sample Input

Input

21 2

Output

3

Input

31 2 3

Output

3

Input

21000 1000

Output

1000

 #include <stdio.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = +;
int ch[maxn*][], cnt, root, n;
LL num[maxn], ans;
void Insert(LL tar)
{
int cur = root;
for(int i = ; i >= ; i--)
{
int id = ((tar & (1LL<<(i-))) ? : );
if(ch[cur][id] == -)
{
ch[cnt][] = ch[cnt][] = -;
ch[cur][id] = cnt++;
}
cur = ch[cur][id];
}
}
void Find(LL tar)
{
int cur = root;
LL ret = ;
for(int i = ; i >= ; i--)
{
int id = ((tar & (1LL<<(i-))) ? : );
if(ch[cur][id^] != -)
{
ret |= (1LL << (i-));
cur = ch[cur][id^];
}
else
cur = ch[cur][id];
}
ans = max(ans, ret);
}
int main()
{
LL pre, suf;
while(scanf("%d", &n) != EOF)
{
pre = suf = cnt = ;
ch[cnt][] = ch[cnt][] = -;
root = cnt++;
Insert(0LL);
for(int i = ; i < n; i++)
{
scanf("%I64d", &num[i]);
suf ^= num[i];
}
ans = suf;
for(int i = ; i < n; i++)
{
pre ^= num[i];
suf ^= num[i];
Insert(pre);
Find(suf);
}
printf("%I64d\n", ans);
}
return ;
}
/*
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 100000+100;
int ch[maxn*40][2], cnt, root, n;
LL num[maxn], ans;
void Insert(LL tar)
{
int cur = root;
for(int i = 40; i >= 1; i--)
{
int id = ((tar & (1LL<<(i-1))) ? 1 : 0);
if(ch[cur][id] == -1)
{
ch[cnt][0] = ch[cnt][1] = -1;
ch[cur][id] = cnt++;
}
cur = ch[cur][id];
}
}
void Find(LL tar)
{
int cur = root;
LL ret = 0;
for(int i = 40; i >= 1; i--)
{
int id = ((tar & (1LL<<(i-1))) ? 1 : 0);
if(ch[cur][id^1] != -1)
{
ret |= (1LL << (i-1));
cur = ch[cur][id^1];
}
else
cur = ch[cur][id];
}
ans = max(ans, ret);
}
int main()
{
LL pre, suf;
while(scanf("%d", &n) != EOF)
{
pre = suf = cnt = 0;
ch[cnt][0] = ch[cnt][1] = -1;
root = cnt++;
Insert(0LL);
for(int i = 0; i < n; i++)
{
scanf("%I64d", &num[i]);
suf ^= num[i];
}
ans = suf;
for(int i = 0; i < n; i++)
{
pre ^= num[i];
suf ^= num[i];
Insert(pre);
Find(suf);
}
printf("%I64d\n", ans);
}
return 0;
}
 #include <stdio.h>

 typedef __int64 LL;
const int maxn = + ;
int memory[maxn*][], allocp, root, n;
LL num[maxn], ans;
void Insert(LL tar)
{
LL cur = root;
for(int i = ; i >= ; i--) {
int k = (((1LL<<i) & tar)?:);
if(memory[cur][k] == -) {
memory[allocp][] = memory[allocp][] = -;
memory[cur][k] = allocp++;
}
cur = memory[cur][k];
}
}
void Find(LL x)
{
LL ret = ;
int cur = root;
for(int i = ; i >= ; i--) {
int k = (x & (1LL<<i))?:;
if(memory[cur][k^] != -) {
ret |= (1LL << i);
cur = memory[cur][k^];
} else {
cur = memory[cur][k];
}
}
ans = (ans>ret)?ans:ret;
}
int main()
{
LL pre, suf;
while(~scanf("%d", &n)) {
pre = suf = allocp = ;
memory[allocp][] = memory[allocp][] = -;
root = allocp++;
Insert(0LL);
for(LL i = ; i < n; i++) {
scanf("%I64d", &num[i]);
suf ^= num[i];
}
ans = suf;
for(int i = ; i < n; i++)
{
pre ^= num[i];
suf ^= num[i];
Insert(pre);
Find(suf);
}
printf("%I64d\n", ans);
}
return ;
}

cf-282e的更多相关文章

  1. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  4. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  5. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  7. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  8. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  9. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

  10. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

随机推荐

  1. Linux下/etc/resolv.conf 配置DNS客户

    文件/etc/resolv.conf配置DNS客户,它包含了主机的域名搜索顺序和DNS服务器的地址,每一行应包含一个关键字和一个或多个的由空格隔开的参数.下面是一个例子文件: search mydom ...

  2. VMware网络配置 - 三种网络模式简介

    安装好虚拟机以后,在网络连接里面可以看到多了两块网卡: 其 中VMnet1是虚拟机Host-only模式的网络接口,VMnet8是NAT模式的网络接口,这些后面会详细介绍 选择虚拟机网络模 式方法如下 ...

  3. iOS面试题01

    1.#import和#include.@class有什么区别?#import<>和#import“”又有什么区别? 答:1.#import和#include都能完整地包含某个文件的内容,# ...

  4. synchronized原理

    http://www.cnblogs.com/YDDMAX/p/5658607.html http://www.cnblogs.com/YDDMAX/p/5658668.html synzhroniz ...

  5. 最简单的PC机串口通信程序

    把串口当作文件IO来操作,简单易行!    已验证,gcc和tcc都可以编译成功,并使用.  需注意,先有串口,改好红色字体串口号再编译运行! #include <stdio.h>  #i ...

  6. 如何查看mac系统是32位还是64位的操作系统

    (一)点击工具栏左上角点击 (苹果Logo)标志,关于本机  -->  更多信息 --> 系统报告  -->(左侧栏中)软件 (二)打开终端,输入命令 uname -a 回车 x8 ...

  7. iOS:BitCode的介绍

    一.什么是BitCode?作用是什么? Bitcode is an intermediate representation of a compiled program. Apps you upload ...

  8. iOS:iOS开发非常全的三方库、插件等等

    iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...

  9. 压测 502 日志报错 upstream timed out (110: Connection timed out)

    环境介绍 服务器:centos6.5服务:nginx proxy 问题描述: 压测 开发同事 的开发环境项目没事,但是 线上机器 命中%50 ,大量502 php的某些页面打不开,页面提示gatewa ...

  10. 什么是BI及哪些行业需要用到BI?

    什么是BI?哪些行业需要用到BI?BI——就是分析利用企业已有的各种商用数据来了解企业的经营状况和外部环境,从而为企业的经营决策提供数据支撑.下面我们来详细分解下: 企业应用BI的目标即是期望通过对来 ...