BZOJ4260 [Codechef REBXOR]

The Title requires two subsections in a sequence to maximize the XOR sum of each subsection.

Speaking of XOR, I think of blooming TrieTrieTrie dictionary tree.

First consider how to find the XOR sum of a lll to rrr subsegment ((XOR sum: all XOR values)):

al⊕al+1⊕...⊕ar−1⊕ar=(a1⊕a2⊕...⊕ar−1⊕ar)⊕(a1⊕a2⊕...⊕al−2⊕al−1)a_l\oplus a_{l+1}\oplus...\oplus a_{r-1} \oplus a_{r}=(a_1\oplus a_2\oplus...\oplus a_{r-1} \oplus a_{r})\oplus(a_1\oplus a_2\oplus...\oplus a_{l-2} \oplus a_{l-1})al​⊕al+1​⊕...⊕ar−1​⊕ar​=(a1​⊕a2​⊕...⊕ar−1​⊕ar​)⊕(a1​⊕a2​⊕...⊕al−2​⊕al−1​)

So we can deal with XOR prefix sum and add 01Trie01Trie01Trie tree to solve it.

Considering the requirement of two subsegments, ls[i],rs[i]ls[i],rs[i]ls[i],rs[i] respectively represent the largest XOR subsegment of 1_i1-i1_i and the largest XOR subsegment of i_n I-N i_n. Finally, sweep through maxmaxmax.
The code is not long:

#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define lowbit(x) (x)&(-x)
#define oo (1e18)
#define ll long long
#define LL unsigned long long
using namespace std;
int n,a[400005],s[400005],ls[400005],rs[400005],ch[12500000][2],cnt;
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=(ret<<3)+(ret<<1)+(ch^48);ch=getchar();}
    return ret*ff;
}
inline void insert(int x){
    int now=0;
    for(int i=30;i>=0;i--){
        int t=(x&(1<<i))?1:0;
        if(!ch[now][t]) ch[now][t]=++cnt;
        now=ch[now][t];
    }
}
inline int find(int x){
    int now=0,res=0;
    for(int i=30;i>=0;i--){
        int t=(x&(1<<i))?0:1;
        if(ch[now][t]){
            now=ch[now][t];
            res+=(1<<i);
        }
        else now=ch[now][!t];
    }
    return res;
}
signed main(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    ls[1]=a[1];
    insert(a[1]);
    for(int i=2;i<=n;i++){
        ls[i]=max(ls[i-1],find(a[i]));
        insert(a[i]);
    }
    memset(ch,0,sizeof(ch));
    cnt=0;
    rs[n]=a[n];
    insert(a[n]);
    for(int i=n-1;i>=2;i--){
        rs[i]=max(rs[i+1],find(a[i]));
        insert(a[i]);
    }
    int ans=0;
    for(int i=1;i<=n-1;i++) ans=max(ans,ls[i]+rs[i+1]);
    printf("%d",ans);
    return 0;
}

Posted by evaoparah on Thu, 28 Mar 2019 21:06:28 -0700