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

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. Arcgis for JS之Cluster聚类分析的实现

    原文:Arcgis for JS之Cluster聚类分析的实现 在做项目的时候,碰见了这样一个问题:给地图上标注点对象,数据是从数据库来 的,包含XY坐标信息的,通过graphic和graphicla ...

  2. LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...

  3. Miscellaneous--Tech

    1. Questions: 1)EF.2)MVC/MVP/MVVM.3)page lifecyle. preInit,Init,InitCompleted,preLoad,Load,LoadCompl ...

  4. 折腾Centos6.4记

    背景: 闲置了一台Thinkpad,之前装的是Kali Linux,但无线网卡挂掉了,加之硬盘分区不当,平时几乎没怎么用,重新使用kali的livecd进行分区,然后安装,总是出错,尝试了七八次,仍然 ...

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

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

  6. Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select

    此文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,对下拉列表框 Select 的操作. 下拉列表是 Web UI 自动化测试过程中使用率非常高的,通常有两种形式的下拉列表,一 ...

  7. 前端面试题(html篇)

    前端面试题(html篇)

  8. free 命令

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...

  9. linux php

    ubuntu php -v 查看php版本号 查看目录 cd / ls apache: 如果采用RPM包安装,安装路径应在/etc/httpd目录下 apache配置文件:/etc/httpd/con ...

  10. 九个uname命令获取Linux系统详情的实例

    当你在控制台模式下,无法通过“鼠标右键 > 关于”获取操作系统的信息.这时,在Linux下,你可以使用uname命令,帮助你完成这些工作. Uname是unix name的缩写.在控制台中实际使 ...