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 ...
 
随机推荐
- 2017-2018-1 我爱学Java 第二周 作业
			
Android Game Discussion Questions Answers 20162309邢天岳 20162311张之睿 20162312张家铖 20162313苑洪铭 20162324春旺 ...
 - PostgreSQL 配置安装
			
Mac 安装 http://postgresapp.com/ 创建和删除数据库用户 对应命令如下(在postgres=# 环境下):1.查看数据库用户列表: \du2.创建数据库用户: create ...
 - Flask学习 一 基本结构
			
-from flask import Flask +from flask import Flask,render_template -from flask import request -from f ...
 - GPUImage滤镜效果翻译
			
#import"GPUImageBrightnessFilter.h"//亮度 #import"GPUImageExposureFilter.h"//曝光 #i ...
 - poj 2142 The Balance
			
The Balance http://poj.org/problem?id=2142 Time Limit: 5000MS Memory Limit: 65536K Descripti ...
 - 易错点---所有的字符都自带bool值
			
所有的字符都自带布尔值,只有0,None,空为False,其他全部为真!!!!!!!!!!! count = 0 while count < 3 : inp_age =input('Enter ...
 - JAVA_SE基础——31.this关键字
			
黑马程序员入学blog... 也算是学习笔记体会. this的通俗解释: 有一个A类,一个B方法,一个C变量,其中B和C都在类A中 this.B()就是调用A类中的B方法 this.C=1(假设C是一 ...
 - Python之旅_第一章Python入门
			
一.编程语言分类 1.机器语言:即计算机能听懂的二进制语言,0000 0001,直接操控硬件: 2.汇编语言:简写的英文标识符代替二进制语言,本质同样是直接操控硬件: 3.高级语言:用更贴近人类的语言 ...
 - JMeter入门(01)概念和样例
			
一.概念 JMeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台,实现了许多和互联网相关的网络测试组件,同时还保留着很强的扩展性. JMeter可以用来测试诸如:静态文件,Java Ser ...
 - gradle入门(1-4)多项目构建实战
			
一.多项目构建 1.多项目构建概念 尽管我们可以仅使用单个组件来创建可工作的应用程序,但有时候更广泛的做法是将应用程序划分为多个更小的模块. 因为这是一个非常普遍的需求,因此每个成熟的构建工具都必须支 ...