题目链接

Problem Description
Monkey A lives on a tree, he always plays on this 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?

 
Input
There are no more than 6 test cases.

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

 
Output
For each query, just print an integer in a line indicating the largest result.
 
Sample Input
2 2
1 2
1
1 3
2 1
 
Sample Output
2
3
 
 
题意:有一棵由n个节点构成的树,每个点上有一个权值,现在q次询问,每次输入u,x 表示以u为根节点的子树上 以某个节点上的权值异或x得到的最大值?
 
思路:持久化 trie 树,感觉和主席树差不多,是有 n 个版本的字典树,在遍历树的过程中经过点时,建立新的字典树,但实际上与旧的相比每次只是增加了log2(1e9)个节点,另外是在子树 u 上求最大异或值,所以需要保存 u 子树之前节点号和 u 子树中的最后一个节点的编号,作差即可得到相应的数据;
 
代码如下:
#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(持久化字典树)的更多相关文章

  1. HDU 6191 Query on A Tree(字典树+离线)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  2. HDU - 6191 Query on A Tree (可持久化字典树/字典树合并)

    题目链接 题意:有一棵树,树根为1,树上的每个结点都有一个数字x.给出Q组询问,每组询问有两个值u,x,代表询问以结点u为根的子树中的某一个数与x的最大异或值. 解法一:dfs序+可持久化字典树.看到 ...

  3. 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 ...

  4. [hdu 6191] Query on A Tree

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  5. HDU6191 Query on A Tree (01字典树+启发式合并)

    题意: 给你一棵1e5的有根树,每个节点有点权,1e5个询问(u,x),问你子树u中与x异或最大的值是多少 思路: 自下而上启发式合并01字典树,注意合并时清空trie 线段树.字典树这种结构确定的数 ...

  6. HDU 6191 Query on A Tree(可持久化Trie)

    题意 \(n\) 个点的有根树,根为 \(1\) .每个点有点权,有 \(q\) 个询问,每次询问以 \(u\) 为根的子树的点的点权中异或 \(x\) 所得的最大值是多少. 思路 求出整棵树的 \( ...

  7. HDU 6191 Query on A Tree ( 2017广西邀请赛 && 可持久化Trie )

    题目链接 题意 : 给你一棵树.树上的每个点都有点权.之后有若干次问询.每次问询给出一个节点编号以及一个整数 X .问你以给出节点为根的子树中哪个节点和 X 异或最大.输出这个值 分析 : 看到这种树 ...

  8. 【HDU 6191】Query on A Tree 【可持久化字典树】

    题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这 ...

  9. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

  10. Hdu-4757 Tree(可持久化字典树+lca)

    题目链接:点这 我的github地址:点这     Problem Description   Zero and One are good friends who always have fun wi ...

随机推荐

  1. C# 委托高级应用----线程——创建无阻塞的异步调用(一)

    前言 本文大部分内容来自于mikeperetz的Asynchronous Method Invocation及本人的一些个人体会所得,希望对你有所帮助.原英文文献可以在codeproject中搜索到. ...

  2. 用html和css轻松实现康奈尔笔记(5R笔记)模板

    缘起 人家都说康奈尔笔记法,很好用呢,能抵抗遗忘曲线,让你的笔记事半功倍,有兴趣的同学自行百度哈. 网上有很多现成的模板,下载下来之后吧,看着好像在上面写英文可能更方便一点,行距很小,而且还有网址在上 ...

  3. 'boost/iterator/iterator_adaptor.hpp' file not found之xcode生成时报错的解决方案

    xcode生成rn(0.49.3)项目的时候出现“'boost/iterator/iterator_adaptor.hpp' file not found之xcode”报错. 原因: /Users/x ...

  4. OpenStack运维(三):OpenStack存储节点和配置管理

    1.对象存储节点维护 1.1 重启存储节点 如果一个存储节点需要重启,直接重启即可. 1.2 关闭存储节点 如果一个存储节点需要关闭很长一段时间,可以考虑将该节点从存储环中移除. swift-ring ...

  5. Linux第四节 组管理、用户管理、权限管理 / chmod /chown / umask / vim

    三期第三讲1.组管理/用户管理(重要文件系统会实时备份 file-) vim/etc/group: 组管理文件://组名:密码控位键:组id:成员 vim/etc/gshadow:组密码管理文件:// ...

  6. java 异常处理与返回

    try{ // 1. return ++x; }catch(){ }finally{ //2. x++; } 实际返回值还是 ++x后的结果,因为 ++x 后 x 的值会入栈,作为返回结果: 以上代码 ...

  7. Apache Avro# 1.8.2 Specification (Avro 1.8.2规范)二

    h5 { text-indent: 0.71cm; margin-top: 0.49cm; margin-bottom: 0.51cm; direction: ltr; color: #000000; ...

  8. NodeJS初介

    之前很多环境搭建中都使用到了Nodejs,所以这边对Nodejs做一个简单总结. 1.什么是Nodejs Node.js是一个Javascript运行环境(runtime),发布于2009年5月,由R ...

  9. Java中堆与栈

    简单的说:Java把内存划分成两种:一种是栈内存,一种是堆内存. 1:什么是堆内存: 堆内存是是Java内存中的一种,它的作用是用于存储Java中的对象和数组,当我们new一个对象或者创建一个数组的时 ...

  10. python科学计算_scipy_常数与优化

    scipy在numpy的基础上提供了众多的数学.科学以及工程计算中常用的模块:是强大的数值计算库: 1. 常数和特殊函数 scipy的constants模块包含了众多的物理常数: import sci ...