Nikitosh和异或

因为下面这段代码卡了接近一小时!

int s=p&1<<i; // wrong
int s=p>>i&1; // correct

题意:最大化两个异或对之和(还是看下面的题面吧)

思路

  1. 预处理前缀最大异或对,然后从后往前求后缀最大异或对即可
  2. 当然整个过程都利用了 01 t r i e 01trie 01trie性质,即在已经构建的 01 t r i e 01trie 01trie上贪心地选择路径

题面描述

#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}

const int maxn = 4e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const double eps = 1e-7;

int N;
int a[maxn];
int dp[maxn];
int node[maxn<<5][2], tot;

inline void insert(int p) {
    int now=0;
    for(int i=30; i>=0; --i) {
        int s=p>>i&1;
        if(!node[now][s]) node[now][s]=++tot;
        now=node[now][s];
    }
}

inline int match(int p) {
    int now=0, ans=0;
    for(int i=30; i>=0; --i) {
        int s=p>>i&1;
        if(node[now][!s]) ans|=1<<i, now=node[now][!s];
        else now=node[now][s];
    }
    return ans;
}

int main() {
    //ios::sync_with_stdio(false); cin.tie(0);
    N=read();
    for(int i=1; i<=N; ++i) a[i]=a[i-1]^read();
    insert(0);
    for(int i=1; i<N; ++i) dp[i]=max(dp[i-1],match(a[i])), insert(a[i]);
    for(int i=0; i<=tot; ++i) node[i][0]=node[i][1]=0;
    tot=0, insert(a[N]); int pre=0; ll ans=0;
    for(int i=N-1; i; --i)
        ans=max(ans,ll(dp[i])+(pre=max(pre,match(a[i])))), insert(a[i]);
    cout<<ans<<endl;
}