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 ...
随机推荐
- swift版的元组
swift版的元组 说明 元组的内容并不多,使用的话跟普通变量类似,以下是测试源码: // // ViewController.swift // Tuples // // Created by You ...
- [翻译] USING GIT IN XCODE [3] 在XCODE中使用GIT[3]
USING GIT IN XCODE MAKING AND COMMITTING CHANGES Once you have a working copy of your project, it’s ...
- Linux 隐藏系统信息
Linux查看系统信息 [更多参考]https://www.cnblogs.com/ftl1012/p/uname.html Linux隐藏系统信息 查看: cat /etc/issue.net ...
- 最大公约数(GCD)与最小公倍数(LCM)的计算
给出两个数a.b,求最大公约数(GCD)与最小公倍数(LCM) 一.最大公约数(GCD) 最大公约数的递归: * 1.若a可以整除b,则最大公约数是b * 2.如果1不成立,最大公约数便是b ...
- zabbix日常监控Apache2.4
Apache的安装请参考https://www.cnblogs.com/huangyanqi/p/9168637.html 1.修改配置 [root@apache ~]# httpd -v Serve ...
- [2018HN省队集训D5T1] 沼泽地marshland
[2018HN省队集训D5T1] 沼泽地marshland 题意 给定一张 \(n\times n\) 的棋盘, 对于位置 \((x,y)\), 若 \(x+y\) 为奇数则可能有一个正权值. 你可以 ...
- Alpha 冲刺报告(5/10)
Alpha 冲刺报告(5/10) 队名:洛基小队 峻雄(组长) 已完成:修改角色的移动脚本 明日计划:完善此项脚本 剩余任务:角色的属性脚本 困难:没有时间,代码的编写时间太慢 ----------- ...
- POI导出excel,本地测试没问题,linux测试无法导出
java.lang.RuntimeException: java.io.IOException: No such file or directory at org.apache.poi. ...
- iOS 网络缓存总结
一.缓存策略: 1.缓存策略的配置: 缺省缓存策略的存储策略需要服务器的响应配置: 缺省缓存策略的使用需要请求端的配置: 2.缓存策略的缺陷: 移动端比较通用的缓存策略是先使用缓存同时更新本地数据: ...
- 线性dp
线性dp应该是dp中比较简单的一类,不过也有难的.(矩乘优化递推请出门右转) 线性dp一般是用前面的状态去推后面的,也有用后面往前面推的,这时候把循环顺序倒一倒就行了.如果有的题又要从前往后推又要从后 ...