Monkey A lives on a tree, he always plays on this tree.
One day, monkey A learned about one of the bit-operations, xor. He was keen of this interesting operation and wanted to practise it at once.
Monkey A gave a value to each node on the tree. And he was curious about a problem.
The problem is how large the xor result of number x and one node value of label y can be, when giving you a non-negative integer x and a node label u indicates that node y is in the subtree whose root is u(y can be equal to u).
Can you help him?
Input
There are no more than 6 test cases.
For each test case there are two positive integers n and q, indicate that the tree has n nodes and you need to answer q queries.
Then two lines follow.
The first line contains n non-negative integers V1,V2,⋯,VnV1,V2,⋯,Vn, indicating the value of node i.
The second line contains n-1 non-negative integers F1,F2,⋯Fn−1F1,F2,⋯Fn−1, FiFi means the father of node i+1i+1.
And then q lines follow.
In the i-th line, there are two integers u and x, indicating that the node you pick should be in the subtree of u, and x has been described in the problem.
2≤n,q≤1052≤n,q≤105
0≤Vi≤1090≤Vi≤109
1≤Fi≤n1≤Fi≤n, the root of the tree is node 1.
1≤u≤n,0≤x≤1091≤u≤n,0≤x≤109
Output
For each query, just print an integer in a line indicating the largest result.
Sample Input
2 2
1 2
1
1 3
2 1
Sample Output
2
3
题意 :给出一棵树,树上每个点有点权,每次询问给出u x,求以u为根的子树中点权和x异或得到的最大值
分析: 可以先把这个题写了
然后把树用DFS序处理之后也就是,区间查询了。
代码
#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 root[MAXN*35],sum[MAXN*35],son[MAXN*35][2],sz;
void init(){
memset(root,0,sizeof(root));
memset(sum,0,sizeof(sum));
memset(son,0,sizeof(son));
sz=0;
}
int Insert(int val,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&(val>>i);
son[x][j]=++sz;
x=son[x][j],pre=son[pre][j];
}
sum[x]=sum[pre]+1;
return t;
}
int Query(int val,int le,int ri){
int ans=0;
for(int i=31;i>=0;i--){
int j=1&(val>>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 cost[MAXN],in[MAXN],out[MAXN],id;
vector<int>ve[MAXN];
void dfs(int now,int pre){ // 得到DFS序
in[now]=id++; root[in[now]]= Insert( cost[now], root[in[now]-1]);
for(int i=0;i<ve[now].size();i++){
if(ve[now][i]==pre) continue;
dfs(ve[now][i],now);
}
out[now]=id-1;
}
int main(){
int n,q;
while(scanf("%d%d",&n,&q)!=EOF){
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
init();
for(int i=1;i<=n;i++) { scanf("%d",&cost[i]); ve[i].clear(); }
for(int i=1;i<=n-1;i++) {
int a;scanf("%d",&a);
ve[a].push_back(i+1);
}
id=1; dfs(1,-1);
// for(int i=1;i<=n;i++) {
// printf("in %d out %d \n",in[i],out[i]);
// }
while(q--){
int u,x;scanf("%d%d",&u,&x);
printf("%d\n", Query(x,root[in[u]-1],root[out[u]]));
}
}
return 0;
}