Query on A Tree

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)

Problem Description
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?

 
Input
There are no more than 6 test cases.

For each test case there are two positive integers n and q, indicate 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, indicating the value of node i.

The second line contains n-1 non-negative integers F1,F2,⋯Fn−1, Fi means the father of node i+1.

And then q lines follow.

In the i-th line, there are two integers u and x, indicating that the node you pick should be in the subtree of u, and x has been described in the problem.

2≤n,q≤105

0≤Vi≤109

1≤Fi≤n, the root of the tree is node 1.

1≤u≤n,0≤x≤109

 
Output
For each query, just print an integer in a line indicating the largest result.
 
Sample Input
2 2
1 2
1
1 3
2 1
 
Sample Output
2 3

题解:

  每个数存在各自trie树里边,n个点这是棵树,再从底向上tri树合并起来

  查询就是查询一颗合并后的trie树,利用从高位到低位,贪心取

#include <bits/stdc++.h>
inline int read(){int x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;} using namespace std; #define LL long long
const int N = 2e5; vector<int > G[N];
int n, q, x, u, a[N];
int ch[N*][], root[N],sz; void inserts(int u,int x) {
root[u] = ++sz;
int tmp = sz;
int y = sz;
for(int i = ; i >= ; --i) {
int tmps = (x>>i)&;
if(!ch[y][tmps]) ch[y][tmps] = ++sz;
y = ch[y][tmps];
}
}
int merges(int u,int to) {
if(u == ) return to;
if(to == ) return u;
int t = ++sz;
ch[t][] = merges(ch[u][],ch[to][]);
ch[t][] = merges(ch[u][],ch[to][]);
return t;
}
void dfs(int u) {
inserts(u,a[u]);
for(auto to:G[u]) {
dfs(to);
root[u] = merges(root[u],root[to]);
}
}
LL query(int u,int x) {
int y = root[u];
LL ret = ;
for(int i = ; i >= ; --i) {
int tmps = (x>>i)&;
if(ch[y][tmps^]) ret += (<<i),y = ch[y][tmps^];
else y = ch[y][tmps];
}
return ret;
}
void init() {
for(int i = ; i <= n; ++i) root[i] = ,G[i].clear();
sz = ;
memset(ch,,sizeof(ch));
}
int main( int argc , char * argv[] ){
while(scanf("%d%d",&n,&q)!=EOF) {
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
init();
for(int i = ; i <= n; ++i) {
scanf("%d",&x);
G[x].push_back(i);
}
dfs();
for(int i = ; i <= q; ++i) {
scanf("%d%d",&u,&x);
printf("%lld\n",query(u,x));
}
}
return ;
}

2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并的更多相关文章

  1. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  2. 2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree

    Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A learne ...

  3. 2017ACM/ICPC广西邀请赛-重现赛

    HDU 6188 Duizi and Shunzi 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 思路: 签到题,以前写的. 实现代码: #inc ...

  4. 2017ACM/ICPC广西邀请赛-重现赛1005 CS course

    2017-08-31 16:19:30 writer:pprp 这道题快要卡死我了,队友已经告诉我思路了,但是做题速度很缓慢,很费力,想必是因为之前 的训练都是面向题解编程的缘故吧,以后不能这样了,另 ...

  5. 2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

    上一场CF打到心态爆炸,这几天也没啥想干的 A Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. 2017ACM/ICPC广西邀请赛 1005 CS Course

    CS Course Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. 2017ACM/ICPC广西邀请赛-重现赛 1004.Covering

    Problem Description Bob's school has a big playground, boys and girls always play games here after s ...

  8. 2017ACM/ICPC广西邀请赛-重现赛 1001 A Math Problem

    2017-08-31 16:48:00 writer:pprp 这个题比较容易,我用的是快速幂 写了一次就过了 题目如下: A Math Problem Time Limit: 2000/1000 M ...

  9. 2017ACM/ICPC广西邀请赛 Color it

    Color it Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Tota ...

随机推荐

  1. Working out (DP)

    Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...

  2. LCA+主席树 (求树上路径点权第k大)

      SPOJ 10628. Count on a tree (树上第k大,LCA+主席树) 10628. Count on a tree Problem code: COT You are given ...

  3. BZOJ 2242 [SDOI2011]计算器 ——EXGCD/快速幂/BSGS

    三合一的题目. exgcd不解释,快速幂不解释. BSGS采用了一种不用写EXGCD的方法,写起来感觉好了很多. 比较坑,没给BSGS的样例(LAJI) #include <map> #i ...

  4. POJ 1067: Wythoff Game【博弈】

    经典的威佐夫博奕把黄金分割常数乘以k(k=m-n)即为奇异点,此时奇异点是用小数据观察出来的,具体的数学证明,观察到黄金分割常数是无理数,再加上高斯函数[kφ]的形势将自然数分割成两个等价类很容易想到 ...

  5. 转载:shell中awk printf的用法

    转载:http://www.linuxawk.com/jiaocheng/83.html 6. printf函数   打印输出时,可能需要指定字段间的空格数,从而把列排整齐.在print函数中使用制表 ...

  6. [无趣]bit reverse

    真不想承认啊,因为年轻而犯下的错误! inline void _BR(int* a,int r){ for(int i=0,j=1;i<r;++i,j<<=1){ for(int k ...

  7. sqlserver通过设计器修改表结构保存时提示:保存到文本问题

    在sqlserver通过设计器修改表结构后保存时提示:保存到文本问题,这个问题可能通过修改设置项解决 工具>选项>设计器>   在弹出的窗口是把“阻止保存要求重新创建表的更改”选项的 ...

  8. shell的select脚本的简单入门

    shell的select脚本的简单入门 语法:select var in ...;do break;doneecho $var 示例: #/bin/bash echo "what is yo ...

  9. [bzoj5457]城市_dsu on tree

    bzoj 5457 城市 题目大意 给定一棵以\(1\)为根的\(n\)个节点的有根树. 每个节点有一个民族和该民族在当前节点的人数. 有\(n\)个询问,第\(i\)个询问是求以\(i\)为根的子树 ...

  10. 洛谷—— P3395 路障

    https://www.luogu.org/problem/show?pid=3395 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1, ...