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 ...
随机推荐
- io多路复用(三)
#!/usr/bin/env python # -*- coding:utf-8 -*- import socket sk1 = socket.socket() sk1.bind(('127.0.0. ...
- MySQL InnoDB锁机制
概述: 锁机制在程序中是最常用的机制之一,当一个程序需要多线程并行访问同一资源时,为了避免一致性问题,通常采用锁机制来处理.在数据库的操作中也有相同的问题,当两个线程同时对一条数据进行操作,为了保证数 ...
- JAVA_SE基础——20.数组的常见操作
1.遍历数组 使用for循环来遍历数组 代码如下: public class Ergodic { public static void main(String[] args) { int[] arr ...
- python 内置函数之lambda-filter-reduce-apply-map
(1)lambda lambda是Python中一个很有用的语法,它允许你快速定义单行最小函数.类似于C语言中的宏,可以用在任何需要函数的地方. 基本语法如下: 函数名 = lambda args1, ...
- linux下的Shell编程(8)自定义函数
Shell Script中也可以使用自定义的函数,其语法形式如下: functionname() { - }
- Linux下的Shell编程(1)最简单的例子
深入地了解和熟练地掌握Shell编程,是每一个Linux用户的必修 功课之一. 从第一行开始 我们可以使用任意一种文字编辑器编写shell脚本,它必须以如下行开始(必须放在文件的第一行): #!/bi ...
- hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(2)安装hadoop
一.依赖安装 安装JDK 二.文件准备 hadoop-2.7.3.tar.gz 2.2 下载地址 http://hadoop.apache.org/releases.html 三.工具准备 3.1 X ...
- Excel 日期截取(函数)
需求:时间段截取,去掉年月日,保留时分. 实现函数: =TEXT(A2,"HH:MM")&"-"&TEXT(B2,"HH:MM& ...
- 19届华为实习生笔试之判断iPv6地址类型
题二: 答案: #coding=utf-8 import re,sys str = sys.stdin.readline().strip() def regex(str): result = &quo ...
- Java 高级开发必修知识---反射
Class类的使用 1) 在面向对象的世界里,万事万物皆对象 A. Java语言中,普通数据类型,静态成员不是对象,其他皆对象 B. 每一个类也是对象 C. 类是java.lang.Class类的实例 ...