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树,利用从高位到低位,贪心取

  1. #include <bits/stdc++.h>
  2. 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;}
  3.  
  4. using namespace std;
  5.  
  6. #define LL long long
  7. const int N = 2e5;
  8.  
  9. vector<int > G[N];
  10. int n, q, x, u, a[N];
  11. int ch[N*][], root[N],sz;
  12.  
  13. void inserts(int u,int x) {
  14. root[u] = ++sz;
  15. int tmp = sz;
  16. int y = sz;
  17. for(int i = ; i >= ; --i) {
  18. int tmps = (x>>i)&;
  19. if(!ch[y][tmps]) ch[y][tmps] = ++sz;
  20. y = ch[y][tmps];
  21. }
  22. }
  23. int merges(int u,int to) {
  24. if(u == ) return to;
  25. if(to == ) return u;
  26. int t = ++sz;
  27. ch[t][] = merges(ch[u][],ch[to][]);
  28. ch[t][] = merges(ch[u][],ch[to][]);
  29. return t;
  30. }
  31. void dfs(int u) {
  32. inserts(u,a[u]);
  33. for(auto to:G[u]) {
  34. dfs(to);
  35. root[u] = merges(root[u],root[to]);
  36. }
  37. }
  38. LL query(int u,int x) {
  39. int y = root[u];
  40. LL ret = ;
  41. for(int i = ; i >= ; --i) {
  42. int tmps = (x>>i)&;
  43. if(ch[y][tmps^]) ret += (<<i),y = ch[y][tmps^];
  44. else y = ch[y][tmps];
  45. }
  46. return ret;
  47. }
  48. void init() {
  49. for(int i = ; i <= n; ++i) root[i] = ,G[i].clear();
  50. sz = ;
  51. memset(ch,,sizeof(ch));
  52. }
  53. int main( int argc , char * argv[] ){
  54. while(scanf("%d%d",&n,&q)!=EOF) {
  55. for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
  56. init();
  57. for(int i = ; i <= n; ++i) {
  58. scanf("%d",&x);
  59. G[x].push_back(i);
  60. }
  61. dfs();
  62. for(int i = ; i <= q; ++i) {
  63. scanf("%d%d",&u,&x);
  64. printf("%lld\n",query(u,x));
  65. }
  66. }
  67. return ;
  68. }

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. 机房合作(三):We are Team,We are Family

    导读:拖拖拉拉,机房的合作也算是接近了尾声了.在这个过程中,真心是感谢我的两个组员.这个机房合作,看似简单,但我的组员给我的帮助和感动,都是不可忽略的.记得刚开始的时候,我就说过:不怕猪一样的组长,咱 ...

  2. shell的brake和continue的用法

    在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,像大多数编程语言一样,Shell也使用 break 和 continue 来跳出循环. break命令 break命令允许跳出所有循环(终止 ...

  3. cf493E Vasya and Polynomial

    Vasya is studying in the last class of school and soon he will take exams. He decided to study polyn ...

  4. gridview无数据源实现更新数据库(即断开更新数据库)

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  5. angular中ng-class的一些用法

    在前面Angularjs开发一些经验总结中我们说到在angular开发中angular controller never 包含DOM元素(html/css),在controller需要一个简单的POJ ...

  6. 【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题

    Tomcat的网站上的说法http://wiki.apache.org/tomcat/FAQ/Logging#Q6: System.out 和 System.err 都被打印到 catalina.ou ...

  7. Java内存区域划分、内存分配原理(深入理解JVM一)

    Java虚拟机在执行Java的过程中会把管理的内存划分为若干个不同的数据区域.这些区域有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,而有的区域则依赖线程的启动和结束而创建和销 ...

  8. spring boot -- 无法读取html文件,碰到的坑

    碰到的坑,无法Controller读取html文件 1. Controller类一定要使用@Controller注解,不要用@RestController 2. resource目录下创建templa ...

  9. MVP模式是你的救命稻草吗?

    为什么要学习架构? 不管是MVC还是MVP,亦或则其他架构,它们的设计目的都是为了达到编码的最高境界,那就是:低藕合,高复用,易测试,好维护. 而要达到这个终极目标,首先要理解的是每个部分各自负责些什 ...

  10. php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的

    原文呢:http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict Onl ...