The XOR Largest Pair(tire树)

topic

The XOR Largest Pair

Resolve

A year ago to listen to seniors talk about this question, what 01trie, good senior ah, so I did not learn, and now look. . . .
See xor should think of binary, a look at the data \ (A_i <31 is 2 ^ {} \) , consideration of all the numbers are processed binary number of length 32, into the dictionary tree, when the query-by-bit comparison there are different different go there, thus ensuring that the results of the query every time you insert a number of the largest and continuously updated maximum value on it
I do not have this kind of lazy-bit computing on the direct use bitset maintained
from a high level to better position the insertion may be considered some of the

Code

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

int n, a, num, ans;

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

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

int query(int x) {
    bitset<35>s(x);
    int rt = 0, ret = 0;
    for (int i = 30; i >= 0; --i) {
        int v = (int)s[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() {
    cin >> n;
    for (int i = 1, x; i <= n; ++i) {
        cin >> x;
        ans = max(ans, query(x));
        insert(x);
    }
    cout << ans;
}

Guess you like

Origin www.cnblogs.com/lykkk/p/11256831.html
XOR