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. Java面试知识总结三

    1.SpringMVC在项目中的应用? 1)service层,项目的业务逻辑层,一般先定义一个接口,在写一个实现类,实现所有的接口方法.service的实现类中要加注解@Service(用于标注业务层 ...

  2. MVP应用在android app上

    使用MVP模式来解耦activity中业务代码和界面代码.在activity中,将其中的业务抽象到presenter层:将其中的界面代码抽象到View层. MVP模式: 一个软件被划分成三层,View ...

  3. hdu 2157 How many ways?? ——矩阵十题第八题

    Problem Description 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, ...

  4. Morley's Theorem (计算几何基础+向量点积、叉积、旋转、夹角等+两直线的交点)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 找不到jar包的问题,路径问题

    1.参考连接: https://blog.csdn.net/huangbiao86/article/details/6428608 折腾了一上午,找到了这错误的原因.哎……悲剧! 确认包已经被导入we ...

  6. Java 中的静态内部类

    静态内部类是 static 修饰的内部类,这种内部类的特点是: 1. 静态内部类不能直接访问外部类的非静态成员,但可以通过 new 外部类().成员 的方式访问 2. 如果外部类的静态成员与内部类的成 ...

  7. python2 处理urllib/urllib2错误并打印源码

    import urllib2 import urllib url = 'http://localhost/12.php' data = {} data['cmd']='whoami' data = u ...

  8. Python3【模块】concurrent.futures模块,线程池进程池

    Python标准库为我们提供了threading和multiprocessing模块编写相应的多线程/多进程代码,但是当项目达到一定的规模,频繁创建/销毁进程或者线程是非常消耗资源的,这个时候我们就要 ...

  9. Django rest framework + Vue简单示例

    构建vue项目参考这篇文章https://segmentfault.com/a/1190000008049815 一.创建Vue项目 修改源:npm config set registry https ...

  10. linux加载指定目录的so文件

    linux加载指定目录的so文件 http://blog.csdn.net/win_lin/article/details/8286125 download urlhttp://download.ch ...