BZOJ4260 Codechef REBXOR(trie)

  用trie求出前缀最大区间异或和、后缀最大区间异或和即可。注意空间是nlog的。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
#define N 400010
int n,a[N],pre[N],suf[N],trie[N<<5][2],cnt,ans;
void ins(int x)
{
    int k=0;
    for (int j=30;~j;j--)
    {
        if (!trie[k][(x&(1<<j))>0]) trie[k][(x&(1<<j))>0]=++cnt;
        k=trie[k][(x&(1<<j))>0];
    }
}
int query(int x)
{
    int k=0,s=0;
    for (int j=30;~j;j--)
    if (trie[k][(x&(1<<j))==0]) s|=1<<j,k=trie[k][(x&(1<<j))==0];
    else k=trie[k][(x&(1<<j))>0];
    return s;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj4260.in","r",stdin);
    freopen("bzoj4260.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read();
    for (int i=1;i<=n;i++) a[i]=a[i-1]^read();
    cnt=0;memset(trie,0,sizeof(trie));ins(0);
    for (int i=1;i<=n;i++)
    {
        pre[i]=max(pre[i-1],query(a[i]));
        ins(a[i]);
    }
    cnt=0;memset(trie,0,sizeof(trie));ins(0);
    for (int i=n;i>=1;i--)
    {
        suf[i]=max(suf[i+1],query(a[i]));
        ins(a[i]);
    }
    for (int i=1;i<n;i++) ans=max(ans,pre[i]+suf[i+1]);
    cout<<ans;
    return 0;
}

 

posted @ 2018-10-23 18:39  Gloid  阅读(143)  评论(0编辑  收藏  举报