2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree
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的更多相关文章
- 2017ACM/ICPC广西邀请赛-重现赛
HDU 6188 Duizi and Shunzi 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 思路: 签到题,以前写的. 实现代码: #inc ...
- 2017ACM/ICPC广西邀请赛-重现赛1005 CS course
2017-08-31 16:19:30 writer:pprp 这道题快要卡死我了,队友已经告诉我思路了,但是做题速度很缓慢,很费力,想必是因为之前 的训练都是面向题解编程的缘故吧,以后不能这样了,另 ...
- 2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
上一场CF打到心态爆炸,这几天也没啥想干的 A Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- 2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi
Problem Description Nike likes playing cards and makes a problem of it. Now give you n integers, ai( ...
- 2017ACM/ICPC广西邀请赛-重现赛 1004.Covering
Problem Description Bob's school has a big playground, boys and girls always play games here after s ...
- 2017ACM/ICPC广西邀请赛-重现赛 1001 A Math Problem
2017-08-31 16:48:00 writer:pprp 这个题比较容易,我用的是快速幂 写了一次就过了 题目如下: A Math Problem Time Limit: 2000/1000 M ...
- 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 ...
- HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序
题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...
- 2017ACM/ICPC广西邀请赛 1005 CS Course
CS Course Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- python 操作Memcached
启动Memcached memcached -d -m 10 -u root -l 10.211.55.4 -p 12000 -c 256 -P /tmp/memcached.pid 参数说明: -d ...
- 《高级软件测试》web测试实践--12月31日记录
今日的任务进度如上图所示.我们对华科软件学院和计算机学院的网站进行了对比分析,分析的角度包括基本功能分析.前端性能分析.用户调研等.在这里我们简单总结下我们得到的评测结果. 基本功能分析:计算机学院和 ...
- DML数据操作语言之增加,删除,更新
1.数据的增加 数据的增加要用到insert语句 ,基本格式是: insert into <表名> (列名1,列名2,列名3,......) values (值1,值2,值3,..... ...
- JAVA_SE基础——21.二维数组的定义
2 二维数组的定义 基本与一维数组类似 //定义一个3行5列的二维数组 //方法1,先new对象,然后再初始化每个元素 int[][] a = new int[3][5]; a[0][0]=1; a[ ...
- Win10下, TortoiseGit安装及配合Gitee使用完整版
Windows10下, TortoiseGit的安装及使用, 并配合Gitee码云使用! 1) 安装TortoiseGit 官网, 32位, 64位, 自选 https://tortoisegit.o ...
- Linq 巧用 Max,Sum
IList<, , , , , }; var sum1 = intList.Sum(s => { == ) { return s; } ; }); Console.WriteLine(&q ...
- kubernetes入门(09)kubernetes的命令
Help执行<kubectl>或<kubectl help> | <kubectl --help>获得命令的帮助信息.kubectl的帮助信息.示例相当详细,而且简 ...
- ELK学习总结(1-2)安装ElasticSearch
1.下载安装 Centos6.4 jdk1.8.20以上 elasticsearch::https://www.elastic.co/downloads/elasticsearch ...
- Spark入门(1-2)Spark的特点、生态系统和技术架构
一.Spark的特点 Spark特性 Spark通过在数据处理过程中成本更低的洗牌(Shuffle)方式,将MapReduce提升到一个更高的层次.利用内存数据存储和接近实时的处理能力,Spark比其 ...
- bad interpreter:No such file or directory 解决方法
今天在执行一个从网上考下来的脚本的时候,出现了下面的错误: Linux下面一个脚本死活也运行不了, 我检查了数遍,不可能有错. 提示:bad interpreter:No such file or d ...