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 ...
随机推荐
- MySQL存储引擎之Spider内核深度解析
作者介绍 朱阅岸,中国人民大学博士,现供职于腾讯云数据库团队.研究方向主要为数据库系统理论与实现.新硬件平台下的数据库系统以及TP+AP型混合系统. Spider是为MySQL/MariaDB开发 ...
- 教你使用Paw解析http请求
教你使用Paw解析http请求 软件下载地址: 链接: http://pan.baidu.com/s/1gdzmjq7 密码: 3mpb 这款应用的图片像极了百度,哈哈. 支持的请求方式: 完整的显示 ...
- MFC连接postgre数据库
配置环境: include:libpq-fe.h文件 lib:libpq.lib文件 dll:libeay32.dll,libiconv-2.dll,libintl-8.dll,libpq.dll,s ...
- Python学习---模拟微信网页登录180410
WEB微信 网页登录的猜想: a. 访问页面出现二维码 b. 长轮询监听是否已经扫码并且点击确认 c. 如何进行会话保持 d. 如何获取用户列表 e. 如何发送消息(接收消息) 过程:访问微信官网[h ...
- 铁乐学Python_day12_装饰器
[函数的有用信息] 例: def login(user, pwd): ''' 功能:登录调用 参数:分别有user和pwd,作用分别是用户和密码: return: 返回值是登录成功与否(True,Fa ...
- [Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform
[Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform 题意 给定一个小写字母构成的字符串, 每个字符有一个非负权值. 输出所有满足权值和等于这个子串在所有本质 ...
- PAT 1001A+B Format
Github 1001 题目速览 1.解题的思路过程 认真读题,题目为A+BFormat,简单的计算a+b问题,特殊在于输出的形式. 输入形式为每个输入文件包含一个测试样例,每个测试样例仅包含一对整型 ...
- mysql用户创建及授权
一. 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...
- weblogic之CVE-2018-3191漏洞分析
weblogic之CVE-2018-3191漏洞分析 理解这个漏洞首先需要看这篇文章:https://www.cnblogs.com/afanti/p/10193169.html 引用廖新喜说的,说白 ...
- Hadoop学习之路(十七)MapReduce框架Partitoner分区
Partitioner分区类的作用是什么? 在进行MapReduce计算时,有时候需要把最终的输出数据分到不同的文件中,比如按照省份划分的话,需要把同一省份的数据放到一个文件中:按照性别划分的话,需要 ...