HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序
题意
给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少
分析
先dfs一遍,得到dfs序,就可以将这个问题转化为求区间\([l,r]\)中的值与\(x\)异或值最大的经典问题,
就按dfs序建可持久化01字典树,查询的时候查区间\([in[u],out[u]]\)就行了,\(in[u]\)和\(out[u]\)存的分别是\(u\)的子树上的节点在dfs序上的起始位置和结尾位置。
Code
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=1e9;
const ll mod=1e9+7;
const int maxn=1e5+10;
int n,q;
int v[maxn];
vector<int>f[maxn];
int in[maxn],out[maxn];
int r;
int a[maxn];
int root[maxn];
int son[maxn*40][2],tot;
int sum[maxn*40];
void dfs(int u,int fa){
in[u]=++r;a[r]=u;
for(int x:f[u]){
if(x==fa) continue;
dfs(x,u);
}
out[u]=r;
}
int insert(int val,int pre){
int x=++tot,pos=x;
for(int i=30;i>=0;i--){
son[x][0]=son[pre][0];
son[x][1]=son[pre][1];
int j=((val>>i)&1);
son[x][j]=++tot;
x=son[x][j];pre=son[pre][j];
sum[x]=sum[pre]+1;
}
return pos;
}
int query(int val,int l,int r){
int ans=0;
for(int i=30;i>=0;i--){
int j=((val>>i)&1);j=!j;
if(sum[son[r][j]]-sum[son[l][j]]>0){
ans|=(1<<i);
}else{
j=!j;
}
l=son[l][j];r=son[r][j];
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n>>q){
memset(son,0,sizeof(son));
memset(sum,0,sizeof(sum));
memset(root,0,sizeof(root));
memset(a,0,sizeof(a));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
r=tot=0;
for(int i=1;i<=n;i++){
cin>>v[i];
f[i].clear();
}
for(int i=1,x;i<n;i++){
cin>>x;
f[x].push_back(i+1);
}
dfs(1,0);
for(int i=1;i<=n;i++){
root[i]=insert(v[a[i]],root[i-1]);
}
while(q--){
int u,x;
cin>>u>>x;
int ans=query(x,root[in[u]-1],root[out[u]]);
cout<<ans<<endl;
}
}
return 0;
}
HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序的更多相关文章
- 2017ACM/ICPC广西邀请赛-重现赛
HDU 6188 Duizi and Shunzi 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 思路: 签到题,以前写的. 实现代码: #inc ...
- 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 ...
- 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 learne ...
- 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广西邀请赛-重现赛 1004.Covering
Problem Description Bob's school has a big playground, boys and girls always play games here after s ...
- 2017ACM/ICPC广西邀请赛
A.A Math Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll ...
- 2017ACM/ICPC广西邀请赛 A Math Problem
解法:发现..最多15个,那么..暴力一下啦 #include <iostream> #include <stdio.h> #include <vector> #i ...
- 2017ACM/ICPC广西邀请赛 Color it
Color it Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Tota ...
随机推荐
- 转:只能选择GridView中的一个CheckBox(单选CheckBox)
方法1: protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e){CheckBox cbx = e.R ...
- Python入门-模块2(sys模块、shutil 模块)
sys模块: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 s ...
- Redis学习---Redis操作之List
List操作,redis中的List在在内存中按照一个name对应一个List来存储 lpush(name,values) --> 实际上是左添加 # 在name对应的list中添加元素,每个新 ...
- 铁乐学python_day23_面向对象进阶1_反射
铁乐学python_day23_面向对象进阶1_反射 以下内容大部分摘自博客http://www.cnblogs.com/Eva-J/ isinstance()和issubclass() 两者的返回值 ...
- Hadoop HA on Yarn——集群配置
集群搭建 因为服务器数量有限,这里服务器开启的进程有点多: 机器名 安装软件 运行进程 hadoop001 Hadoop,Zookeeper NameNode, DFSZKFailoverContro ...
- css3实现 依次出现三个点(一般用于提示加载中。。。 提交中。。。)
<a href="javascript:" class="login">登录中<span class="dotting"& ...
- JS应该放在什么位置?
(1)放在底部,虽然放在底部照样会阻塞所有呈现,但不会阻塞资源下载 (2)如果嵌入JS放在head中,请把嵌入JS放在CSS头部 (3)使用defer(只支持IE) (4)不要在嵌入的JS中调用运行时 ...
- docker 导入导出镜像
docker容器导入导出有两种方法: 一种是使用save和load命令 使用例子如下: docker save ubuntu:load>/root/ubuntu.tar docker load& ...
- 文件上传之MultipartFile使用
转载 文件断点上传,html5实现前端,java实现服务器 一.单/多文件上传使用例子: 工程路径如下 -src |--main.java --controller --service ...
- Java反射学习一
Java 反射机制 基本概念 在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法? 答案是肯定的. 这种动态获取类的信息以及动态调用对象 ...