跳到正文

题解

U109923 [Codechef REBXOR]Nikitosh and xor思路

Link_1314约 5 分钟
原文
文章信息与版本

文章信息

收录
2026年2月23日
更新
2026年5月2日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

前言

考场上不知为何就想出了这个解法,如有细节错误,求神犇指点。

思路

这道题可以利用前缀和和字典树来求解。

首先我们知道两个性质,那就是 x∧x=0x\wedge x = 0,0∧x=x0\wedge x = x。

然后我们把数组分为左右两边,枚举两段区间之间的断点。

我们用 maxleft[x]maxleft[x] 表示区间 [1,x][1,x] 所能得到的区间异或最大值,用 maxright[x]maxright[x] 表示区间 [x,n][x,n] 所能得到的区间异或最大值。现在想办法求 maxleftmaxleft 和 maxrightmaxright,考虑更新 maxleft[x]maxleft[x]。

易得 maxleft[x]=max⁡(maxleft[x−1],max⁡(a[i]∧a[i+1]⋯∧a[x]))maxleft[x]=\max(maxleft[x - 1],\max(a[i]\wedge a[i + 1]\cdots\wedge a[x])),其中 max⁡(a[i]∧a[i+1]⋯∧a[x])\max(a[i]\wedge a[i + 1]\cdots\wedge a[x])可以这样求:因为 x∧x=0x\wedge x = 0,所以区间 [l,r][l,r] 的异或值等于 (a[1]∧a[2]∧a[3]⋯∧a[l−1])∧(a[1]∧a[2]∧a[3]⋯∧a[r])=A[l−1]∧A[r](a[1]\wedge a[2]\wedge a[3]\cdots\wedge a[l - 1])\wedge(a[1]\wedge a[2]\wedge a[3]\cdots\wedge a[r]) = A[l - 1]\wedge A[r]。所以求 maxleftmaxleft 转化为找 j<ij<i 使得 A[i]∧A[j]A[i]\wedge A[j] 最大。

在实际操作中可以 maxleft=max⁡(maxleft[i−1],getmax(x))maxleft=\max(maxleft[i - 1],getmax(x)),这样 ans=max⁡(maxleft[i]+maxright[i+1])ans=\max(maxleft[i] + maxright[i + 1])就行了。

代码如下

C++
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500000;
int A[MAXN], N;
int maxleft[MAXN], maxright[MAXN];
int ne[20000000][2];
int nnodes;
void Insert(int x){
    for(int bit = 30, node = 1; bit >= 0; bit--){
        int dir = 0;
        if((x & (1 << bit)) > 0) dir = 1;
        if(ne[node][dir] == 0){
            nnodes++;
            ne[node][dir] = nnodes;
            ne[nnodes][0] = ne[nnodes][1] = 0;
        }
        node = ne[node][dir];
    }
}
int getmax(int x){//返回与 x 异或最大的数
    int ans = 0;
    for(int bit = 30, node = 1; bit >= 0; bit--){
        int dir = 0;
        if((x & (1 << bit)) > 0) dir = 1;
        if(ne[node][1 - dir] > 0){
            ans |= (1 << bit);
            dir = 1 - dir;
        }
        node = ne[node][dir];
    }
    return ans;
}
int main(){
    // freopen("xor.in","r",stdin);
    // freopen("xor.out","w",stdout);
    std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> N;
    for(int i = 1; i <= N; i++){
        cin >> A[i];
    }
    nnodes = 1;
    ne[1][0] = ne[1][1] = 0;
    Insert(0);
    int i, sxor = 0;
    maxleft[0] = 0;
    //枚举 i,利用字典树,边插入边查询
    for(int i = 1; i <= N; i++){//求前缀和
        sxor ^= A[i];
        maxleft[i] = getmax(sxor);
        if(maxleft[i - 1] > maxleft[i]) maxleft[i] = maxleft[i - 1];
        Insert(sxor);
    }
    nnodes = 1;
    ne[1][0] = ne[1][1] = 0;
    Insert(0);
    sxor = 0;
    maxright[N + 1] = 0;
    for(int i = N; i >= 1; i--){//求前缀和
        sxor ^= A[i];
        maxright[i] = getmax(sxor);
        if(maxright[i + 1] > maxright[i]) maxright[i] = maxright[i + 1];
        Insert(sxor);
    }
    unsigned long long ans = 0, cans;
    for(int i = 2; i <= N; i++){
        cans = (unsigned long long)maxleft[i - 1] + (unsigned long long)maxright[i];
        if(cans > ans) ans = cans;
    }
    cout << ans << endl;
    return 0;
}
原文