cf-282e

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

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 ;
}
上一篇:django项目部署上线


下一篇:django项目部署上线 nginx + uwsgi