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?

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

题目大意:一棵树,每一个节点有一个权值vi,有M组询问(u,x)表示求u的子树内某一个节点y,使得\(v_y xor x\) 最大

解题报告:对于子树询问,一般转化为区间求解,此题没有修改,直接上莫队,对于xor操作取max,显然这是trie树的基本操作,所以此题就是个trie树维护的莫队,指针的移动对应trie树中的插入和删除.

对于询问,我们在trie树中查询,我们尽量往和x相反的方向走即可

复杂度:\(O(n\sqrt{n}log_2n)\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=1e5+5,M=6e6+5,maxdep=30;
int gi(){
int str=0;char ch=getchar();
while(ch>'9' || ch<'0')ch=getchar();
while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
return str;
}
struct node{
int l,r,s;
}t[M];
int tot=0,w[35],root=0,n,Q,val[N],nxt[N<<1],to[N<<1],head[N];
int num=0,blc;
void link(int x,int y){
nxt[++num]=head[x];to[num]=y;head[x]=num;
}
void insert(int &rt,int x,int d,int to){
if(!rt)rt=++tot;
if(d==-1){
t[rt].s+=to;return ;
}
if(x&w[d])insert(t[rt].r,x,d-1,to);
else insert(t[rt].l,x,d-1,to);
t[rt].s=t[t[rt].l].s+t[t[rt].r].s;
}
int query(int &rt,int x,int d){
if(!rt || d==-1)return 0;
if(x&w[d]){
if(t[t[rt].l].s)return query(t[rt].l,x,d-1)+w[d];
return query(t[rt].r,x,d-1);
}
else{
if(t[t[rt].r].s)return query(t[rt].r,x,d-1)+w[d];
return query(t[rt].l,x,d-1);
}
}
int DFN=0,L[N],R[N],ans[N],dfn[N];
void dfs(int x,int last){
int u;L[x]=++DFN;dfn[DFN]=x;
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(u==last)continue;
dfs(u,x);
}
R[x]=DFN;
}
struct Ques{
int l,r,x,bs,id;
bool operator <(const Ques &pp)const{
if(bs!=pp.bs)return bs<pp.bs;
return r<pp.r;
}
}q[N];
void add(int i){
insert(root,val[dfn[i]],maxdep,1);
}
void delet(int i){
insert(root,val[dfn[i]],maxdep,-1);
}
void Clear(){
num=0;tot=0;DFN=0;root=0;memset(head,0,sizeof(head));
memset(t,0,sizeof(t));
}
void work()
{
Clear();
int x,y;
blc=sqrt(n)+1;
for(int i=1;i<=n;i++)val[i]=gi();
for(int i=2;i<=n;i++){
x=gi();
link(x,i);link(i,x);
}
dfs(1,1);
for(int i=1;i<=Q;i++){
x=gi();y=gi();
q[i].l=L[x];q[i].r=R[x];q[i].x=y;
q[i].bs=q[i].l/blc;q[i].id=i;
}
sort(q+1,q+Q+1);
int l=1,r=0;
for(int i=1;i<=Q;i++){
while(r<q[i].r)r++,add(r);
while(l>q[i].l)l--,add(l);
while(r>q[i].r)delet(r),r--;
while(l<q[i].l)delet(l),l++;
ans[q[i].id]=query(root,q[i].x,maxdep);
}
for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
} int main()
{
w[0]=1;for(int i=1;i<=maxdep;i++)w[i]=w[i-1]<<1;
while(~scanf("%d%d",&n,&Q))
work();
return 0;
}

2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree的更多相关文章

  1. 2017ACM/ICPC广西邀请赛-重现赛

    HDU 6188 Duizi and Shunzi 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 思路: 签到题,以前写的. 实现代码: #inc ...

  2. 2017ACM/ICPC广西邀请赛-重现赛1005 CS course

    2017-08-31 16:19:30 writer:pprp 这道题快要卡死我了,队友已经告诉我思路了,但是做题速度很缓慢,很费力,想必是因为之前 的训练都是面向题解编程的缘故吧,以后不能这样了,另 ...

  3. 2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

    上一场CF打到心态爆炸,这几天也没啥想干的 A Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  4. 2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi

    Problem Description Nike likes playing cards and makes a problem of it. Now give you n integers, ai( ...

  5. 2017ACM/ICPC广西邀请赛-重现赛 1004.Covering

    Problem Description Bob's school has a big playground, boys and girls always play games here after s ...

  6. 2017ACM/ICPC广西邀请赛-重现赛 1001 A Math Problem

    2017-08-31 16:48:00 writer:pprp 这个题比较容易,我用的是快速幂 写了一次就过了 题目如下: A Math Problem Time Limit: 2000/1000 M ...

  7. 2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  8. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  9. 2017ACM/ICPC广西邀请赛 1005 CS Course

    CS Course Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. python的dir、help、str用法

    当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表.当没有为其提供参数时, 它返回当前模块中定义的名字的列表.dir() 函数使用举例: 1 2 3 4 5 6 >>& ...

  2. Flask 学习 五 电子邮件

    pip install mail from flask_mail import Mail # 邮件配置 app.config['MAIL_SERVER']='smtp.qq.com' app.conf ...

  3. 【iOS】swift 74个Swift标准库函数

    本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swift standard li ...

  4. Linux 磁盘和文件管理系统 文件打包解压备份 VIM、VI编辑器

  5. python 中 reduce 函数的使用

    reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...

  6. aws - shadow 影子使用

    参考链接: https://github.com/aws/aws-iot-device-sdk-java 一.设备注册 二.设备主题

  7. iot:下一步要做的工作

    1.DeviceMessage抽象(定义&支持扩展)2.createDeviceMessage.analyseDeviceMessage(支持扩展)3.日志打印4.错误处理5.断线重连6.交互 ...

  8. RocketMQ(五):namesrv初探

    匠心零度 转载请注明原创出处,谢谢! RocketMQ网络部署图 NameServer:在系统中是做命名服务,更新和发现 broker服务. Broker-Master:broker 消息主机服务器. ...

  9. python当中的生成器

    最近身边的朋友都在问我迭代器是什么回事,经常跟大家一起讨论python的迭代器,一点点的我觉着自己有了更深一层的理解.我写下这篇文章,希望能对懵懵懂懂的好伙伴有些帮助~ 我也不是什么能人,难免说错一些 ...

  10. 1.7 理解dropout

    Dropout为什么有正则化的作用? 下面来直观理解一下. 上面讲到,dropout每次迭代都会让一部分神经元失活,这样使得神经网络会比原始的神经网络规模变小,因此采用一个较小神经网络好像和使用正则化 ...