bhupixb

Mo's Algorithm

Mar 21st, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. // Seize the moment try to freeze it and own it, squeeze it and hold it,
  2. // ‘Cause we consider these minutes golden.
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. #define MP make_pair
  9. #define F first
  10. #define S second
  11. #define pi pair<int,int>
  12.  
  13. const unsigned mod = 1e9 + 7;
  14. const unsigned N = 1e5 + 4;
  15.  
  16. int color[N], arr[N];
  17. int id[N], ind, sz[N];
  18. vector<int> g[N];
  19. int ans[N];
  20.  
  21. void dfs(int to, int p) {
  22. sz[to] = 1;
  23. arr[++ind] = to;
  24. id[to] = ind;
  25. for (int from: g[to]) {
  26. if (from != p) {
  27. dfs(from, to);
  28. sz[to] += sz[from];
  29. }
  30. }
  31. }
  32.  
  33. const int block = 431;
  34.  
  35. struct query {
  36. int l, r, idx, k;
  37. bool operator<(const query &y) const {
  38. int a = l / block, b = y.l / block;
  39. if (a == b) return r < y.r; // sort by r if both are in same block
  40. return a < b; // sort by block number if in different blocks
  41. }
  42. };
  43.  
  44. int glAns[N], freq[N];
  45.  
  46. inline void del(int cl) {
  47. if (glAns[color[arr[cl]]] > 0) {
  48. freq[glAns[color[arr[cl]]]] -= 1;
  49. }
  50. glAns[color[arr[cl]]] -= 1;
  51. }
  52. inline void add(int cl) {
  53. glAns[color[arr[cl]]] += 1;
  54. freq[glAns[color[arr[cl]]]] += 1;
  55. }
  56.  
  57. signed main() {
  58. ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  59.  
  60. int n, q, u, v;
  61. cin >> n >> q;
  62.  
  63. for (int i = 1; i <= n; ++i) {
  64. cin >> color[i];
  65. }
  66.  
  67. for (int i = 1; i < n; ++i) {
  68. cin >> u >> v;
  69. g[u].push_back(v);
  70. g[v].push_back(u);
  71. }
  72.  
  73. dfs(1, 0);
  74.  
  75. /*for (int i = 1; i <= ind; ++i) {
  76. cout << arr[i] << ' ' << id[i] << '\n';
  77. }*/
  78.  
  79. vector<query> Q(q);
  80.  
  81. for (int i = 0; i < q; ++i) {
  82. cin >> u >> v;
  83. Q[i].l = id[u];
  84. Q[i].r = id[u] + sz[u] - 1;
  85. Q[i].idx = i;
  86. Q[i].k = v;
  87. }
  88.  
  89. sort(Q.begin(), Q.end());
  90.  
  91. int cl = 0, cr = 0;
  92. // cl indicates -> excluded from [1, cl)
  93. // cr indicates -> included from [1, cr)
  94.  
  95. for (const auto it: Q) {
  96. int l = it.l, r = it.r;
  97.  
  98. // cout << "main : " << l << ' ' << r << '\n';
  99. // cout << "before : " << cl << ' ' << cr << '\n';
  100.  
  101. while (cl < l) {
  102. del(cl);
  103. ++cl;
  104. }
  105. while (cl > l) {
  106. add(cl - 1); // cl will be one index ahead of included index so cl - 1
  107. --cl;
  108. }
  109. while (cr <= r) {
  110. add(cr);
  111. ++cr; // at last when cr == r it points to r + 1
  112. }
  113. while (cr > r + 1) {
  114. del(cr - 1);
  115. --cr;
  116. }
  117.  
  118. // cout << "after : " << cl << ' ' << cr << '\n';
  119.  
  120. ans[it.idx] = freq[it.k];
  121. }
  122.  
  123. for (int i = 0; i < q; ++i) {
  124. cout << ans[i] << '\n';
  125. }
  126.  
  127. return 0;
  128. }
Add Comment
Please, Sign In to add comment