Query on A Tree

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?

给出一棵根为\(1\)的树,每个点都有权值,\(q\)次询问,每次询问以\(u\)为根的子树里和\(x\)异或的最大值是多少

如果询问是以\(1\)为根的话,我们可以直接把所有点放到\(01\)字典树里去,然后每次拿\(x\)去匹配

现在不是以\(1\)为根,可以考虑树上启发式合并,先处理轻儿子,然后处理重儿子,保留重儿子的子树建出来的字典树,然后把其他子树里的点放进去

从高位到低位贪心匹配即可

//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int n,w[MAXN],q,son[MAXN],sz[MAXN],ret[MAXN];
vector<int> G[MAXN];
vector<pair<int,int> > Q[MAXN];
struct Trie{
int tot,ch[MAXN<<4][2];
void clear(){ tot = 0, ch[0][0] = ch[0][1] = 0; }
int newnode(){ tot++; ch[tot][0] = ch[tot][1] = 0; return tot; }
void insert(int x){
int p = 0;
for(int i = 30; ~i; i--){
int nxt = (x&(1<<i))?1:0;
if(!ch[p][nxt]) ch[p][nxt] = newnode();
p = ch[p][nxt];
}
}
int match(int x){
int res = 0, p = 0;
for(int i = 30; ~i; i--){
int nxt = (x&(1<<i))?0:1;
if(ch[p][nxt]) res |= (1<<i), p = ch[p][nxt];
else p = ch[p][nxt^1];
}
return res;
}
}trie;
void dfs(int u){
sz[u] = 1; son[u] = 0;
for(int v : G[u]){
dfs(v); sz[u] += sz[v];
if(sz[v]>sz[son[u]]) son[u] = v;
}
}
void update(int u){
trie.insert(w[u]);
for(int v : G[u]) update(v);
}
void search(int u, bool clear){
for(int v : G[u]) if(v!=son[u]) search(v,true);
if(son[u]) search(son[u],false);
for(int v : G[u]) if(v!=son[u]) update(v);
trie.insert(w[u]);
for(auto que : Q[u]) ret[que.second] = trie.match(que.first);
if(clear) trie.clear();
}
void solve(){
for(int i = 1; i <= n; i++){
G[i].clear(); Q[i].clear();
scanf("%d",&w[i]);
}
for(int i = 2; i <= n; i++){
int par; scanf("%d",&par);
G[par].emplace_back(i);
}
dfs(1);
for(int i = 1; i <= q; i++){
int u, x; scanf("%d %d",&u,&x);
Q[u].emplace_back(make_pair(x,i));
}
search(1,true);
for(int i = 1; i <= q; i++) printf("%d\n",ret[i]);
}
int main(){
while(scanf("%d %d",&n,&q)!=EOF) solve();
return 0;
}

HDU6191 Query on A Tre【dsu on tree + 01字典树】的更多相关文章

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

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

  2. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  3. HDU6191(01字典树启发式合并)

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

  4. HDU5589 Tree【分块 01字典树】

    HDU5589 Tree 题意: 给出一棵\(N\)个点的树,每条边有边权,每次询问下标为\([L,R]\)区间内的点能选出多少点对,点对之间的路径上的边权异或和大于\(M\) 题解: 对于两点\(u ...

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

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

  6. HDU 4757 Tree(可持续化字典树,lca)

    题意:询问树上结点x到结点y路上上的权值异或z的最大值. 任意结点权值 ≤ 2^16,可以想到用字典树. 但是因为是询问某条路径上的字典树,将字典树可持续化,字典树上的结点保存在这条路径上的二进制数. ...

  7. HDU5589:Tree(莫队+01字典树)

    传送门 题意 略 分析 f[u]表示u到根的边的异或 树上两点之间的异或值为f[u]^f[v], 然后将查询用莫队算法分块,每个点插入到字典树中,利用字典树维护两点异或值大于等于M复杂度O(N^(3/ ...

  8. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  9. trie tree(字典树)

    hihocoder题目(http://hihocoder.com/problemset):#1014 trie树 #include <iostream> using namespace s ...

随机推荐

  1. PHP 爬取图片 保存本地

    public function getImage($url,$filename='') { if($url == ''){ return false; } if($filename == ''){ $ ...

  2. HBase 底层原理详解(深度好文,建议收藏)

    HBase简介 HBase 是一个分布式的.面向列的开源数据库.建立在 HDFS 之上.Hbase的名字的来源是 Hadoop database,即 Hadoop 数据库.HBase 的计算和存储能力 ...

  3. Linux find 命令的初步实现(C++)

    Implement a myfind command following the find command in UNIX operating system. The myfind command s ...

  4. python学习笔记 | 猜拳游戏

    ''' @author: 人人都爱小雀斑 @time: 2020/3/6 18:52 @desc: 实验结果心得: 1.难点主要在判断谁输谁赢 2.挺好的 ''' import random d={1 ...

  5. awk中引用shell变量的方法

    1.通过命令行参数定义变量时引用: awk -v awk变量名= shell变量名 #!/bin/bash var4bash=test awk -v var4awk="$var4bash&q ...

  6. 【七天搞定Python】day01.Python环境配置、pip、IDE、注释、变量,数据类型、标识符/关键字、输出、输入

    什么是Python? 动态解释型语言,1982年由荷兰人Guido von Rossum发明. 更多细节可以google,这里不做展开. Python解释器: CPython(官方版本C语言实现) I ...

  7. luogu P2198 杀蚂蚁

    题目描述 经过小FF的研究,他发现蚂蚁们每次都走同一条长度为n个单位的路线进攻, 且蚂蚁们的经过一个单位长度所需的时间为T秒.也就是说,只要小FF在条路线上布防且给蚂蚁造成沉痛伤害就能阻止蚂蚁的进军. ...

  8. Docker相关简介以及使用方法

    Docker: 可以把它看作是一个软件,在这个软件当中呢,还可以安装其他的软件,还可以把软件所需要的环境依赖一起添加进来,这样让开发人员的程序在不同的环境当中都可以流转起来,避免了程序出现" ...

  9. Py-面向对象,组合,继承

    面向对象 只有特定对象能使用特定的几个方法对象=特征+动作 def dog(name,gender,type): #狗的动作 def jiao(dog): print('一条狗%s,汪汪汪' %dog ...

  10. Matlab GUI学习总结

    从简单的例子说起吧.   创建Matlab GUI界面通常有两种方式:   1,使用 .m 文件直接动态添加控件     2.  使用 GUIDE 快速的生成GUI界面显然第二种可视化编辑方法算更适合 ...