每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]])

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=;
  8. struct poi{int size,lt,rt;}tree[maxn*];
  9. struct zs{int too,pre;}e[maxn<<];
  10. int n,m,x,y,z,sz,tot,N;
  11. int d[maxn],f[maxn][],last[maxn],root[maxn],v[maxn],b[maxn],num[maxn];
  12. inline void read(int &k)
  13. {
  14. int f=;k=;char c=getchar();
  15. while(c<''||c>'')c=='-'&&(f=-),c=getchar();
  16. while(c<=''&&c>='')k=k*+c-'',c=getchar();
  17. k*=f;
  18. }
  19. inline void add(int x,int y){e[++tot].too=y;e[tot].pre=last[x];last[x]=tot;}
  20. void insert(int &x,int l,int r,int cx)
  21. {
  22. tree[++sz]=tree[x];tree[sz].size++;x=sz;
  23. if(l==r)return;
  24. int mid=(l+r)>>;
  25. if(cx<=mid)insert(tree[x].lt,l,mid,cx);
  26. else insert(tree[x].rt,mid+,r,cx);
  27. }
  28. int query(int a,int b,int c,int d,int l,int r,int k)
  29. {
  30. if(l==r)return l;
  31. int mid=(l+r)>>;
  32. int t1=tree[a].lt,t2=tree[b].lt,t3=tree[c].lt,t4=tree[d].lt;
  33. int tmp=tree[t1].size+tree[t2].size-tree[t3].size-tree[t4].size;
  34. if(tmp>=k)return query(t1,t2,t3,t4,l,mid,k);
  35. return query(tree[a].rt,tree[b].rt,tree[c].rt,tree[d].rt,mid+,r,k-tmp);
  36. }
  37. void dfs(int x,int fa)
  38. {
  39. root[x]=root[fa];insert(root[x],,N,v[x]);
  40. d[x]=d[fa]+;f[x][]=fa;
  41. for(int i=last[x];i;i=e[i].pre)
  42. if(e[i].too!=fa)dfs(e[i].too,x);
  43. }
  44. inline int lca(int x,int y)
  45. {
  46. if(d[x]<d[y])swap(x,y);
  47. for(int i=;i>=;i--)
  48. if(d[f[x][i]]>=d[y])x=f[x][i];
  49. if(x==y)return x;
  50. for(int i=;i>=;i--)
  51. if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
  52. return f[x][];
  53. }
  54. int main()
  55. {
  56. read(n);read(m);
  57. for(int i=;i<=n;i++)read(v[i]),b[i]=v[i];N=n;
  58. sort(b+,b++N);N=unique(b+,b++N)-b-;
  59. for(int i=;i<=n;i++)x=lower_bound(b+,b++N,v[i])-b,num[x]=v[i],v[i]=x;
  60. for(int i=;i<n;i++)
  61. read(x),read(y),add(x,y),add(y,x);
  62. dfs(,);for(int j=;j<;j++)for(int i=;i<=n;i++)f[i][j]=f[f[i][j-]][j-];
  63. int lan=;
  64. for(int i=;i<=m;i++)
  65. {
  66. read(x);read(y);read(z);x^=lan;int fq=lca(x,y);
  67. printf("%d",lan=num[query(root[x],root[y],root[fq],root[f[fq][]],,N,z)]);
  68. if(i!=m)puts("");
  69. }
  70. }

bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)的更多相关文章

  1. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  2. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  3. 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 ...

  4. 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 ...

  5. Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)

    题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...

  6. spoj COT - Count on a tree (树上第K小 LCA+主席树)

    链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...

  7. BZOJ2588: Spoj 10628. Count on a tree

    传送门 刚开始看错题以为是dfs序瞎搞.. 后来看清题了开始想用树剖瞎搞... 感觉要滚粗啊.. 对于每个点到根的路径建立线段树,暴力建MLE没跑,上主席树,然后$(x,y)$的路径就可以先求出来$L ...

  8. 【主席树】bzoj2588 Spoj 10628. Count on a tree

    每个点的主席树的root是从其父转移来的.询问的时候用U+V-LCA-FA(LCA)即可. #include<cstdio> #include<algorithm> using ...

  9. 主席树初探--BZOJ2588: Spoj 10628. Count on a tree

    n<=100000的点权树,有m<=100000个询问,每次问两个点间的第k小点权,保证有解,强制在线. 主席上树啦!类似于之前的序列不带修改询问的前缀表示法,现在只要把前缀当成某点到根的 ...

随机推荐

  1. 180623-SpringBoot之logback配置文件

    SpringBoot配置logback 项目的日志配置属于比较常见的case了,之前接触和使用的都是Spring结合xml的方式,引入几个依赖,然后写个 logback.xml 配置文件即可,那么在S ...

  2. post接口_form表单上传

    上传文件的本质是浏览器读取本地文件的内容,以二进制数据方式传输到服务端,服务端新建一个文件,将获取到的数据复制到文件中 LR中上传操作可以通过web_submit_data函数实现,支持录制要点:we ...

  3. NHibernate3快速上手教程FluentNHibernate配置与DBHelper(已过期,有更好的)

    很多学习NHibernate的新手很容易卡在配置文件这一关,正所谓万事开头难,上手后再配合官方文档就比较容易了. 网上关于配置文件的资料非常多,但由于版本的问题,许多老的教程中都没有明确指出类库的版本 ...

  4. [模板]非递归线段树(zkw的变异版本)

    类似于zkw,但空间只用两倍,zkw要4倍. 链接 可以下传标记,打熟后很好码. #include <set> #include <cmath> #include <cs ...

  5. LeetCode 109——有序链表转化二叉搜索树

    1. 题目 2. 解答 2.1. 方法一 在 LeetCode 108--将有序数组转化为二叉搜索树 中,我们已经实现了将有序数组转化为二叉搜索树.因此,这里,我们可以先遍历一遍链表,将节点的数据存入 ...

  6. python SyntaxError: Non-ASCII character '\xe8' in file C:\Users\nwpujun\PycharmProjects\projects\hrl1\hrlAgent\src\li_nn.py on line 52

    解决方法:在文件头部加上这样的一句话 # -*- coding: utf-8 -*- 注意:加在其他的位置可能没用,我就是这样的

  7. 火狐metamask账号

    火狐metamask lock trophy pyramid sunny aim inmate body sense sing castle cinnamon cram

  8. mybatis 枚举类型使用

    一.首先定义接口,提供获取数据库存取的值得方法,如下: public interface BaseEnum { int getCode(); } 二.定义mybatis的typeHandler扩展类, ...

  9. [leetcode-718-Maximum Length of Repeated Subarray]

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  10. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...