题目链接

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. jar包后台启动--nohup篇

    直接java -jar TestHttps-0.0.1-SNAPSHOT.jar的话是前段启动,但是窗口关闭之类的程序也就关闭了 我们可以nohup java -jar TestHttps-0.0.1 ...

  2. 【python】type()、instance()

    >>> a=520 >>> type(a) <class 'int'> >>> a=' >>> type(a) &l ...

  3. 【Hdu2089】不要62(数位DP)

    Description 题目大意:给定区间[n,m],求在n到m中没有"62"或"4"的数的个数. 如62315包含62,88914包含4,这两个数都是不合法的 ...

  4. solr安装配置

    1.solr是基于tomcat安装部署的 2.网上下载solr-5.2.1 http://lucene.apache.org/solr/downloads.html 3.解压solr文件 tar zx ...

  5. java RTTI笔记 之Class学习笔记(摘自java编程思想)

    1.java 使用Class对象来执行其RTTI.java 中每个类在编译后都会对应产生一个Class对象(更恰当地说是被保存在一个同名的.class文件中),甚至void和基本类型也都对应一个cla ...

  6. Mysql的硬件优化和配置优化

    mysql数据库的优化,算是一个老生常谈的问题了,网上也有很多关于各方面性能优化的例子,今天我们要谈的是MySQL硬件优化和系统参数的优化-即优化my.cnf文件 MySQL的优化我分为两个部分,一是 ...

  7. css3毛玻璃模糊效果

    CSS3 blur滤镜实现如下测试代码: .blur { -webkit-filter: blur(10px); /* Chrome, Opera */ -moz-filter: blur(10px) ...

  8. WPF-控件(1)

    小记:最近公司使用WPF开发一款新的产品,从零基础开始学习WPF,在看书的过程中,有些内容需要规整到一起,最终选择了博客园,希望对自己所掌握的知识有所规整. --------------------- ...

  9. Vue 爬坑之路(八)—— 使用 Echarts 创建图表

    在后台管理系统中,图表是一个很普遍的元素.目前常用的图标插件有 charts,  Echarts, highcharts.这次将介绍 Echarts 在 Vue 项目中的应用. 一.安装插件 使用 c ...

  10. git强制push

    Git 如何强制push? $ git push -u origin master –f   文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有 ...