Query on A Tree

时间限制: 8 Sec  内存限制: 512 MB
提交: 15  解决: 3
[提交][状态][讨论版]

题目描述

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?

输入

There are no more than six test cases.
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 (可持续化字典树)的更多相关文章

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

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

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

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

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

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

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  8. SP1487 PT07J - Query on a tree III (主席树)

    SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...

  9. Problem: Query on the tree(二分+划分树)

    题目链接: Problem: Query on the tree Time limit: 1s     Mem limit: 64 MB      Problem Description There ...

随机推荐

  1. [Luogu 3973] TJOI2015 线性代数

    [Luogu 3973] TJOI2015 线性代数 这竟然是一道最小割模型. 据说是最大权闭合子图. 先把矩阵式子推出来. 然后,套路建模就好. #include <algorithm> ...

  2. javascript「篱式」条件判断

    我们已经知道,null 没有任何的属性值,并且无法获取其实体(existence)值.所以 null.property 返回的是错误(error)而不是 undefined . 考虑下面的代码 if ...

  3. 【hdu5217-括号序列】线段树

    题意:给一串括号,有2个操作,1.翻转某个括号.2.查询某段区间内化简后第k个括号是在原序列中的位置.1 ≤ N,Q ≤ 200000. 题解: 可以知道,化简后的序列一定是)))((((这种形式的. ...

  4. Android--hardwareAccelerated 硬件加速详解 android:largeHeap="true"

    做项目时,引导页面的ViewPager报了OOM异常,图片并不大,在清单文件Application节点中添加了两行代码就解决了这个问题 android:hardwareAccelerated=&quo ...

  5. Bagging和Boosting 概念及区别(转)

    Bagging和Boosting都是将已有的分类或回归算法通过一定方式组合起来,形成一个性能更加强大的分类器,更准确的说这是一种分类算法的组装方法.即将弱分类器组装成强分类器的方法. 首先介绍Boot ...

  6. Java多线程学习(四)等待/通知(wait/notify)机制

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  7. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  8. python基础===codecs打开文件,解决文件编码格式的问题

    codecs https://docs.python.org/3/library/codecs.html 我们经常用open打开文件的时候会出现各式各样的错误,编码格式的问题,等等~真的很烦 现在尽量 ...

  9. monkey测试===修改adb的默认端口

    最近电脑上由于公司系统的原因,adb的端口被占用了,但是占用端口的进程是必须启动的,不能被杀死,在网上找了很多办法,大家都是说杀死占用端口的进程.这个方法并不适用我,所以在此给大家一个新的方法.新建一 ...

  10. 设计模式之笔记--原型模式(Prototype)

    原型模式(Prototype) 定义 原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 类图 描述 提供一个克隆自身的接口--Clone方法. 应用场景 ...