Description

给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。
 

Input

第一行两个整数N,M。
第二行有N个整数,其中第i个整数表示点i的权值。
后面N-1行每行两个整数(x,y),表示点x到点y有一条边。
最后M行每行两个整数(u,v,k),表示一组询问。
 

Output

M行,表示每个询问的答案。最后一个询问不输出换行符

Sample Input

8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2

Sample Output

2
8
9
105
7

HINT

HINT:
N,M<=100000
暴力自重。。。
 
思路:
强制在线,lca + 主席树
 
实现代码:
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define lson l,m,rt<<1
  5. #define rson m+1,r,rt<<1|1
  6. #define mid int m = (l + r) >> 1
  7. const int M = 2e5 + ;
  8. int p[M][],dep[M],head[M],sum[M*],ls[M*],rs[M*],root[M*];
  9. int cnt1,n,idx,cnt,f[M];
  10.  
  11. struct node {
  12. int to,next;
  13. }e[M];
  14.  
  15. void add(int u,int v){
  16. e[++cnt1].to=v;e[cnt1].next=head[u];head[u]=cnt1;
  17. e[++cnt1].to=u;e[cnt1].next=head[v];head[v]=cnt1;
  18. }
  19.  
  20. int lca(int a,int b){
  21. if(dep[a] > dep[b]) swap(a,b);
  22. int h = dep[b] - dep[a];
  23. for(int i = ;(<<i)<=h;i++){
  24. if((<<i)&h) b = p[b][i];
  25. }
  26.  
  27. if(a!=b){
  28. for(int i = ;i >= ;i --){
  29. if(p[a][i]!=p[b][i]){
  30. a = p[a][i]; b = p[b][i];
  31. }
  32. }
  33. a = p[a][];
  34. }
  35. return a;
  36. }
  37.  
  38. void build(int l,int r,int &rt){
  39. rt = ++idx;
  40. sum[rt] = ;
  41. if(l == r) return;
  42. mid;
  43. build(l,m,ls[rt]);
  44. build(m+,r,rs[rt]);
  45. }
  46.  
  47. void update(int p,int l,int r,int old,int &rt){
  48. rt = ++idx;
  49. ls[rt] = ls[old]; rs[rt] = rs[old]; sum[rt] = sum[old] + ;
  50. if(l == r) return ;
  51. mid;
  52. if(p <= m) update(p,l,m,ls[old],ls[rt]);
  53. else update(p,m+,r,rs[old],rs[rt]);
  54. }
  55.  
  56. int query(int a,int b,int lc,int cl,int l,int r,int k){
  57. if(l == r) return l;
  58. mid;
  59. int cnt = sum[ls[a]] + sum[ls[b]] - sum[ls[lc]] - sum[ls[cl]];
  60. if(k <= cnt)
  61. return query(ls[a],ls[b],ls[lc],ls[cl],l,m,k);
  62. else
  63. return query(rs[a],rs[b],rs[lc],rs[cl],m+,r,k-cnt);
  64. }
  65. int a[M],b[M];
  66.  
  67. void dfs(int u,int fa){
  68. f[u] = fa;
  69. update(a[u],,cnt,root[fa],root[u]);
  70. for(int i = head[u];i;i = e[i].next){
  71. int v = e[i].to;
  72. if(v == fa) continue;
  73. p[v][] = u;
  74. dep[v] = dep[u] + ;
  75. dfs(v,u);
  76. }
  77. }
  78.  
  79. void init()
  80. {
  81. cnt1 = ; idx = ;
  82. memset(head,,sizeof(head));
  83. memset(dep,,sizeof(dep));
  84. memset(p,,sizeof(p));
  85. memset(f,,sizeof(f));
  86. dep[] = ;
  87. }
  88.  
  89. int main()
  90. {
  91. int m;
  92. while(scanf("%d%d",&n,&m)!=EOF){
  93. init();
  94. for(int i = ; i <= n;i ++){
  95. scanf("%d",&a[i]);
  96. b[i] = a[i];
  97. }
  98. int l,r,c;
  99. sort(b+,b+n+);
  100. cnt = unique(b+,b++n)-b-;
  101. for(int i = ;i <= n;i ++)
  102. a[i] = lower_bound(b+,b+cnt+,a[i]) - b;
  103. for(int i = ;i <= n-;i ++){
  104. scanf("%d%d",&l,&r);
  105. add(l,r);
  106. }
  107. build(,cnt,root[]);
  108. dfs(,);
  109. for(int j = ;(<<j)<=n;j++)
  110. for(int i = ;i <= n;i++)
  111. p[i][j] = p[p[i][j-]][j-];
  112. int num = ;
  113. for(int i = ;i <= m;i ++){
  114. scanf("%d%d%d",&l,&r,&c);
  115. l = l^num;
  116. int lc = lca(l,r);
  117. int id = query(root[l],root[r],root[lc],root[f[lc]],,cnt,c);
  118. num = b[id];
  119. printf("%d\n",b[id]);
  120. }
  121. }
  122. return ;
  123. }

bzoj 2588 : Spoj 10628. Count on a tree的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  3. Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...

  4. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  5. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

  6. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 7669  Solved: 1894[Submi ...

  7. 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree

    题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...

  8. BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

    分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...

  9. ●BZOJ 2588 Spoj 10628. Count on a tree

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...

  10. 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree

    [题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...

随机推荐

  1. python三:循环语句练习--小白博客

    # 打印0-10去掉5 count = - : count += : continue print(count) # 打印0-10的偶数 count = : print(count) count+= ...

  2. Educational Codeforces Round 52 (Rated for Div. 2) -C

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  3. Python Revisited Day 02 (数据类型)

    目录 Python 关键字 整数 整数转换函数 整数位逻辑操作符 浮点类型 math模块函数与常量 复数 精确的十进制数字 decimal 字符串 str.format() 格式规约 Python 关 ...

  4. 【学习总结】GirlsInAI ML-diary day-4:变量/Variable

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day4 变量/Variable 变量是计算机编程中一个很基础的概念,在计算机程序中,variables are reser ...

  5. jQuery学习(监听DOM加载)

    jQuery的extend方法 function njQuery() { } /* njQuery.extend = function (obj) { // 此时此刻的this就是njQuery这个类 ...

  6. laravel log改为时间格式

    1 providers新建文件 LogRotateServiceProvider.php <?php namespace App\Providers; use Monolog\Formatter ...

  7. 5 Http请求中文乱码处理

    java 乱码分很多种,这里主要研究解决http请求中出现乱码的情况. http请求出现中文乱码的主要原因:发送方与接收方编码不一致,服务器默认支持的编码与web应用不一致,如:tomcat 是国外程 ...

  8. Oracle可视化工具PL/SQL Developer的安装与配置

    安装程序: 安装目录不能有中文和空格,否则无法进行远程连接. 推荐使用 D:\PLSQLDeveloper 为安装目录 破解PLSQLDeveloper 使用工具 PLSQL Developer10. ...

  9. mybatis二级缓存详解

    1  二级缓存简介 二级缓存是在多个SqlSession在同一个Mapper文件中共享的缓存,它是Mapper级别的,其作用域是Mapper文件中的namespace,默认是不开启的.看如下图: 1. ...

  10. vue 项目使用 webpack 构建自动获取电脑ip地址

    1.开发 H5 时移动端,经常会使用真机进行调试本地环境.webpack 配置服务器好多脚手架写的都是固定的,而在团队开发中需要每人配置自己的本机 ip 进行开发,每次开启开发环境的都需要修改,并且还 ...