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,⋯,Vn, indicating the value of node i.
The second line contains n-1 non-negative integers F1,F2,⋯Fn−1, Fi means the father of node i+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≤105
0≤Vi≤109
1≤Fi≤n, the root of the tree is node 1.
1≤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
和线段树合并类似,而且好写很多,本质上其实都是黑箱操作,把两个数据集合并就是了,数据结构的使用其实降低了很多思维难度。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxx 100005
using namespace std;
int head[maxx];
int to[maxx],_next[maxx];
int val[maxx];
int cnt;
void addEdge(int u,int v)
{
to[++cnt]=v,_next[cnt]=head[u],head[u]=cnt;
}
int n,q;
struct node
{
int ind;
int x;
node(){}
node(int _ind,int _x):ind(_ind),x(_x){}
};
vector<node>query[maxx];//询问关联的节点
struct Trie
{
int val;
Trie* next[2];
}*root[maxx];
void _insert(Trie* rt,int x)//字典树插入
{
for(int i=30;i>=0;i--)
{
int id=(x>>i)&1;
if(rt->next[id]==NULL)
{
rt->next[id]=new Trie;
rt->next[id]->next[0]=NULL;
rt->next[id]->next[1]=NULL;
}
rt=rt->next[id];
}
rt->val=x;
}
int _query(Trie* rt,int x)//字典树询问
{
for(int i=30;i>=0;i--)
{
int id=(x>>i)&1;
if(rt->next[id^1]!=NULL)
rt=rt->next[id^1];
else
rt=rt->next[id];
}
return rt->val^x;
}
Trie* _merge(Trie* a,Trie* b)//字典树合并,非常好写
{
if(a==NULL)return b;
if(b==NULL)return a;
a->next[0]=_merge(a->next[0],b->next[0]);
a->next[1]=_merge(a->next[1],b->next[1]);
delete(b);
return a;
}
int ans[maxx];
void dfs(int u)
{
root[u]=new Trie;
root[u]->next[0]=root[u]->next[1]=NULL;
_insert(root[u],val[u]);
for(int i=head[u];i;i=_next[i])
{
int v=to[i];
dfs(v);
root[u]=_merge(root[u],root[v]);
}
for(int i=0;i<query[u].size();i++)
{
node qq=query[u][i];
ans[qq.ind]=_query(root[u],qq.x);
}
}
void clean(Trie* rt)
{
if(rt->next[0]!=NULL)clean(rt->next[0]);
if(rt->next[1]!=NULL)clean(rt->next[1]);
delete(rt);
}
void init()
{
cnt=0;
memset(head,0,sizeof(head));
for(int i=1;i<=n;i++)
query[i].clear();
}
int main()
{
while(scanf("%d%d",&n,&q)==2)
{
init();
for(int i=1;i<=n;i++)
scanf("%d",val+i);
int u,x;
for(int i=2;i<=n;i++)
scanf("%d",&u),addEdge(u,i);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&u,&x);
query[u].push_back(node(i,x));
}
dfs(1);
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
clean(root[1]);
}
return 0;
}