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 ...
随机推荐
- [翻译] DKTagCloudView - 标签云View
DKTagCloudView 效果(支持点击view触发事件): Overview DKTagCloudView is a tag clouds view on iOS. It can generat ...
- Linux 系统的网络基础_【all】
网络基础 1.网线:568B: 白橙 橙色 白绿 蓝色 白蓝 绿色 白棕 棕色 2.交换机:电信号转发的网络设备,它可以为接入交换机的任2个网络节点设备提供电信号通信 3.路由器:连接局域网,广域网的 ...
- Composer 的简介、安装及使用
Composer的简介 简单说,Composer 就是一个安装包管理工具,服务于 PHP 生态系统.它包括了两个部分:Composer 和 Packagist. Composer Composer 是 ...
- Atom 绝赞插件
文件图标: file-icons 根据不同文件后缀名显示不同类型图标 标签栏根据不同文件格式显示色彩: filetype-color 在标签栏不同格式文件显示不同的颜色的标题,支持二度设置. 小地图: ...
- C++实现一个Vector3空间向量类(转)
转自:http://www.2cto.com/kf/201311/260139.html ector2,3,4类在DirectX中都有现成的可以调用,不过要实现其中的功能其实也不难,也都是一些简单的数 ...
- SDN 第一次上机作业
第一题 拓扑: 测试连通性: 第二题 拓扑: 测试连通性: 第三题 拓扑: 测试连通性:
- Spring 利用PropertyPlaceholderConfigurer占位符
Hey Girl 博客园 首页 博问 闪存 新随笔 订阅 管理 posts - 42, comments - 3, trackbacks - 0 Sp ...
- 使用 CSS 根据兄弟元素的个数来调整样式
在某些场景下,我们需要根据兄弟元素的总数来为它们设置样式.最常见的场景就是,当一个列表不断延长时,通过隐藏控件或压缩控件等方式来节省屏幕空间,以此提升用户体验. 为保证一屏内容能展示更多的内容,需要将 ...
- 如何动态调用 C 函数
JSPatch 支持了动态调用 C 函数,无需在编译前桥接每个要调用的 C 函数,只需要在 JS 里调用前声明下这个函数,就可以直接调用: require('JPEngine').addExtensi ...
- BZOJ3052:[WC2013]糖果公园(树上莫队)
Description Input Output Sample Input 4 3 51 9 27 6 5 12 33 13 41 2 3 21 1 21 4 20 2 11 1 21 4 2 Sam ...