Bzoj 4260——Codechef REBXOR(Trie)

传送门

 事实上,只需稍加变化就可以将此题变为The Xor Largest Pair (这是什么玩意儿???请看我的随笔(传送门))

设l[i]表示1≤l≤r≤i的最大的a[l]^a[l+1]^...^a[r],r[i]表示i≤l≤r≤N的最大a[l]^a[l+1]^...^a[r],则结果为max(l[i]+r[i+1]); 那么,如何求l[i]与r[i]呢?

设x[i]表示a[0]^a[1]^...^a[i](a[0]=0),那么l[i]=max(x[i]^x[j])(0≤j<i),此时用一下The Xor Largest Pair即可,r[i]同理

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cstdlib>
 7 using namespace std;
 8 int ch[400005<<5][2],tot=1,n,a[400005],l[400005],r[400005];
 9 void insert(int x)
10 {
11     int u=1;
12     for (int i=1<<30; i; i>>=1){
13         int c=x&i; if (c) c=1; else c=0;
14         if (!ch[u][c]) ch[u][c]=++tot;
15         u=ch[u][c];
16     }
17 }
18 int get(int x)
19 {
20     int u=1,ans=0;
21     for (int i=1<<30; i; i>>=1){
22         int c=x&i; if (c) c=0; else c=1;
23         if (ch[u][c]) ans+=i,u=ch[u][c]; else u=ch[u][!c];
24     }
25     return ans;
26 }
27 int main()
28 {
29     scanf("%d",&n);
30     memset(ch,0,sizeof(ch));
31     for (int i=1; i<=n; i++) scanf("%d",&a[i]);
32     insert(0); int now=0;
33     for (int i=1; i<=n; i++){
34         now^=a[i];
35         insert(now);
36         l[i]=max(l[i-1],get(now));
37     }
38     memset(ch,0,sizeof(ch));  tot=1;insert(0); now=0;
39     for (int i=n; i>=1; i--){
40         now^=a[i];
41         insert(now);
42         r[i]=max(r[i+1],get(now));
43     }
44     int ans=0;
45     for (int i=1; i<n; i++) ans=max(ans,l[i]+r[i+1]);
46     cout<<ans<<endl;
47     return 0;
48 }

 miao~~~

posted @ 2018-08-01 15:05  wangyh1008  阅读(176)  评论(0编辑  收藏  举报