Query on A Tree

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1238    Accepted Submission(s): 444


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 $V_1,V_2,\cdots,V_n$, indicating the value of node i.

The second line contains n-1 non-negative integers $F_1,F_2,\cdots\,F_{n-1}$, $F_i$ 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 \leq n,q \leq 10^5$

$0 \leq V_i \leq 10^9$

$1 \leq F_i \leq n$, the root of the tree is node 1.

$1 \leq u \leq n,0 \leq x \leq 10^9$
 

Output
For each query, just print an integer in a line indicating the largest result.
 

Sample Input
  1. 2 2
  2. 1 2
  3. 1
  4. 1 3
  5. 2 1

很久都没写过博客了qwq,重新写起了久违的博客。

其实前几天才做了18年的湖北省赛的网络同步赛,I题完全就是这个题的弱化版,而且数据还很水,被我写错了一个地方还AC了。

经人提点后,才发现对于一颗树上的每一个节点以及他的子节点经过DFS序可以构成一个连续的区间(转换成区间后就完全就是跟湖北省赛的I一模一样的问题,实在是无力吐槽),但是有一个需要注意的地方,dfs序的标号与原先的节点标号是不一样的,所以需要用一个数组记录他们之间的映射关系,然后通过离线即可解决问题。

关于离线的具体操作,就是将所有的询问保存下来,然后按照左端点的位置从大到小排序,然后从大到小插入大于每个询问值对应的二进制到01字典树中,同时更新或者标记对应点的id,如果查询时遇到id的值小于等于询问的右区间即可取得区间异或的最大值,否则反之。

  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <ctime>
  8. #include <deque>
  9. #include <iostream>
  10. #include <istream>
  11. #include <iterator>
  12. #include <list>
  13. #include <map>
  14. #include <new>
  15. #include <ostream>
  16. #include <queue>
  17. #include <set>
  18. #include <sstream>
  19. #include <stack>
  20. #include <string>
  21. #include <vector>
  22. using namespace std;
  23. # ifdef __GNUC__
  24. # if __cplusplus > 199711L
  25. # include <unordered_set>
  26. # include <unordered_map>
  27. # else
  28. #include <tr1/unordered_map>
  29. #include <tr1/unordered_set>
  30. using tr1::unordered_map;
  31. using tr1::unordered_set;
  32. # endif
  33. # else
  34. # include <unordered_map>
  35. # include <unordered_set>
  36. # endif
  37. #define FOR(i,a,n) for(register int i=(a),_=(n)+1;i<_;++i)
  38. #define FIR(i,n,a) for(register int i=(n),_=(a)-1;i>_;--i)
  39. #define all(a) (a).begin(),(a).end()
  40. #define vlb(a,n) (lower_bound(all(a),n)-(a).begin())
  41. #define vub(a,n) (upper_bound(all(a),n)-(a).begin())
  42. #define vlbx(a,n) ((a)[(lower_bound(all(a),n)-(a).begin())])
  43. #define vubx(a,n) ((a)[(upper_bound(all(a),n)-(a).begin())])
  44. #define reunique(a) (a).resize(unique(all(a))-(a).begin())
  45. #define mem(a,b) memset((a),(b),sizeof(a))
  46. #define sz(x) (int((x).size()))
  47. #define lowbit(x) ((x)&(-x))
  48. #define lch p<<1,l,mid
  49. #define rch p<<1|1,mid+1,r
  50. #define ll (p<<1)
  51. #define rr (p<<1|1)
  52. #define queues priority_queue
  53. #define pb push_back
  54. #define mp(a,b) make_pair((a),(b))
  55. #define lb lower_bound
  56. #define ub upper_bound
  57. #define ff first
  58. #define ss second
  59. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  60. //__attribute__((optimize("-O2")))
  61. typedef long long LL;
  62. typedef pair<int,int> PII;
  63. typedef pair<LL,LL> PLL;
  64. typedef vector<bool> VB;
  65. typedef vector<int> VI;
  66. typedef vector<LL> VL;
  67. typedef vector<PII> VII;
  68. typedef vector<PLL> VLL;
  69. // STL other **************************************************************************************
  70. template<class T,class U>inline istream& operator >> (istream& os,pair<T,U>& p) { return os>>p.first>>p.second; }
  71. template<class T,class U>inline pair<T,U> operator + (const pair<T,U>& p1,const pair<T,U>& p2) { return mp(p1.ff+p2.ff,p1.ss+p2.ss); }
  72. template<class T,class U>inline pair<T,U>& operator += (pair<T,U>& p1,const pair<T,U>& p2) { p1.ff+=p2.ff,p1.ss+=p2.ss; return p1; }
  73. template<class T,class U>inline pair<T,U> operator - (const pair<T,U>& p1,const pair<T,U>& p2) { return mp(p1.ff-p2.ff,p1.ss-p2.ss); }
  74. template<class T,class U>inline pair<T,U>& operator -= (pair<T,U>& p1,const pair<T,U>& p2) { p1.ff-=p2.ff,p1.ss-=p2.ss; return p1; }
  75. // Useful constants *******************************************************************************
  76. const int primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, 1000000123};
  77. const int dx[]= { 0, 1, 0,-1, 0, 1,-1, 1,-1};
  78. const int dy[]= {-1, 0, 1, 0,-1, 1,-1,-1, 1};
  79. #define ee 2.718281828459
  80. #define eps 0.00000001
  81. #define fftmod 998244353
  82. #define INF 0x3f3f3f3f
  83. #define LINF 0xfcfcfcfcfcfcfcfll
  84. #define MOD 1000000007
  85. #define pi 3.14159265358979323846l
  86. int teble();
  87. int main() {
  88. ios_base::sync_with_stdio(0);
  89. cin.tie(0);
  90. #ifndef ONLINE_JUDGE
  91. // freopen("in.txt","r",stdin);
  92. // freopen("out.txt","w",stdout);
  93. #endif
  94. teble();
  95. return 0;
  96. }
  97. //***************************************************************
  98. //***************************************************************
  99. //***************************************************************
  100. /**________________________code.begin_________________________**/
  101. const int N=1e5+5;
  102. int a[N],b[N],tot,ans[N];
  103. PII p[N];
  104. struct Edge_Node {
  105. int head[N],cnt;
  106. struct Edge { int to,next; } edge[N];
  107. void init() { mem(head,0),cnt=1; }
  108. void add(int u,int v) {
  109. edge[cnt]={v,head[u]},head[u]=cnt++;
  110. }
  111. } E;
  112. struct Dictionarie_Tree {
  113. int tree[N<<5][3],top;
  114. void init() { mem(tree[0],0),top=1; }
  115. void ins(int x,int id) {
  116. int rt,nxt,u,t;
  117. for(rt=0,t=31;~t;rt=nxt,--t) {
  118. nxt=tree[rt][u=x>>t&1];
  119. if(!nxt)mem(tree[top],0),tree[rt][u]=nxt=top++;
  120. tree[nxt][2]=id;
  121. }
  122. }
  123. int query(int x,int l) {
  124. int rt,nxt,u,t,ans=0;
  125. for(rt=0,t=31;~t;rt=nxt,--t) {
  126. u=x>>t&1,ans<<=1;
  127. if((nxt=tree[rt][!u])&&tree[nxt][2]<=l)ans+=1;
  128. else nxt=tree[rt][u];
  129. }
  130. return ans;
  131. }
  132. } D;
  133. struct Node {
  134. int id,x,l,r;
  135. bool operator < (Node &m) const {
  136. return l>m.l;
  137. }
  138. } node[N];
  139. void init() {
  140. E.init(),D.init(),tot=0;
  141. }
  142. void dfs(int x) {
  143. p[x].ff=++tot,b[tot]=x;
  144. for(int i=E.head[x];i;i=E.edge[i].next) {
  145. dfs(E.edge[i].to);
  146. }
  147. p[x].ss=tot;
  148. }
  149. int teble() {
  150. int n,q,x,y;
  151. while(cin>>n>>q) {
  152. init();
  153. FOR(i,1,n)cin>>a[i];
  154. FOR(i,2,n)cin>>x,E.add(x,i);
  155. dfs(1);
  156. FOR(i,1,q)cin>>x>>y,node[i]={i,y,p[x].ff,p[x].ss};
  157. sort(node+1,node+n+1);
  158. int s=n;
  159. FOR(i,1,q) {
  160. while(s>=node[i].l)D.ins(a[b[s]],s),--s;
  161. ans[node[i].id]=D.query(node[i].x,node[i].r);
  162. }
  163. FOR(i,1,q)cout<<ans[i]<<endl;
  164. }
  165. return 0;
  166. }
  167. /**_________________________code.end__________________________**/
  168. //***************************************************************
  169. //***************************************************************
  170. //***************************************************************
  171. /****************************************************************
  172. ** **
  173. ** ********* ********* ******** ** ********* **
  174. ** ** ** ** ** ** ** **
  175. ** ** ** ** ** ** ** **
  176. ** ** ********* ******** ** ********* **
  177. ** ** ** ** ** ** ** **
  178. ** ** ** ** ** ** ** **
  179. ** ** ********* ******** ********* ********* **
  180. ** **
  181. ****************************************************************/

