COT2 - Count on a tree II

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 : ask for how many different integers that represent the weight of nodes there are on the path from u to v.

Input

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

In the second line there are N integers. The i-th integer denotes the weight of the i-th 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 two integers u v, which means an operation asking for how many different integers that represent the weight of nodes there are on the path from u to v.

Output

For each operation, print its result.

Example

  1. Input:
  2. 8 2
  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 8
  11. 2 5
  12. 7 8
  1. Output:
  2. 4
  3. 4

 

题目链接:SPOJ COT2

一开始不太会,看着这个人的教程写的:树上莫队,然而还是有几个地方不是很懂而且他的简洁代码跟文中的一些变量名对不上号……,但至少没百度搜到的某些题解代码写的丑

他的主要做法就是把树用稍微修改的DFS序变成序列,然后根据两种不同的情况进行判断,并且过程中要算上LCA的贡献,代码虽然写出来了但是感觉跟文中的方法有一点出入,有待理解

最后的LCA判断实际上是多出来的一个点,要还原回去,否则指针是在区间移动的只管辖区间,这个多出来的点会造成错误的影响

代码:

  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define INF 0x3f3f3f3f
  5. #define LC(x) (x<<1)
  6. #define RC(x) ((x<<1)+1)
  7. #define MID(x,y) ((x+y)>>1)
  8. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  9. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  10. typedef pair<int, int> pii;
  11. typedef long long LL;
  12. const double PI = acos(-1.0);
  13. const int N = 40010;
  14. const int M = 200010;
  15. struct edge
  16. {
  17. int to, nxt;
  18. edge() {}
  19. edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
  20. };
  21. struct query
  22. {
  23. int u, v;
  24. int lca, x;
  25. int id, l, r, b;
  26. bool operator<(const query &rhs)const
  27. {
  28. if (b != rhs.b)
  29. return b < rhs.b;
  30. return r < rhs.r;
  31. }
  32. };
  33. edge E[N << 1];
  34. query Q[M];
  35. int head[N], tot;
  36.  
  37. int arr[N];//values in nodes
  38. int ver[N << 1], F[N], D[N << 1], dp[N << 1][18], ts; //for LCA
  39. int ST[N << 1], EN[N << 1], A[N << 1], sz, unit; //for Mo's algo
  40.  
  41. vector<int>vec;
  42. int ans[M], cnt[N];
  43. int cnode[N];
  44.  
  45. void init()
  46. {
  47. CLR(head, -1);
  48. tot = 0;
  49. ts = 0;
  50. sz = 0;
  51. vis.reset();
  52. vec.clear();
  53. CLR(ans, 0);
  54. CLR(cnt, 0);
  55. CLR(cnode, 0);
  56. }
  57. inline void add(int s, int t)
  58. {
  59. E[tot] = edge(t, head[s]);
  60. head[s] = tot++;
  61. }
  62. void dfs(int u, int pre, int d)
  63. {
  64. ver[++ts] = u;
  65. D[ts] = d;
  66. F[u] = ts;
  67.  
  68. ST[u] = ++sz;
  69. A[sz] = u;
  70.  
  71. for (int i = head[u]; ~i; i = E[i].nxt)
  72. {
  73. int v = E[i].to;
  74. if (v != pre)
  75. {
  76. dfs(v, u, d + 1);
  77.  
  78. ver[++ts] = u;
  79. D[ts] = d;
  80. }
  81. }
  82.  
  83. EN[u] = ++sz;
  84. A[sz] = u;
  85. }
  86. void RMQ_init(int l, int r)
  87. {
  88. int i, j;
  89. for (i = l; i <= r; ++i)
  90. dp[i][0] = i;
  91. for (j = 1; l + (1 << j) - 1 <= r; ++j)
  92. {
  93. for (i = l; i + (1 << j) - 1 <= r; ++i)
  94. {
  95. int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1];
  96. dp[i][j] = D[a] < D[b] ? a : b;
  97. }
  98. }
  99. }
  100. int LCA(int u, int v)
  101. {
  102. int l = F[u], r = F[v];
  103. if (l > r)
  104. swap(l, r);
  105. int k = log2(r - l + 1);
  106. int a = dp[l][k], b = dp[r - (1 << k) + 1][k];
  107. return D[a] < D[b] ? ver[a] : ver[b];
  108. }
  109. inline void Add(const int &u,int &Ans)
  110. {
  111. ++cnode[u];
  112. if (cnode[u] == 1)
  113. {
  114. if (++cnt[arr[u]] == 1)
  115. ++Ans;
  116. }
  117. else if (cnode[u] == 2)
  118. {
  119. if (--cnt[arr[u]] == 0)
  120. --Ans;
  121. }
  122. }
  123. inline void Del(const int &u, int &Ans)
  124. {
  125. --cnode[u];
  126. if (cnode[u] == 0)
  127. {
  128. if (--cnt[arr[u]] == 0)
  129. --Ans;
  130. }
  131. else if (cnode[u] == 1)
  132. {
  133. if (++cnt[arr[u]] == 1)
  134. ++Ans;
  135. }
  136. }
  137. int main(void)
  138. {
  139. int n, m, i;
  140. while (~scanf("%d%d", &n, &m))
  141. {
  142. init();
  143. for (i = 1; i <= n; ++i)
  144. {
  145. scanf("%d", &arr[i]);
  146. vec.push_back(arr[i]);
  147. }
  148.  
  149. sort(vec.begin(), vec.end());
  150. vec.erase(unique(vec.begin(), vec.end()), vec.end());
  151. for (i = 1; i <= n; ++i)
  152. arr[i] = lower_bound(vec.begin(), vec.end(), arr[i]) - vec.begin() + 1;
  153.  
  154. for (i = 1; i < n; ++i)
  155. {
  156. int u, v;
  157. scanf("%d%d", &u, &v);
  158. add(u, v);
  159. add(v, u);
  160. }
  161. dfs(1, -1, 0);
  162. unit = sqrt(sz);
  163. RMQ_init(1, ts);
  164. for (i = 0; i < m; ++i)
  165. {
  166. scanf("%d%d", &Q[i].u, &Q[i].v);
  167. Q[i].id = i;
  168. Q[i].lca = LCA(Q[i].u, Q[i].v);
  169.  
  170. if (ST[Q[i].u] > ST[Q[i].v])
  171. swap(Q[i].u, Q[i].v);
  172. if (Q[i].lca == Q[i].u)
  173. {
  174. Q[i].l = ST[Q[i].u];
  175. Q[i].r = ST[Q[i].v];
  176. Q[i].x = 0;
  177. }
  178. else
  179. {
  180. Q[i].l = EN[Q[i].u];
  181. Q[i].r = ST[Q[i].v];
  182. Q[i].x = 1;
  183. }
  184. Q[i].b = Q[i].l / unit;
  185. }
  186. sort(Q, Q + m);
  187. int L = Q[0].l, R = L - 1;
  188. int Ans = 0;
  189. for (i = 0; i < m; ++i)
  190. {
  191. while (L > Q[i].l)
  192. Add(A[--L], Ans);
  193.  
  194. while (L < Q[i].l)
  195. Del(A[L++], Ans);
  196.  
  197. while (R > Q[i].r)
  198. Del(A[R--], Ans);
  199.  
  200. while (R < Q[i].r)
  201. Add(A[++R], Ans);
  202.  
  203. if (Q[i].x)
  204. Add(Q[i].lca, Ans);
  205.  
  206. ans[Q[i].id] = Ans;
  207.  
  208. if (Q[i].x)
  209. Del(Q[i].lca, Ans);
  210. }
  211. for (i = 0; i < m; ++i)
  212. printf("%d\n", ans[i]);
  213. }
  214. return 0;
  215. }

SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)的更多相关文章

  1. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  2. SPOJ COT2 Count on a tree II (树上莫队,倍增算法求LCA)

    题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...

  3. spoj COT2 - Count on a tree II 树上莫队

    题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include < ...

  4. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  5. SPOJ COT2 Count on a tree II (树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...

  6. SPOJ COT2 Count on a tree II(树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...

  7. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  8. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  9. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

随机推荐

  1. Problem A: 李白打酒

    Problem A: 李白打酒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 825  Solved: 373[Submit][Status][Web ...

  2. 2018.2.5 PHP如何写好一个程序用框架

    随着PHP标准和Composer包管理工具的面世,普通开发者撸一个框架已经不再是什么难事了. 无论是路由管理.ORM管理.还是视图渲染都有许许多多优秀的包可以使用.我们就像堆积木一样把这些包用comp ...

  3. 解决mysql8小时无连接自动断掉机制

    windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 MySQL是一个小型关系型数据库管理系统,由于MySQL ...

  4. MySQL 5.7 在线启用和关闭GTID

    1.相关基础 MySQL 5.7.6之后GTID_MODE提供了两个新的选项分别为ON_PERMISSIVE和OFF_PERMISSIVEOFF_PERMISSIVE:不产生GTID事务, Slave ...

  5. java 程序设计第三次作业内容

    第一题:输出结果是什么? System.out.println("5+5="+5+5); 第二题:输出结果是什么? int a=3,b; b=a++; sop("a=&q ...

  6. SpringBoot之自动配置原理

    我在前面的Helloworld的程序中已经分析过一次,配置原理了: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration 2).@En ...

  7. tab菜单的点击的动态效果和内容页面的关联显示jQuery

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

  8. 01 python爬虫

    ---

  9. JVM 内存分配和回收策略

    对象的内存分配,主要是在java堆上分配(有可能经过JIT编译后被拆为标量类型并间接地在栈上分配),如果启动了本地线程分配缓冲,将按线程优先在TLAB上分配.少数情况下也是直接分配到老年代,分配规则不 ...

  10. 使用U盘给笔记本重做系统

    **一.戴尔 Vostro 14 3000 Series **1. 开机时快速按F12进入BIOS界面 **2. 按照下图进行一系列的处理,把U盘被设置为第一启动项 **3. 插入U盘后进入老毛桃PE ...