1. 程式人生 > >bzoj4260 Codechef REBXOR

bzoj4260 Codechef REBXOR

題目描述:

 題解:
Trie樹。

從左向右掃一遍,然後從右向左掃一遍。

程式碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 400050
#define M 31*N
inline int rd()
{
    int f=1,c=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while
(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();} return f*c; } int n,p[N]; int l[N],r[N]; struct Trie { int ch[M][2],siz[M],tot; void insert(int x) { int u=0; for(int i=29;i>=0;i--) { int k = (x>>i)&1; if(!ch[u][k])ch[u][k]=++tot; u
=ch[u][k];siz[u]++; } } int query(int x) { int u = 0,ret = 0; for(int i=29;i>=0;i--) { int k = (x>>i)&1; if(siz[ch[u][!k]]) { ret|=(1<<i); u=ch[u][!k]; }else if
(siz[ch[u][k]]) { u=ch[u][k]; }else break; } return ret; } void init() { for(int i=0;i<=tot;i++) { ch[i][0]=ch[i][1]=siz[i]=0; } tot=0; } }tr; int main() { n=rd(); for(int i=1;i<=n;i++)p[i]=rd(); for(int i=1;i<=n;i++) { l[i]=tr.query(p[i]); tr.insert(p[i]); } for(int i=n;i>=1;i--) { r[i]=max(r[i+1],tr.query(p[i])); tr.insert(p[i]); } int ans = 0; for(int i=1;i<n;i++)ans=max(ans,l[i]+r[i+1]); printf("%d\n",ans); return 0; }