HDU 6191 Query on A Tree(字典树+离线)的更多相关文章

  1. HDU - 6191 Query on A Tree (可持久化字典树/字典树合并)

    题目链接 题意:有一棵树,树根为1,树上的每个结点都有一个数字x.给出Q组询问,每组询问有两个值u,x,代表询问以结点u为根的子树中的某一个数与x的最大异或值. 解法一:dfs序+可持久化字典树.看到 ...

  2. HDU 6191 Query on A Tree(可持久化Trie+DFS序)

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

  3. [hdu 6191] Query on A Tree

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

  4. HDU 6191 Query on A Tree(可持久化Trie)

    题意 \(n\) 个点的有根树,根为 \(1\) .每个点有点权,有 \(q\) 个询问,每次询问以 \(u\) 为根的子树的点的点权中异或 \(x\) 所得的最大值是多少. 思路 求出整棵树的 \( ...

  5. HDU 6191 Query on A Tree ( 2017广西邀请赛 && 可持久化Trie )

    题目链接 题意 : 给你一棵树.树上的每个点都有点权.之后有若干次问询.每次问询给出一个节点编号以及一个整数 X .问你以给出节点为根的子树中哪个节点和 X 异或最大.输出这个值 分析 : 看到这种树 ...

  6. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  7. HDU:1251-统计难题(字典树模板,动态建树,静态建树)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memor ...

  8. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  9. HDU 1247 Hat’s Words(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

随机推荐

  1. [转帖] 国产x86-海光禅定 2018年营收过亿?

    中科曙光:全年业绩稳健,海光芯片营收过亿 X86服务器市场Intel占据绝对优势:X86处理器已经成为全球最广泛使用的处理器架构之一,尤其是在PC和服务器领域,其中在处理器市场的份额高达90%以上.中 ...

  2. MySQL_基础

    ## 数据库的基本概念 1. 数据库的英文单词: DataBase 简称 : DB 2. 什么数据库? * 用于存储和管理数据的仓库. 3. 数据库的特点: 1. 持久化存储数据的.其实数据库就是一个 ...

  3. 头大!RabbitMQ 和 Kafka 到底怎么选?

    前言 开源社区有好多优秀的队列中间件,比如RabbitMQ和Kafka,每个队列都貌似有其特性,在进行工程选择时,往往眼花缭乱,不知所措. 对于RabbitMQ和Kafka,到底应该选哪个? Rabb ...

  4. @OneToMany 一对多 通过表之间的链接

    https://blog.csdn.net/qq_38157516/article/details/80146547 一对多 一个人对多张卡,但是一张卡只能对应一个人,典型的一对多关系,下面就用One ...

  5. gRPC编译教程

    windows平台的编译 一.编译openssl ① 安装perl(可以使用ActivePerl),执行perl Configure VC-WIN64A no-asm .在这里解释一下参数含义,VC- ...

  6. P1106 删数问题

    展开 题目描述 键盘输入一个高精度的正整数NN(不超过250250位) ,去掉其中任意kk个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的NN和kk,寻找一种方案使得剩下的数字组成的新 ...

  7. RabbitMQ入门教程(十):队列声明queueDeclare

    原文:RabbitMQ入门教程(十):队列声明queueDeclare 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https:// ...

  8. Git小结---So far.......

    基本的: 1. 在配置了SSH Key的情况下,clone项目时使用:git clone git@github.com/用户名/仓库名.git  使用这种方式而不使用https的方式的好处在于,在pu ...

  9. Android中res下anim和animator文件夹区别与总结

    1.anim文件夹 anim文件夹下存放tween animation(补间动画)和frame animation(逐帧动画) 逐帧动画: ①在animation-list中使用item定义动画的全部 ...

  10. leetcode957. N 天后的牢房

    8 间牢房排成一排,每间牢房不是有人住就是空着. 每天,无论牢房是被占用或空置,都会根据以下规则进行更改: 如果一间牢房的两个相邻的房间都被占用或都是空的,那么该牢房就会被占用. 否则,它就会被空置. ...