2017广西邀请赛 Query on A Tree (可持续化字典树)
Query on A Tree
时间限制: 8 Sec 内存限制: 512 MB
提交: 15 解决: 3
[提交][状态][讨论版]
题目描述
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?
输入
For each test case there are two positive integers n(2 ≤ n ≤ 105) and q(2 ≤ q ≤ 105), indicating 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(0 ≤ Vi ≤ 109), indicating the value of node i. The root of the tree is node 1.
The second line contains n-1 non-negative integers F1, F2,...Fn−1, Fi(1 ≤ Fi ≤ n) means the father of node i + 1.
And then q lines follow.
In the i-th line, there are two integers u(1 ≤ u ≤ n) and x(0 ≤ x ≤ 109), indicating that the node you pick should be in the subtree of u, and x has been described in the problem.
输出
样例输入
2 2
1 2
1
1 3
2 1
样例输出
2
3
【题意】给你一棵树,每个节点有权值,Q次询问,求u为跟的子树里与x亦或后的值最大是多少。
【分析】可持续化字典树可用来解决一段区间内与x亦或后的值最大是多少,这里可以用dfs序将子树转为序列。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+;;
const int M = ;
const int mod = 1e9+;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii; int bin[];
int n,m,tot;
int a[N],root[N],id[N],st[N],ed[N];
vector<int>edg[N];
struct trie{
int cnt;
int ch[N*][],sum[N*];
void init(){
met(ch,);met(sum,);
cnt=;
}
int insert(int x,int val){
int tmp,y;tmp=y=++cnt;
for(int i=;i>=;i--)
{
ch[y][]=ch[x][];ch[y][]=ch[x][];
sum[y]=sum[x]+;
int t=val&bin[i];t>>=i;
x=ch[x][t];
ch[y][t]=++cnt;
y=ch[y][t];
}
sum[y]=sum[x]+;
return tmp;
}
int query(int l,int r,int val){
int tmp=;
for(int i=;i>=;i--)
{
int t=val&bin[i];t>>=i;
if(sum[ch[r][t^]]-sum[ch[l][t^]])
tmp+=bin[i],r=ch[r][t^],l=ch[l][t^];
else r=ch[r][t],l=ch[l][t];
}
return tmp;
}
}trie;
void dfs(int u,int fa){
st[u]=++tot;
id[tot]=u;
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(v==fa)continue;
dfs(v,u);
}
ed[u]=tot;
}
int main(){
bin[]=;for(int i=;i<=;i++)bin[i]=bin[i-]<<;
while(~scanf("%d%d",&n,&m)){
root[]=;tot=;
trie.init();
for(int i=;i<=n;i++)scanf("%d",&a[i]),edg[i].clear();
for(int i=;i<=n;i++){
int v;
scanf("%d",&v);
edg[v].pb(i);
}
dfs(,);
for(int i=;i<=n;i++)root[i]=trie.insert(root[i-],a[id[i]]);
int l,r,x,u;
while(m--){
scanf("%d%d",&u,&x);
l=st[u];r=ed[u];
printf("%d\n",trie.query(root[l-],root[r],x));
}
}
return ;
}
2017广西邀请赛 Query on A Tree (可持续化字典树)的更多相关文章
- HDU 4757 Tree(可持续化字典树,lca)
题意:询问树上结点x到结点y路上上的权值异或z的最大值. 任意结点权值 ≤ 2^16,可以想到用字典树. 但是因为是询问某条路径上的字典树,将字典树可持续化,字典树上的结点保存在这条路径上的二进制数. ...
- HDU 6191 Query on A Tree(字典树+离线)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- HDU6191 Query on A Tree (01字典树+启发式合并)
题意: 给你一棵1e5的有根树,每个节点有点权,1e5个询问(u,x),问你子树u中与x异或最大的值是多少 思路: 自下而上启发式合并01字典树,注意合并时清空trie 线段树.字典树这种结构确定的数 ...
- BZOJ_1803_Spoj1487 Query on a tree III_主席树+dfs序
BZOJ_1803_Spoj1487 Query on a tree III_主席树 Description You are given a node-labeled rooted tree with ...
- 2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- hdu 4836 The Query on the Tree(线段树or树状数组)
The Query on the Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- SPOJ 375. Query on a tree (动态树)
375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...
- SP1487 PT07J - Query on a tree III (主席树)
SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...
- Problem: Query on the tree(二分+划分树)
题目链接: Problem: Query on the tree Time limit: 1s Mem limit: 64 MB Problem Description There ...
随机推荐
- sudo: /usr/libexec/sudo/sudoers.so must be only be writable by owne
1. chmod 644 sudoers.so 2. pkexec chmod 0440 /etc/sudoers
- CentOS7 搭建Apache环境
一.安装 yum -y install httpd 二.配置 主路径:/etc/httpd/ Apache目录说明 1 2 3 4 5 6 drwxr-xr-x 2 root root 4096 Ma ...
- 「6月雅礼集训 2017 Day5」仰望星空
[题目大意] 给你$n$个点,被一个半径为$R$的元圆划分成内(包含边界).外两个部分. 要连若干线,每个点只能连一条线,不存在重点和三点共线. 线只能连在内部点和外部点之间,线长度不超过$d$. 如 ...
- UOJ#31 【UR #2】猪猪侠再战括号序列
传送门http://uoj.ac/problem/31 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫_CallMeGGBond. 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领域的兴趣:尤其 ...
- Maven整体认识——详细介绍
前言 本文可以帮助你加深对Maven的整体认识,不是一篇基础文章.如果你现在还没有用 Maven 跑过 HelloWorld,那么本文可能不适合你. 一.Maven简介 Maven 官网:https: ...
- A Simple Math Problem(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 思路:矩阵快速幂模板题,不过因为刚刚入门矩阵快速幂,所以经常把数组f存反,导致本地错误一晚,差点 ...
- python初步学习-python数据类型-字典(dict)
字典 字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起.注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息. 注意,你只 ...
- vue路由-编程式导航
除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现. router.push(location, onComp ...
- linux运维记
nmap 127.0.0.1 命令查看当前服务器对外有多少端口,用于检查漏洞 vim ctrl+z ,jobs,fg 切换控制应用程序 vim 执行命令 #!sh aa.sh执行命令 运维系统监控开源 ...
- 测试php单例模式和静态访问,实例化访问的效率
// 测试的类class Memory { private static $a= null; public function __construct() { return self::$a; } pu ...