这道题要处理节点与父亲节点的前缀,而不是dfs的区间前缀。
当查询的时候对每一位只需要查询sum[x][!t]+sum[y][!t] > sum[lca][!t]就行了。
倍增
#pragma GCC optimize(2)
#pragma comment(linker, “/ STACK : 1024000000, 1024000000”)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int Case = 1;
int n, m, cc[maxn], cnt, head[maxn], dep[maxn];
int p[30][maxn];
struct node {
int val, v, nex;
} CC[maxn << 1];
void add(int u, int v) {
cnt++;
CC[cnt].v = v;
CC[cnt].nex = head[u];
head[u] = cnt;
}
int root[maxn << 6];
void init() {
cnt = 0;
memset(head, 0, sizeof(head));
}
struct Trie {
int nex[maxn << 6][2], tot, sum[maxn << 6][2];
int newnode() {
nex[tot][0] = nex[tot][1] = 0;
return tot++;
}
void init() {
tot = 1;
nex[0][0] = nex[0][1] = 0;
}
int insert(int x, int id) {
int t = newnode(), res = t;
for (int i = 16; i >= 0; i--) {
int tx = (1 << i) & x;
tx = (tx >> i);
sum[t][tx] = sum[id][tx] + 1;
sum[t][!tx] = sum[id][!tx];
nex[t][tx] = newnode();
nex[t][!tx] = nex[id][!tx];
t = nex[t][tx];
id = nex[id][tx];
}
return res;
}
int query(int lca, int x, int y, int s) {
int res = 0, ans = cc[lca] ^ s;
lca = root[lca];
for (int i = 16; i >= 0; i--) {
int t = (1 << i) & s;
t = (t >> i);
if (sum[y][!t]