题意:n个点的树,Q次询问,询问u-v路径上的点的权值与z抑或的最大值。
先考虑,在一个区间上的问题,可以先建一个可持久化的Trie,然后每次询问,就和线段树的操作差不多,从最高位开始考虑选1还是选0。
在树上的话, 可以转化成 parent[LCA(u, v)] - u , parent[LCA(u, v)] - v,这两个区间的最大值。
#include <bits/stdc++.h> using namespace std;
const int maxn = 1e5 + ;
const int maxdep = ;
const int maxlog = ;
vector <int> G[maxn];
int idx, A[maxn];
//可持久化Trie, 初始状态val为0
int tree[maxn], lson[maxn * maxlog], rson[maxn * maxlog], val[maxn * maxlog];
int insert(int root, int x){ // x为每次插入的值
int newroot = idx++;
int tmp = newroot;
for (int i = ; i >= ; i--){
if (!(x & ( << i))){
lson[newroot] = idx++;
rson[newroot] = rson[root];
newroot = lson[newroot];
root = lson[root];
}else{
rson[newroot] = idx++;
lson[newroot] =lson[root];
newroot = rson[newroot];
root = rson[root];
}
val[newroot] = val[root] + ;
}
return tmp;
}
int query (int ua, int ub, int z, int d){
if (d == -){
return ;
}
int res = ;
if (z & ( << d)){
if (val[lson[ub]] - val[lson[ua]]){
res ^= << d;
res += query(lson[ua], lson[ub], z, d-);
}else{
res = query(rson[ua], rson[ub], z, d-);
}
}else{
if (val[rson[ub]] - val[rson[ua]]){
res ^= << d;
res += query(rson[ua], rson[ub],z, d-);
}else{
res = query(lson[ua], lson[ub], z, d-);
}
}
return res;
}
int parent[maxdep][maxn], dep[maxn];
void dfs(int u, int father){
parent[][u] = father;
dep[u] = dep[father] + ;
tree[u] = insert(tree[father], A[u]);
for (int v: G[u]){
if (v != father){
dfs(v, u);
}
}
}
void lca_init(int n){
dfs(, );
for (int k = ; k + < maxdep; k++){
for (int v = ; v <= n; v++){
if (parent[][v] <= ){
parent[k+][v] = -;
}else{
parent[k+][v] = parent[k][parent[k][v]];
}
}
}
}
int lca(int u, int v){
if (dep[u] > dep[v]){
swap(u, v);
}
for (int k = ; k < maxdep; k++){
if ((dep[v] - dep[u]) >> k & ){
v = parent[k][v];
}
}
if (u == v){
return u;
}
for (int k = maxdep-; k >= ; k--){
if (parent[k][u] != parent[k][v]){
u = parent[k][u];
v = parent[k][v];
}
}
return parent[][u];
}
void init(){
for (int i = ; i < maxn; i++){
G[i].clear();
}
idx = ;
memset(val, , sizeof (val));
}
void debug(int x, int i){
if (val[lson[x]]){
printf("%d,0 ", i);
debug(lson[x], i-);
} if (val[rson[x]]){
printf("%d,1 ", i);
debug(rson[x], i-);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, m;
while (~ scanf ("%d%d", &n, &m)){
init();
for (int i = ; i <= n; i++){
scanf ("%d", A+i);
}
for (int i = ; i < n-; i++){
int u, v;
scanf ("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(, );
lca_init(n);
for (int i = ; i < m; i++){
int u, v, c;
scanf ("%d%d%d", &u, &v, &c);
int tmp = parent[][lca(u, v)];
printf("%d\n", max(query(tree[tmp], tree[u], c, ), query(tree[tmp], tree[v], c, )));
}
}
return ;
}