题意:

给你一棵1e5的有根树,每个节点有点权,1e5个询问(u,x),问你子树u中与x异或最大的值是多少

思路:

自下而上启发式合并01字典树,注意合并时清空trie

线段树、字典树这种结构确定的数据结构,启发式合并的时候不需要考虑次序,复杂度都是nlogn

代码:

2200 / 10000ms , 60 / 128 M

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x))
#define LLONG_MAX 9223372036854775807 using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 1e5+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int tot;
int n, q;
int a[maxm];
int root[maxm];
int trie[maxn][];
vector<int>v[maxm];
queue<int>pool;
int build(){
if(!pool.empty()){
int x = pool.front();
pool.pop();
return x;
}
++tot;
return tot;
}
void insert(int root, int t){
for(int i = ; i >= ; i--){
int x = (t>>i)&;
if(!trie[root][x])trie[root][x]=build();
root = trie[root][x];
}
return;
}
int query(int root, int t){
int ans = ;
for(int i = ; i >= ; i--){
int x = (t>>i)&;
if(trie[root][x^]){
ans|=(<<i);
root = trie[root][x^];
}
else root = trie[root][x];
}
return ans;
}
vector<PI>ask[maxn];
void del(int x){
if(!x)return;
pool.push(x);
return;
}
int merge(int p, int q){
if(!p)return q;
if(!q)return p;
trie[p][] = merge(trie[p][], trie[q][]);
trie[p][] = merge(trie[p][], trie[q][]);
trie[q][]=trie[q][]=;
del(q);
return p;
}
int ans[maxn];
void dfs(int x){
root[x] = build();
insert(root[x], a[x]);
for(int i = ; i < (int)v[x].size(); i++){
int y = v[x][i];
dfs(y);
root[x] = merge(root[x],root[y]);
}
for(int i = ; i < (int)ask[x].size(); i++){
ans[ask[x][i].fst] = query(root[x], ask[x][i].sc);
}
return;
} int main(){
while(~scanf("%d %d", &n, &q)){
tot = ;
while(!pool.empty())pool.pop();
mem(trie,);
for(int i = ; i <= n; i++){
v[i].clear();
ask[i].clear();
root[i]= ;
scanf("%d", &a[i]);
}
for(int i = ; i <= n; i++){
int x;
scanf("%d", &x);
v[x].pb(i);
}
for(int i = ; i <= q; i++){
int u, x;
scanf("%d %d", &u, &x);
ask[u].pb(make_pair(i,x));
}
dfs();
for(int i = ; i <= q; i++){
printf("%d\n",ans[i]);
}
del(root[]);
}
return ;
}
/*
2 2
1 2
1
1 3
2 1 3 2
1 2 3
1 1
3 4
1 3 11 4
1 2 3 4 5 6 7 8 9 10 11
1 1 1 2 2 3 4 4 4 5
2 5
3 6
1 2
8 7 100000 2
1 55
2 33
*/

HDU6191 Query on A Tree (01字典树+启发式合并)的更多相关文章

  1. 牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)

    牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并) 题意:给你一颗树,要求找出简单路径上最大权值为1~n每个边权对应的最大异或和 题解: 根据异或的性质我们可以得到 \ ...

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

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

  3. Love Live!-01字典树启发式合并

    链接:https://ac.nowcoder.com/acm/contest/201/D?&headNav=www 思路:题目要求的是每个等级下的最大 简单路径中的最大异或值,那么我们为了保证 ...

  4. HDU6191 Query on A Tre【dsu on tree + 01字典树】

    Query on A Tree Problem Description Monkey A lives on a tree, he always plays on this tree. One day, ...

  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 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

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

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

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

  8. Chip Factory(01字典树)

    Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others)  ...

  9. Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)

    Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...

随机推荐

  1. 小白学 Python 爬虫(35):爬虫框架 Scrapy 入门基础(三) Selector 选择器

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  2. Magicodes.IE编写多框架版本支持和执行单元测试

    背景 很多情况下,我们编写了一些工具库之后,往往在某些框架版本中会出现一些问题,比如本人最近写的一个导入导出的工具库Magicodes.IE(GitHub:https://github.com/xin ...

  3. Jpa自定义查询报错(Failed to convert from type [java.lang.Object[]] to type)

    Jpa自定义查询报错 问题背景 今天遇到一个奇怪的报错"Failed to convert from type [java.lang.Object[]] to type",这个报错 ...

  4. Redis系列之----Redis的数据类型及使用场景

       Redis是一个开源的.高性能的.基于键值对的缓存与存储系统,能够提供多种不同的键值数据类型来适应不同场景下的缓存和存储需求.    Redis中所有的数据都存储在内存中,因此读写速度非常快,相 ...

  5. 快速开发一个npm包(轮子)

    动机 很多人都想写一个自己的轮子,可是开始动手的时候你总会遇到以下问题 一个基本的 js 库应该如何编写 基本的前端项目都要哪些文件 又要怎么打包发布到 npm 上 你的 es6 语法如何才能让别人识 ...

  6. docker学习1--dockerfile

    记录docker学习过程 实践启动一个redis server 命令之前 要弄懂 dockfile 镜像 容器 三者概念以及三者之间的关系 dockerfile格式 # Comment 注释 INST ...

  7. WiFi模块Demo(新手教程)图文详解模块使用教程

    本文出自APICloud官方论坛,感谢论坛版主 Mr.ZhouHeng 的分享. 第一步我们需要在开发控制台创建一个Native App应用以及添模块的准备工作: 按照下图步骤 输入完点创建完成之后 ...

  8. React16源码解读:揭秘ReactDOM.render

    引言 在上一篇文章中我们通过create-react-app脚手架快速搭建了一个简单的示例,并基于该示例讲解了在类组件中React.Component和React.PureComponent背后的实现 ...

  9. Core 定时任务之TimeJob

    第一:引入NuGet包: Install-Package Pomelo.AspNetCore.TimedJob -Version -rtm- 第二: 在StartUp 中注册Job: public c ...

  10. 使用RobotFramework的DataBaseLibrary(Java实现)

    RobotFramework能用Python和Jython两条腿走路.但有的时候你得选一条.今天就碰上个问题,为了整合其它模块必须用Java实现的DataBaseLibrary 其实实它很简单,记录步 ...