Zero and One are good friends who always have fun with each other. This time, they decide to do something on a tree which is a kind of graph that there is only one path from node to node. First, Zero will give One an tree and every node in this tree has a value. Then, Zero will ask One a series of queries. Each query contains three parameters: x, y, z which mean that he want to know the maximum value produced by z xor each value on the path from node x to node y (include node x, node y). Unfortunately, One has no idea in this question. So he need you to solve it.
Input
There are several test cases and the cases end with EOF. For each case:
The first line contains two integers n(1<=n<=10^5) and m(1<=m<=10^5), which are the amount of tree’s nodes and queries, respectively.
The second line contains n integers a[1..n] and ai is the value on the ith node.
The next n–1 lines contains two integers u v, which means there is an connection between u and v.
The next m lines contains three integers x y z, which are the parameters of Zero’s query.
Output
For each query, output the answer.
Sample Input
3 2
1 2 2
1 2
2 3
1 3 1
2 3 2
Sample Output
3
0
题意 给出一棵树,树上的点都有权值,每次给出一组询问x y z,求从x到y路径上的点权值和z异或得到的最大值
分析: 我们建一个 子节点继承父节点的01Trie ,然后对于每一组查询x y z ,我们先求出lca(x,y) ,然后再求出 fa(lca),之后分别求出两段区间 ( fa(lca) , x ) ( fa(lca) , y ) 的最大值,然后再取最大值。
代码
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)
const int MAXN = 1e5+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
int son[MAXN*35][2],sum[MAXN*35],root[MAXN*35],sz;
int Insert(int a,int pre){
int x=++sz,t=x;
for(int i=31;i>=0;i--){
son[x][1]=son[pre][1],son[x][0]=son[pre][0];
sum[x]=sum[pre]+1;
int j=1&(a>>i);
son[x][j]=++sz;
x=son[x][j],pre=son[pre][j];
}
sum[x]=sum[pre]+1;
return t;
}
int Query(int a,int le,int ri){
int ans=0;
for(int i=31;i>=0;i--){
int j=1&(a>>i); j=!j;
if(sum[son[ri][j]]-sum[son[le][j]]>0) {
ans|=(1<<i);
le=son[le][j],ri=son[ri][j];
}else
le=son[le][!j],ri=son[ri][!j];
}
return ans;
}
int n,m;
vector<int>ve[MAXN];
int p[MAXN][25],dep[MAXN],arr[MAXN];
void dfs(int now,int pre,int de){
dep[now]=de; p[now][0]=pre;
root[now]=Insert(arr[now],root[pre]);
for(int i=0;i<ve[now].size();i++){
int v=ve[now][i];
if(v==pre) continue;
dfs(v,now,dep[now]+1);
}
}
void lca_init(){
for(int j=1;(1<<j)<=n;j++) {
for(int i=1;i<=n;i++)
p[i][j]=p[p[i][j-1]][j-1];
}
}
int LCA(int v,int u){
if(dep[v]<dep[u]) swap(v,u);
int d=dep[v]-dep[u];
for(int i=0;(d>>i)!=0;i++)
if((d>>i)&1) v = p[v][i];
if(v==u) return v;
for(int i=20;i>=0;i--)
if(p[v][i]!=p[u][i]) v=p[v][i],u=p[u][i];
return p[v][0];
}
int main(){
CLOSE();
// fread();
// fwrite();
while(scanf("%d%d",&n,&m)!=EOF){
sz=0;
for(int i=1;i<=n;i++) ve[i].clear();
for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
ve[a].push_back(b);
ve[b].push_back(a);
}
dfs(1,0,0); lca_init();
while(m--){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
int lca=LCA(x,y); lca=p[lca][0];
// printf("lca %d\n",lca);
int ans=Query(z,root[lca],root[x]);
// printf("ans %d\n",ans);
ans=max(ans,Query(z,root[lca],root[y]));
printf("%d\n",ans);
}
}
return 0;
}