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?
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
題意:給你一棵樹,每次詢問一個數與結點u的子樹上的結點的值異或的最大值。
要查詢一組數和一個數異或的最大值,很輕易可以看出是字典樹。(01字典樹)
然而每次要查詢的是以某一個結點爲根的子樹上的數值的異或最大值,當然不能建n棵以每一個結點爲根的字典樹,所以我們用到了啓發式合併,這樣我們先查詢後合併,就可以得到所有查詢的答案。
像字典樹線段樹這種樹結構,如果兩顆樹a,b的結構相同,那麼他們就可以合併。(線段樹的啓發式合併)
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
void tran(char *a,int x)
{
int i;
for(i=0;i<=31;i++)
a[i] = '0';
a[i] ='\0';
i = 0;
while(x)
{
a[i++] = (x%2)+'0';
x /= 2;
}
}
struct trie
{
trie *next[2];
int cnt;
trie()
{
memset(next,0,sizeof(next));
cnt = 0;
}
};
trie *root[maxn];
void insert(trie *p,int x)
{
int i,k;
char s[33];
tran(s,x);
for(i=31;i>=0;i--)
{
k=s[i]-'0';
if(p->next[k]==NULL)
p->next[k]=new trie();
p=p->next[k];
}
p->cnt = x;
}
int query(trie *p,int x,int dir)//dir爲1就是往相反的方向走,也就是求最大值
{
char s[33];
tran(s,x);
for(int i=31;i>=0;i--)
{
int x = s[i] - '0';
x = x^dir;
if(p->next[x] == NULL)
x^=1;
p = p->next[x];
}
return (p->cnt) ^ x;
}
void del(trie *p)
{
for(int i=0;i<2;i++)
if(p->next[i]!=NULL)
del(p->next[i]);
free(p);
}
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]);
free(b);
return a;
}
struct Query
{
int id,x;
Query(int _id,int _x)
{
id = _id;
x = _x;
}
};
int a[maxn];
vector<int> e[maxn];
vector<Query> q[maxn];
int ans[maxn];
void dfs(int x,int pre)
{
root[x] = new trie();
insert(root[x],a[x]);
for(int i=0;i<e[x].size();i++)
{
int xx = e[x][i];
if(xx == pre)
continue;
dfs(xx,x);
Merge(root[x],root[xx]);
}
for(int i=0;i<q[x].size();i++)
{
ans[q[x][i].id] = query(root[x],q[x][i].x,1);
}
}
int main(void)
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)==2)
{
for(i=1;i<=n;i++)
{
e[i].clear();
q[i].clear();
scanf("%d",&a[i]);
}
for(i=2;i<=n;i++)
{
int x;
scanf("%d",&x);
e[x].push_back(i);
}
for(i=1;i<=m;i++)
{
int u,x;
scanf("%d%d",&u,&x);
q[u].push_back(Query(i,x));
}
dfs(1,-1);
for(i=1;i<=m;i++)
printf("%d\n",ans[i]);
del(root[1]);
}
return 0;
}