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. lightoj 1007 - Mathematically Hard 欧拉函数应用

    题意:求[a,b]内所有与b互质个数的平方. 思路:简单的欧拉函数应用,由于T很大 先打表求前缀和 最后相减即可 初次接触欧拉函数 可以在素数筛选的写法上修改成欧拉函数.此外本题内存有限制 故直接计算 ...

  2. .net core Fundamentals

    • Application Startup 應用程序啟動 • Middleware 中間件 • Working with Static Files 靜態文件 • Routing 路由 • URL Re ...

  3. Large Class--过大的类--要重构的信号

    如果想利用单个类做太多事情,其内往往就会出现太多实例变量.一旦如此,Duplicated Code也就接踵而至.     解决方法:     1.将类内彼此相关的变量,将它们放在一起.使用Extrac ...

  4. 汕头市队赛SRM 20 T3 灵魂觉醒

    背景 自从芽衣.布洛妮娅相继灵魂觉醒之后,琪亚娜坐不住了.自己可是第一个入驻休伯利安号的啊!于是她打算去找德丽莎帮忙,为她安排了灵魂觉醒的相关课程. 第一天,第一节课. “实现灵魂觉醒之前,你需要先将 ...

  5. bzoj 1057 单调栈

    首先我们可以枚举每个一点,然后向下一直拓展到不能拓展为止,然后向下拓展的同时我们可以算出来向左最多拓展的个数,用单调栈来维护一个上升的序列,这样就类似与悬线法找最大01子矩阵了,但是对于这题01交替来 ...

  6. hdu 1016 Prime Ring Problem (素数环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目大意:输入一个n,环从一开始到n,相邻两个数相加为素数. #include <iost ...

  7. postman测试express restful接口

    安装express及postman var express = require('express') var app = express(); var calculation = require('. ...

  8. python之requests库使用问题汇总

    一.请求参数类型 1.get requests.get(url, data, cookies=cookies) url:字符串: data:字典类型,可以为空: cookies:字典类型,可以为空: ...

  9. 【hihocoder】sam-3

    把Parent Tree拓扑排序下,然后从下往上合并. 具体的看官方题解啦~ #include<bits/stdc++.h> #define N 1000010 using namespa ...

  10. iOS APP程序启动原理

    UIApplication 程序启动原理 一个应用程序运行就必须要有一个进程,一个进程至少要有一个线程,我们把这个线程叫做主线程,主线程开启之后会开启一个主运行循环,如果不开启一个运行循环,程序开启了 ...