BZOJ 4260 Codechef REBXOR

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。
因为异或满足前缀和性质
即s[i]^s[j-1]=a[j]^a[j+1].....^a[i]
于是找到一个以i为右端点异或和最大的区间等价于找到与s[i]异或后值最大的s[j-1]
维护一颗trie树,从高位到低位,查询时从上到下贪心尽可能选择高位不同的数
反过来再做一边后缀和
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 typedef long long lol;
 8 int pw2[32],n,f[400005],a[400005],ans;
 9 int ch[12000001][2],size;
10 void insert(int &rt,int s,int l)
11 {
12   if (!rt) rt=++size;
13   if (l==-1)
14     return;
15   if (s&pw2[l]) insert(ch[rt][1],s,l-1);
16   else insert(ch[rt][0],s,l-1);
17 }
18 int query(int rt,int s,int l)
19 {
20   if (l==-1) return 0;
21   bool t=(s&pw2[l]);
22   if (ch[rt][!t])
23     return query(ch[rt][!t],s,l-1)|pw2[l];
24   else return query(ch[rt][t],s,l-1);     
25 }
26 int main()
27 {int i,s,root;
28   pw2[0]=1;
29   for (i=1;i<=30;i++)
30     pw2[i]=pw2[i-1]*2;
31   cin>>n;
32   s=0;
33   root=0;size=0;
34   insert(root,0,30);
35   for (i=1;i<=n;i++)
36     {
37       scanf("%d",&a[i]);
38       s^=a[i];
39       f[i]=max(f[i-1],query(root,s,30));
40       insert(root,s,30);
41     }
42   s=0;
43   root=0;
44   memset(ch,0,sizeof(ch));
45   insert(root,0,30);
46   for (i=n;i>=2;i--)
47     {
48       s^=a[i];
49       ans=max(ans,f[i-1]+query(root,s,30));
50       insert(root,s,30);
51     }
52   cout<<ans;
53 }

 

posted @ 2018-02-03 15:31  Z-Y-Y-S  阅读(193)  评论(0编辑  收藏  举报