一、题目

  

COT - Count on a tree

You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight.

We will ask you to perform the following operation:

  • u v k : ask for the kth minimum weight on the path from node u to node v

Input

In the first line there are two integers N and M. (N, M <= 100000)

In the second line there are N integers. The ith integer denotes the weight of the ith node.

In the next N-1 lines, each line contains two integers u v, which describes an edge (u, v).

In the next M lines, each line contains three integers u v k, which means an operation asking for the kth minimum weight on the path from node u to node v.

Output

For each operation, print its result.

Example

  1. Input:
  2. 8 5
  3. 105 2 9 3 8 5 7 7
  4. 1 2
  5. 1 3
  6. 1 4
  7. 3 5
  8. 3 6
  9. 3 7
  10. 4 82 5 12 5 22 5 32 5 47 8 2 
  1. Output:
  2. 2891057 
  3.  
  4. 题目链接:http://www.spoj.com/problems/COT/二、思路  原理其实和一维线性表序列的一样。只不过这是在树上操作。  在一维线性表序列中,扫描序列中每一个离散化后的数字,每一次新建的权值线段树都是基于前一次的。而在树中,对每一个子节点的数字,新建的线段树都是基于父节点的线段树的。
  1. ***************************未完待续……  
  1. ***************************
  1.  三、源代码
  1. #pragma GCC optimize(2)
  2. #pragma comment(linker, "/STACK:102400000, 102400000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. typedef long long LL;
  6. #define MAXN 111111
  7. LL n, q, m;
  8. /*************树部分*******************/
  9. typedef struct {
  10. int to, next;
  11. } Edge;
  12. Edge tree[MAXN * ];
  13. int head[MAXN], ecnt;
  14.  
  15. void add(int from, int to) {
  16. tree[ecnt].to = to;
  17. tree[ecnt].next = head[from];
  18. head[from] = ecnt++;
  19. }
  20. /*************树部分*******************/
  21. /*************LCA部分*******************/
  22. ], depth[MAXN];
  23. void dfs4lca(int r, int par, int d) {
  24. parent[r][] = par, depth[r] = d;
  25. ; i = tree[i].next) {
  26. int to = tree[i].to;
  27. if(to != par)
  28. dfs4lca(to, r, d + );
  29. }
  30. }
  31. void init4lca() {
  32. dfs4lca(, -, );
  33. ; k < ; ++k) {
  34. ; v <= n; ++v) {
  35. )
  36. parent[v][k + ] = -;
  37. else
  38. parent[v][k + ] = parent[parent[v][k]][k];
  39. }
  40. }
  41. }
  42. int lca(int a, int b) {
  43. if(depth[a] < depth[b])
  44. swap(a, b);
  45. ; i < ; ++i) {
  46. )
  47. a = parent[a][i];
  48. }
  49. if(a == b)
  50. return a;
  51.  
  52. ; i >= ; --i) {
  53. if(parent[a][i] != parent[b][i]) {
  54. a = parent[a][i];
  55. b = parent[b][i];
  56. }
  57. }
  58. ];
  59. }
  60. /*************LCA部分*******************/
  61. /*************主席树部分*******************/
  62. struct {
  63. int lch, rch, cnt;
  64. } nodes[MAXN * ];
  65. int root[MAXN], ncnt;
  66.  
  67. , int r = m) {
  68. if(!nroot) {
  69. nroot = ++ncnt;
  70. nodes[nroot].cnt = nodes[proot].cnt + ;
  71. }
  72. if(l == r)
  73. return;
  74. ;
  75. if(val <= mid) {
  76. nodes[nroot].rch = nodes[proot].rch;
  77. update(nodes[nroot].lch, nodes[proot].lch, val, l, mid);
  78. } else {
  79. nodes[nroot].lch = nodes[proot].lch;
  80. update(nodes[nroot].rch, nodes[proot].rch, val, mid + , r);
  81. }
  82. }
  83. , int r = m) {
  84. if(l == r)
  85. return l;
  86. int cnt = nodes[nodes[uroot].lch].cnt + nodes[nodes[vroot].lch].cnt
  87. - nodes[nodes[lcaroot].lch].cnt - nodes[nodes[lcafroot].lch].cnt;
  88. ;
  89. if(k <= cnt)
  90. return query(nodes[uroot].lch, nodes[vroot].lch, nodes[lcaroot].lch, nodes[lcafroot].lch, k, l, mid);
  91. else
  92. , r);
  93. }
  94. /*************主席树部分*******************/
  95. /*************离散化部分*******************/
  96. LL weight[MAXN], buf[MAXN], mp[MAXN];
  97. int nwt[MAXN];
  98. void discrete() {
  99. memcpy(buf + , weight + , ]) * n);
  100. sort(buf + , buf + n + );
  101. m = unique(buf + , buf + n + ) - buf - ;
  102. ; i <= n; ++i) {
  103. nwt[i] = lower_bound(buf + , buf + m + , weight[i]) - buf;
  104. mp[nwt[i]] = weight[i];
  105. }
  106. }
  107. /*************离散化部分*******************/
  108. template <class T> inline void read(T &x) {
  109. int t;
  110. bool flag = false;
  111. ')) ;
  112. if(t == '-')
  113. flag = true, t = getchar();
  114. x = t - ';
  115. ')
  116. x = x * + t - ';
  117. if(flag)
  118. x = -x;
  119. }
  120.  
  121. void init() {
  122. memset(head, -, sizeof(head));
  123. ecnt = ;
  124. //nodes = 0, root = 0, ncnt = 0;
  125. }
  126.  
  127. void dfs4tree(int r, int par) {
  128. ; i = tree[i].next) {
  129. int to = tree[i].to;
  130. if(to != par) {
  131. update(root[to], root[r], nwt[to]);
  132. dfs4tree(to, r);
  133. }
  134. }
  135. }
  136.  
  137. int main() {
  138. #ifndef ONLINE_JUDGE
  139. freopen("input.txt", "r", stdin);
  140. #endif // ONLINE_JUDGE
  141. init();
  142. LL a, b, u, v, k, wt, uvlca, ans;
  143. scanf("%lld%lld", &n, &q);
  144. ; i <= n; ++i) {
  145. read(weight[i]);
  146. }
  147. ; i < n; ++i) {
  148. read(a), read(b);
  149. add(a, b);
  150. add(b, a);
  151. }
  152. init4lca();
  153. discrete();
  154.  
  155. add(, );//添加一条虚边。
  156. dfs4tree(, -);
  157.  
  158. while(q--) {
  159. read(u), read(v), read(k);
  160. uvlca = LL(lca(u, v));
  161. ans = mp[query(root[u], root[v], root[uvlca], root[parent[uvlca][]], int(k))];
  162. printf("%lld\n", ans);
  163. }
  164. ;
  165. }
  1.  
  1.  

