Query on A Tree
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 298 Accepted Submission(s): 116
Problem Description
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?
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
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<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <set>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
struct node
{
int x,id;
};
typedef struct trie
{
trie* next[2];
}trie;
int a[N], ans[N];
vector<node> q[N];
vector<int>p[N];
trie *root[N];
void update(trie *x,int vx)
{
trie *px=x;
for(int i=29;i>=0;i--)
{
int v=(vx&(1<<i))?1:0;
if(px->next[v]==NULL)
{
trie *tmp=new trie;
tmp->next[0]=tmp->next[1]=NULL;
px->next[v]=tmp;
}
px=px->next[v];
}
return ;
}
trie* merge1(trie *px,trie *qx)
{
if(px==NULL) return qx;
if(qx==NULL) return px;
px->next[0]=merge1(px->next[0],qx->next[0]);
px->next[1]=merge1(px->next[1],qx->next[1]);
free(qx);//释放q不然会超内存
return px;
}
int get(trie *x,int vx)
{
trie *px=x;
int r=0;
for(int i=29;i>=0;i--)
{
int v=(vx&(1<<i))?1:0;
if(px->next[1^v]!=NULL)
{
r|=(1<<i);
px=px->next[1^v];
}
else px=px->next[v];
}
return r;
}
void dfs(int u)
{
root[u]=new trie;
root[u]->next[0]=root[u]->next[1]=NULL;
update(root[u],a[u]);
for(int i=0;i<p[u].size();i++)
{
int v=p[u][i];
dfs(v);
root[u]=merge1(root[u],root[v]);
}
for(int i=0;i<q[u].size();i++)
{
node tmp=q[u][i];
int h=get(root[u], tmp.x);
ans[tmp.id]=h;
}
return ;
}
void delete1(trie *x)
{
if(x->next[0]!=NULL) delete1(x->next[0]);
if(x->next[1]!=NULL) delete1(x->next[1]);
free(x);
return ;
}
int main()
{
int n, m, x;
while(scanf("%d %d", &n, &m)!=EOF)
{
for(int i=1;i<=n;i++) scanf("%d", &a[i]),p[i].clear(),q[i].clear();
for(int i=2;i<=n;i++)
{
scanf("%d", &x);
p[x].push_back(i);
}
for(int i=1;i<=m;i++)
{
int x, y;
scanf("%d %d", &x, &y);
node tmp;
tmp.id=i,tmp.x=y;
q[x].push_back(tmp);
}
dfs(1);
for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
delete1(root[1]);//释放内存
}
return 0;
}
因为没有办法直接用字典树,所以给每一个节点进和出一个标号 然后就可以套模板了(中间变量名写错了,wa一上午,如果代码能力不稳的话,比赛时还是有一个人看着稳一点)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <set>
#include <bits/stdc++.h>
using namespace std;
const int N = 260005;
typedef long long LL;
int head[N];
int a[N];
struct node
{
int to,next;
}p[N*2];
int cnt, num, tot;
LL bit[40];
int in[N], out[N], son[N*32][2], sum[N*32], rt[N];
void init()
{
memset(head,-1,sizeof(head));
memset(son,0,sizeof(son));
memset(sum,0,sizeof(sum));
cnt=0,num=0,tot=0;
return ;
}
void add(int u,int v)
{
p[cnt].to=v,p[cnt].next=head[u];
head[u]=cnt++;
return ;
}
void insert1(int &o,int pre,int x,int len)
{
o=++tot;
son[o][0]=son[pre][0], son[o][1]=son[o][1];
sum[o]=sum[pre]+1;
if(len==-1) return ;
int v=(x&(bit[len]))?1:0;
insert1(son[o][v],son[pre][v],x,len-1);
}
void dfs(int u,int fa)
{
in[u]=++num;
insert1(rt[in[u]],rt[in[u]-1],a[u],30);
for(int i=head[u];i!=-1;i=p[i].next)
{
int v=p[i].to;
if(v==fa) continue;
dfs(v,u);
}
out[u]=num;
}
int query(int ss,int tt,int x,int len)
{
if(len==-1) return 0;
int v=((x>>len)&1)?1:0;
if(sum[son[tt][1^v]]>sum[son[ss][1^v]])
{
return bit[len]+query(son[ss][1^v],son[tt][1^v],x,len-1);
}
return query(son[ss][v],son[tt][v],x,len-1);
}
int main()
{
bit[0]=1;
for(int i=1;i<=30;i++) bit[i]=bit[i-1]*2;
int n, m;
while(scanf("%d %d", &n, &m)!=EOF)
{
init();
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
for(int i=2;i<=n;i++)
{
int x;
scanf("%d", &x);
add(x,i);
}
dfs(1,0);
for(int i=1;i<=m;i++)
{
int u, x;
scanf("%d %d", &u, &x);
printf("%d\n",query(rt[in[u]-1],rt[out[u]],x,30));
}
}
return 0;
}