hdu 6191--Query on A Tree(持久化字典树)
One day, monkey A learned about one of the bit-operations, xor. He was keen of this interesting operation and wanted to practise it at once.
Monkey A gave a value to each node on the tree. And he was curious about a problem.
The problem is how large the xor result of number x and one node value of label y can be, when giving you a non-negative integer x and a node label u indicates that node y is in the subtree whose root is u(y can be equal to u).
Can you help him?
For each test case there are two positive integers n and q, indicate that the tree has n nodes and you need to answer q queries.
Then two lines follow.
The first line contains n non-negative integers V1,V2,⋯,Vn, indicating the value of node i.
The second line contains n-1 non-negative integers F1,F2,⋯Fn−1, Fi means the father of node i+1.
And then q lines follow.
In the i-th line, there are two integers u and x, indicating that the node you pick should be in the subtree of u, and x has been described in the problem.
2≤n,q≤105
0≤Vi≤109
1≤Fi≤n, the root of the tree is node 1.
1≤u≤n,0≤x≤109
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N=1e5+;
int a[N];
struct Node
{
int son[];
int sum[];
}node[*N];
vector<int>G[N];
int la[N],to[N],root[N];
int tot1,tot2; void init()
{
node[].son[]=node[].son[]=;
node[].sum[]=node[].sum[]=;
root[]=;
tot1=tot2=;
for(int i=;i<N;i++) G[i].clear();
}
void build(int pre,int now,int x,int deep)
{
if(deep<) return ;
int tmp=!!(x&(<<deep));
node[now]=node[pre];
node[now].sum[tmp]++;
build(node[pre].son[tmp],node[now].son[tmp]=++tot2,x,deep-);
}
void dfs(int now)
{
la[now]=++tot1;
build(root[la[now]-],root[la[now]]=++tot2,a[now],);
for(int i=;i<G[now].size();i++)
{
int v=G[now][i];
dfs(v);
}
to[now]=tot1;
}
int query(int pre,int now,int sum,int x,int deep)
{
if(deep<) return sum;
int tmp=!!(x&(<<deep));
if(node[now].sum[tmp^]>node[pre].sum[tmp^])
return query(node[pre].son[tmp^],node[now].son[tmp^],sum|(<<deep),x,deep-);
return query(node[pre].son[tmp],node[now].son[tmp],sum,x,deep-);
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF)
{
init();
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
int x; scanf("%d",&x);
G[x].push_back(i);
}
dfs();
while(q--)
{
int u,x; scanf("%d%d",&u,&x);
printf("%d\n",query(root[la[u]-],root[to[u]],,x,));
}
}
return ;
}
hdu 6191--Query on A Tree(持久化字典树)的更多相关文章
- HDU 6191 Query on A Tree(字典树+离线)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- HDU - 6191 Query on A Tree (可持久化字典树/字典树合并)
题目链接 题意:有一棵树,树根为1,树上的每个结点都有一个数字x.给出Q组询问,每组询问有两个值u,x,代表询问以结点u为根的子树中的某一个数与x的最大异或值. 解法一:dfs序+可持久化字典树.看到 ...
- HDU 6191 Query on A Tree(可持久化Trie+DFS序)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- [hdu 6191] Query on A Tree
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- HDU6191 Query on A Tree (01字典树+启发式合并)
题意: 给你一棵1e5的有根树,每个节点有点权,1e5个询问(u,x),问你子树u中与x异或最大的值是多少 思路: 自下而上启发式合并01字典树,注意合并时清空trie 线段树.字典树这种结构确定的数 ...
- HDU 6191 Query on A Tree(可持久化Trie)
题意 \(n\) 个点的有根树,根为 \(1\) .每个点有点权,有 \(q\) 个询问,每次询问以 \(u\) 为根的子树的点的点权中异或 \(x\) 所得的最大值是多少. 思路 求出整棵树的 \( ...
- HDU 6191 Query on A Tree ( 2017广西邀请赛 && 可持久化Trie )
题目链接 题意 : 给你一棵树.树上的每个点都有点权.之后有若干次问询.每次问询给出一个节点编号以及一个整数 X .问你以给出节点为根的子树中哪个节点和 X 异或最大.输出这个值 分析 : 看到这种树 ...
- 【HDU 6191】Query on A Tree 【可持久化字典树】
题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这 ...
- HDU 4757 Tree 可持久化字典树
Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...
- Hdu-4757 Tree(可持久化字典树+lca)
题目链接:点这 我的github地址:点这 Problem Description Zero and One are good friends who always have fun wi ...
随机推荐
- solr集群的理解和配置(待更新)
solr部署在tomcat下,solr集群依赖tomcat集群和zookeeper集群: zookeeper:1.对象注册和发放中心,实现异步调用. 2.配置中心.(solrConfig.xml,sc ...
- iOS SDAutoLayout图文混排-共享
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #526eda } span.s1 { color: #4dbf5 ...
- SpringMVC随笔记录
在web.xml里可以配置webapp的默认首页,格式如下: <welcome-file-list> <welcome-file>index.html</welcome- ...
- 用C#写入Excel表并保存
想用C#操作Excel表,首先要做一些准备工作. 如果要操作 microsoft office Excel 2003表,就需要引入Microsoft office 11.0 object librar ...
- java获取当前应用的运行信息(内存,线程,运行时间,状态等)
一:目的 写这一段程序的原因是需要监控部署的的应用是否正常运行,并且显示其运行状态.在进程莫名死掉后甚至可以自动启动该应用. 首先这段代码可以获取的信息如下 /** * 当前进程运行的主机名 */ p ...
- 调用CMD命令的一个.NET工具类(MyWindowsCmd)
功能大概描述一下如果直接StandardOutput.ReadToEnd()这种方法,有很多限制 这类方式必须把命令全部执行一次写入并标记为exit,而且返回内容的获取会一直等待,如果在主线程里使用会 ...
- [置顶]
Xamarin android中使用signalr实现即时通讯
前面几天也写了一些signalr的例子,不过都是在Web端,今天我就来实践一下如何在xamarin android中使用signalr,刚好工作中也用到了这个,也算是总结一下学到的东西吧,希望能帮助你 ...
- Xamarin android CardView的使用详解
android 5.0新增加的一个控件CardView,在support v7兼容包中,意思就是卡片View,虽然可以设置阴影,圆角等等样式,但是我们也可以自己写出来,谷歌工程师之所以出这个,肯定是帮 ...
- springBoot系列教程07:异常捕获
发生异常是很正常的事,异常种类也是千奇百怪,发生异常并不可怕,只要正确的处理,并正确的返回错误信息并无大碍,如果不进行捕获或者处理,分分钟服务器宕机是很正常的事 所以处理异常时,最基本的要求就是发生异 ...
- LANMP On CentOS 6
摘要 --在CentOS6.2-x86_64上安装Apache,Nginx,MySQL,Php 环境:最小化安装系统 yum install lrzsz mlocate.x86_64 wget lso ...