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 ...
随机推荐
- Oracle 补丁那些事儿(PS、PSU、CPU、SPU、BP、DBBP…)
当前ORACLE数据库提供两种方式的补丁一种是主动的Proactive Patches和另一种被动的Reactive Patches,其中Reactive Patches是指过去的ONE-OFF Pa ...
- 模板(template)包含与继承
Django 模板查找机制: Django 查找模板的过程是在每个 app 的 templates 文件夹中找(而不只是当前 app 中的代码只在当前的 app 的 templates 文件夹中找). ...
- win10 虚拟机 hyper-v 安装 centos 7
一.win 10 企业版 自带虚拟机 hyper-v 1.控制面板-->程序和功能-->启用或关闭Windows功能 勾上 hyper-v 确定就ok了 2.安装成功后会发现在 左下角“开 ...
- Sharepoint 2013/2010 登陆身份验证
SharePoint 2013 and SharePoint 2010登陆身份验证格式: <IdentityClaim>:0<ClaimType><ClaimValueT ...
- 【教程】【FLEX】#006 控件位置的拖动
上一篇笔记学习了怎么从 把一个控件拖放到另外一个控件里面(即 A --> B里面). 而现在呢,则是学习 怎么把 A 拖到另外一个位置. (A -->A位置改变): 先说一下实现的思路( ...
- HDFS下载数据机制的底层分析
HDFS下载数据机制的底层分析 Hadoop中的RPC(Remote Procedure Call)框架 hadoop中结点间的通信采用的是RPC. RPC框架的实现机制图解: 从hdfs下载数据的源 ...
- Nodejs Redis 全部操作方法
安装 npm install redis --save demo var redis = require('redis'); var client = redis.createClient('637 ...
- 【Jenkins持续集成】好用的插件集合
1. Promoted Builds Plugin 这个插件在job构建成功后,依据设置条件(仅手动执行/成功时执行等),执行操作(操作和构建过程基本类似),这样我们就可以在构建之后有机会执行拉分支. ...
- 【转载】uWSGI配置翻译
英文原版: http://uwsgi-docs.readthedocs.io/en/latest/Options.html 转载地址: http://www.cnblogs.com/zhouej/ar ...
- Python中网络编程对 listen 函数的理解
listen函数的第一个参数时SOCKET类型的,该函数的作用是在这个SOCKET句柄上建立监听,至于有没有客户端连接进来,就需要accept函数去进行检查了,accept函数的第一个参数也是SOCK ...