BZOJ4260异或和

4260: Codechef REBXOR

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 918  Solved: 420
[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。
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=4e5+88;
struct Trie
{
    int next[2];
} Ti[N*33];
int sz,v[N],l[N],r[N],vt[N];
void add(int x)
{
    for(int u=0,i=30,op; i>=0; --i)
    {
        op=((1<<i)&x)?1:0;
        if(!Ti[u].next[op])
        {
            Ti[u].next[op]=++sz;
            Ti[sz].next[0]=Ti[sz].next[1]=0;
        }
        u=Ti[u].next[op];
    }
}
int get(int x)
{
    int ret=0;
    for(int u=0,i=30,op; i>=0; --i)
    {
        op=((1<<i)&x)?0:1;
        if(Ti[u].next[op])
        {
            u=Ti[u].next[op];
            ret|=(1<<i);
        }
        else u=Ti[u].next[!op];
    }
    return ret;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i) scanf("%d",v+i);
    for(int i=1; i<=n; ++i) vt[i]=v[i]^vt[i-1];
    vt[0]=vt[n+1]=0;
    for(int i=0; i<=n; ++i)
    {
        l[i]=max(l[i-1],get(vt[i]));
        add(vt[i]);
    }
    sz=0;
    Ti[0].next[0]=Ti[0].next[1]=0;//智障处1
    memset(vt,0,sizeof(vt));
    for(int i=n; i>0; --i)
        vt[i]=v[i]^vt[i+1];  //智障处2
    for(int i=n+1; i>=1; --i)
    {
        r[i]=max(get(vt[i]),r[i+1]);  //智障处3
        add(vt[i]);
    }
    int ans=0;
    for(int i=1; i<n; ++i)
        ans=max(ans,l[i]+r[i+1]);
    printf("%d
",ans);
}
 
 
原文地址:https://www.cnblogs.com/mfys/p/7128211.html