Nikitosh and XOR

https://loj.ac/problem/10051

Title Description

  Sequence number N given A, requires two consecutive sub-sequences obtained (without overlapping), their number in the XOR and maximum values.

Thinking

  This question is actually The XOR Largest Pair advanced version, we know that violence is obviously impossible to enumerate, it is necessary to use some optimization. First, we consider if elected for a maximum continuous sequence of how to do it. Clearly we know a property x ^ x = 0,0 ^ x = x, so for some sequences we can use a similar method of maintaining the prefix and, with s [r] ^ s [l -1] Remove the [L , R & lt] exclusive oR of this period, the number may be because the first few XOR to zero. But the complexity of enumeration sequences of violence we still can not afford, but we know quickly find a pair of numbers from a bunch of different number or maximum value. So this is equivalent to the exclusive OR up from a maximum value s [1..r-1] to identify and s [r], can then take a max, we can also be used to maintain the trie.

  Next is the two sequences do not overlap, we know that it is bounded by two [L . 1, R & lt . 1 ], [L 2 , R & lt 2 ], so they must be divided into two parts from one end, so we just the positive do it again, do it again backwards, two arrays l, r, the answer is l [i] + r [i 1 +] maximum values.

Code

#include <bits/stdc++.h>
using namespace std;
const int MAXN=4e5+5;
int ch[MAXN<<5][3],tot,a[MAXN],l[MAXN],r[MAXN];
void insert(int x)
{
    int u=1;
    for(int i=1<<30;i;i>>=1)
    {
        int num=(x&i)?1:0;
        if(!ch[u][num])ch[u][num]=++tot;
        u=ch[u][num];
    }
}
int find(int x)
{
    int u=1,ans=0;
    for(int i=1<<30;i;i>>=1)
    {
        int num=(x&i)?0:1;
        if(ch[u][num])
        {
            ans+=i;
            u=ch[u][num];
        }
        else u=ch[u][!num];
    }
    return ans;
}
int main() 
{
    int n,sum=0;
    scanf("%d",&n);
    tot=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        sum=sum^a[i];
        insert(sum);
        l[i]=max(l[i-1],find(sum));
    }
    memset(ch,0,sizeof(ch));
    sum=0;tot=1;
    for(int i=n;i>0;i--)
    {
        sum=sum^a[i];
        insert(sum);
        r[i]=max(r[i+1],find(sum));
    }
    int ans=0;
    for(int i=1;i<=n;i++)
        ans=max(ans,l[i]+r[i+1]);
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11628475.html
XOR