题意:

给你一棵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. Excel获取当前日期和时间

    在Excel中获取当前时间 1.第一种在空的单元格内输入函数“NOW()”回车即可获取当前时间如图 2.第二种选中空单元格“按住CTRL+:”回车即可获取当前时间 3.第一种在空的单元格内输入函数“t ...

  2. Math&Random&ThreadLocalRandom类

    Math类 //绝对值值运算: Math.abs(18.999); //返回19.999这个数的绝对值 Math.abs(-12.58); // 返回-12.58这个数的绝对值,为12.58 //取值 ...

  3. python利用sift和surf进行图像配准

    1.SIFT特征点和特征描述提取(注意opencv版本) 高斯金字塔:O组L层不同尺度的图像(每一组中各层尺寸相同,高斯函数的参数不同,不同组尺寸递减2倍) 特征点定位:极值点 特征点描述:根据不同b ...

  4. CDQ 入门

    推荐博客 :https://blog.csdn.net/wu_tongtong/article/details/78785836 https://www.cnblogs.com/mlystdcall/ ...

  5. dp-最大递增子段和

      Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. M ...

  6. 自己动手搭环境—unit 1、Struts2环境搭建

    1.web.xml中增加Struts2配置 <filter> <filter-name>struts2</filter-name> <filter-class ...

  7. Java入门 - 语言基础 - 11.switch_case

    原文地址:http://www.work100.net/training/java-switch-case.html 更多教程:光束云 - 免费课程 switch_case 序号 文内章节 视频 1 ...

  8. [bzoj4872] [洛谷P3750] [六省联考2017] 分手是祝愿

    Description Zeit und Raum trennen dich und mich. 时空将你我分开. \(B\) 君在玩一个游戏,这个游戏由 \(n\) 个灯和 \(n\) 个开关组成, ...

  9. [bzoj4417] [洛谷P3990] [Shoi2013] 超级跳马

    Description 现有一个n行m列的棋盘,一只马欲从棋盘的左上角跳到右下角.每一步它向右跳奇数列,且跳到本行或相邻行.跳越期间,马不能离开棋盘.例如,当n = 3, m = 10时,下图是一种可 ...

  10. TCP客户端服务器编程模型

    1.客户端调用序列 客户端编程序列如下: 调用socket函数创建套接字 调用connect连接服务器端 调用I/O函数(read/write)与服务器端通讯 调用close关闭套接字 2.服务器端调 ...