您的位置:首页 > 其它

BZOJ 4260 Codechef REBXOR 01Trie

2016-11-22 01:34 447 查看

4260: Codechef REBXOR

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 595  Solved: 273

[Submit][Status][Discuss]

Description





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。

Source

By yts1999

.思路:01Trie。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
const int MAX = 4e5 + 5;
struct Trie
{
int root, tot, next[MAX << 4][2], cnt[MAX << 4], end[MAX << 4];
inline int Newnode()
{
memset(next[tot], -1, sizeof next[tot]);
cnt[tot] = 0;
end[tot] = 0;
return tot++;
}
inline void Init()
{
tot = 0;
root = Newnode();
}
inline void Insert(int x)
{
int p = root;
cnt[p]++;
for(int i = 31; i >= 0; i--)
{
int idx = ((1 << i) & x) ? 1 : 0;
if(next[p][idx] == -1)
next[p][idx] = Newnode();
p = next[p][idx];
cnt[p]++;
}
end[p] = x;
}
inline void Delete(int x)
{
int p = root;
cnt[p]--;
for(int i = 31; i >= 0; i--)
{
int idx = ((1 << i) & x) ? 1 : 0;
p = next[p][idx];
cnt[p]--;
}
}
inline int Search(int x)
{
int p = root;
for(int i = 31; i >= 0; i--)
{
int idx = ((1 << i) & x) ? 1 : 0;
if(idx == 0)
p = next[p][1] != -1 ? next[p][1] : next[p][0];
else
p = next[p][0] != -1 ? next[p][0] : next[p][1];
}
return x ^ end[p];
}
};
Trie tr1, tr2;
int num[MAX];
int maxl[MAX], maxr[MAX];
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
}
tr1.Init();
int now = 0;
for(int i = 1; i <= n; i++)
{
tr1.Insert(now);
now ^= num[i];
maxl[i] = max(maxl[i - 1], tr1.Search(now));
}
tr2.Init();
now = 0;
for(int i = n; i > 0; i--)
{
tr2.Insert(now);
now ^= num[i];
maxr[i] = max(maxr[i + 1], tr2.Search(now));
}
int ans = 0;
for(int i = 1; i < n; i++)
{
ans = max(ans, maxl[i] + maxr[i + 1]);
}
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息