Nikitosh and XOR (Trie tree)

topic:

# 10051. "2.3 cases through a 3" Nikitosh and XOR

Resolution:

First, we know that one property \ (x \ oplus x = 0
\) we require \ [\ bigoplus_ {i = l } ^ ra_i \] , then, corresponds to find \ [(\ bigoplus_ {i = 1} ^ la_i) \ oplus (\ bigoplus_ {i = 1}
^ ra_i) \] Therefore, we maintain a prefix and XOR \ (sum_i \)
we \ (L_i \) represents the left to right of \ (I \) period when the maximum bit and exclusive-oR
\ (r_i \) represents the right to left of the \ (I \) at the maximum bit interval and the XOR
clearly \ (l_i = max \ {sum_L \ oplus sum_R \} 1 \ leq L <R \ leq I \)
\ (r_i \) Similarly
last enumerated sum \ (ans = max \ {ans , l_i + r_ {i + 1} \} \)

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 10;

int n, m, num, ans;
int a[N], sum[N], l[N], r[N];

struct node {
    int nx[2];
} e[N]; 

void insert(int x) {
    bitset<35>b(x);
    int rt = 0;
    for (int i = 30; i >= 0; --i) {
        int v = (int)b[i];
        if (!e[rt].nx[v]) e[rt].nx[v] = ++num;
        rt = e[rt].nx[v];
    }
}

int query(int x) {
    bitset<35>b(x);
    int rt = 0, ret = 0;
    for (int i = 30; i >= 0; --i) {
        int v = (int)b[i];
        if (e[rt].nx[v ^ 1]) ret = ret << 1 | 1, rt = e[rt].nx[v ^ 1];
        else ret <<= 1, rt = e[rt].nx[v];
    }
    return ret;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), sum[i] = sum[i - 1] ^ a[i];
    insert(0);
    for (int i = 1; i <= n; ++i) {
        ans = max(ans, query(sum[i]));
        l[i] = ans;
        insert(sum[i]);
    }
    num = ans = 0;
    memset(e, 0, sizeof e);
    for (int i = n; i >= 1; --i) {
        ans = max(ans, query(sum[i]));
        r[i] = ans;
        insert(sum[i]);
    }
    ans = 0;
    for (int i = 1; i < n; ++i) ans = max(ans, l[i] + r[i + 1]);
    cout << ans;
}

Guess you like

Origin www.cnblogs.com/lykkk/p/11265899.html