题目链接

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. Bootstrap学习笔记(二)---常见工具和流程导航范例

    使用bootstrap框架避免不了写CSS,当CSS文件较大时,会发现维护起来很麻烦,一些默认值,如行高.背景色.标注颜色.字号等信息往往反复出现,还有一些大体上一致,只有小部分不同的样式定义,这就需 ...

  2. 记一次生产环境Nginx日志骤增的问题排查过程

    摘要:众所周知,Nginx是目前最流行的Web Server之一,也广泛应用于负载均衡.反向代理等服务,但使用过程中可能因为对Nginx工作原理.变量含义理解错误,或是参数配置不当导致Nginx工作异 ...

  3. CET——4 常用短语

    在网上看到的,先拔到自己这来,四级大大,求过!!!!

  4. vs code调试console程序报错--preLaunchTask“build”

    网上有其他大神给出的建议是注释掉launch.json中的 "preLaunchTask": "build", 但是这种方式也会造成一个问题,就是再使用F5调试 ...

  5. bzoj 1196: [HNOI2006]公路修建问题

    Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织 ...

  6. bzoj 2727: [HNOI2012]双十字

    Description 在C 部落,双十字是非常重要的一个部落标志.所谓双十字,如下面两个例子,由两条水平的和一条竖直的"1"线段组成,要求满足以下几个限制: 我们可以找到 5 个 ...

  7. Python的文件及异常

    1. Python的文件及异常 1.1 文件操作 1.1.1 从文件中读取数据 许多情况下,我们的信息是存储在文本中的.例如对用户行为的分析,用户访问系统或者网站的访问信息会被存储于文本中,然后对文本 ...

  8. 解决 mysql 中文乱码

    mysql版本:5.6.38 虽然创建实例时选择的是utf-8的utf8_general_ci,但是用其他程序保存中文时依旧出现乱码的情况. 记录一种可行的解决方案,即修改数据库的字符集. 由于该环境 ...

  9. [js高手之路] es6系列教程 - new.target属性与es5改造es6的类语法

    es5的构造函数前面如果不用new调用,this指向window,对象的属性就得不到值了,所以以前我们都要在构造函数中通过判断this是否使用了new关键字来确保普通的函数调用方式都能让对象复制到属性 ...

  10. vmvare centos 7.0 root密码忘记后重置及总结

    今天遇到了一个比较尴尬的事情,我centos 7.0的虚拟机密码忘了.....里面还有我配置好的环境呢.于是我就上网上搜索各种方法,最后经我验证下面这个方法比较靠谱: 使用光盘修复Centos: ht ...