SPOJ Count on a tree(主席树+LCA)的更多相关文章

  1. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

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

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

  3. spoj COT - Count on a tree(主席树 +lca,树上第K大)

    您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...

  4. [bzoj2588][count on a tree] (主席树+lca)

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

  5. 洛谷P2633/bzoj2588 Count on a tree (主席树)

    洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...

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

  7. spoj cot: Count on a tree 主席树

    10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...

  8. 【BZOJ-2588】Count on a tree 主席树 + 倍增

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 3749  Solved: 873[ ...

  9. 洛谷P2633 Count on a tree(主席树上树)

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

随机推荐

  1. UVa 11582 巨大的斐波那契数!(幂取模)

    https://vjudge.net/problem/UVA-11582 题意: 输入两个非负整数a.b和正整数n,你的任务是计算f(a^b)除以n的余数.f[0]=0,f[1]=1,f[i+2]=f ...

  2. /msgsrvmgr.cpp:4:26: fatal error: kdl/frames.hpp: No such file or directory #include <kdl/frames.hpp>

    /home/xxx/ros_workspace/src/bp_protocol_bridge/protospot/src/msgsrvmgr.cpp::: fatal error: kdl/frame ...

  3. redis事务和脚本

    事务,简单理解就是,一组动作,要么全部执行,要么就全部不执行.从而避免出现数据不一致的情况. redis提供了简单的事务功能,将一组需要的命令放到multi和exec两个命令之间.multi代表事务开 ...

  4. 一定要用Windows自带的记事本编辑 applicationHost.config

    访问IIS时,发生了一个 HipIISEngineStub.dll不能读取的问题.(Windows Event可以确认). 很容易在网上找到了对策, http://chrisfleischhacker ...

  5. Binding.RelativeSource 属性

    Binding.RelativeSource 属性说明: 通过指定绑定源相对于绑定目标的位置,获取或设置绑定源. 此属性通常用于将对象的某个属性绑定到该对象的另一个属性,或用于在样式或模板中定义绑定. ...

  6. linux利用软件raid搭建iscsi存储

    分区:parted /dev/sdbmklabel gptmkpart primary ext4 0% 100%set 1 raid mdadm -Cv /dev/md0 -n 4 -l5 /dev/ ...

  7. java读取PHP接口数据的实现方法(四)

    PHP文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...

  8. 039——VUE中组件之子组件中data使用实例与text-xtemplate的使用方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. phalcon 设置cookie一直是httponly导致前端读取不到cookie的值

    解决办法: 修改配置如果不好使,则暂时降低phalcon版本为3.1.2. 注意设置cookie的参数secure的值为false,否则js还是读取不到cookie

  10. windows 实用小工具(截图、进程管理)

    1. 截图 picpick:PicPick-NGWIN,一款全功能的设计工具,包含屏幕截图.图片编辑器.颜色选择器.像素标尺和其它更多的功能 2. 二进制/十六进制 十六进制(二进制)编辑器 3. p ...