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

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 some command(continue...)

    挂载硬盘 sudo mount -t ext4 /dev/sdb1 /media/hadoop 自动挂载相关 sudo blkid sudo fdisk -l vim /etc/fstab cat / ...

  2. [LeetCode]题解(python):074-Search a 2D Matrix

    题目来源 https://leetcode.com/problems/search-a-2d-matrix/ Write an efficient algorithm that searches fo ...

  3. 获取网络状态ios(2G、3G、4G、Wifi)

    +(NSString *)getNetWorkStates{UIApplication *app = [UIApplication sharedApplication];NSArray *childr ...

  4. JS-001-单选复选按钮操作

    此文主要针对 web 页面中常见元素(例如:单选按钮.复选按钮)的 JavaScript 操作,进行简单的源码示例演示,敬请小主们参阅.若有不足之处,敬请大神指正,不胜感激! 话不多言了,直接上码: ...

  5. 【转】Android中Application类用法

    转自:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application和A ...

  6. ubuntu12.04进入单用户模式

    更改ubuntu配置文件出错了,只能进入单用户模式改回来. 转载至: http://blog.csdn.net/lfyaa/article/details/9899497 1.重启ubuntu,VMw ...

  7. ASIHTTPRequest实现https双向认证请求

    什么是双向认证呢?简而言之,就是服务器端对请求它的客户端要进行身份验证,客户端对自己所请求的服务器也会做身份验证.服务端一旦验证到请求自己的客户端为不可信任的,服务端就拒绝继续通信.客户端如果发现服务 ...

  8. mysql按条件查询当条件是数字的时候加不加引号是一样的。

    select * from user where id=1 select * from user where id="1" 在查询的注意是否需要加上"";

  9. linux下如何启动/停止/重启mysql:

    一.启动方式1.使用linux命令service 启动:service mysqld start2.使用 mysqld 脚本启动:/etc/inint.d/mysqld start3.使用 safe_ ...

  10. 史上最全的iOS面试题及答案

    迷途的羔羊--专为路痴量身打造的品牌.史上最精准的定位.想迷路都难!闪电更新中...敬请期待,欢迎提意见.下载地址:https://itunes.apple.com/us/app/mi-tu-de-g ...