dsu on tree

题目链接

点我跳转

题目大意

给定一棵 \(n\) 个节点的树,根节点为 \(1\)。每个节点上有一个颜色 \(c_i\)

\(m\) 次询问。

每次询问给出 \(u\) \(k\):询问在以 \(u\) 为根的子树中,出现次数 \(≥k\) 的颜色有多少种。

解题思路

可以开棵权值线段树

如果当前颜色出现的次数 \(cnt[i] = x\), 就把树的第 \(x\) 个位置的值 \(+ 1\)

那么对于每个询问的 \(k\) 输出树的第 \(k\) 个位置的值即可

AC_Code

  1. #include<bits/stdc++.h>
  2. #define rep(i,a,n) for (int i=a;i<=n;i++)
  3. #define per(i,n,a) for (int i=n;i>=a;i--)
  4. #define int long long
  5. #define pb push_back
  6. #define fi first
  7. #define se second
  8. using namespace std;
  9. const int N = 3e5 + 10;
  10. struct Tree{
  11. int l , r , lazy , sum;
  12. }tree[N << 2];
  13. void push_up(int rt)
  14. {
  15. tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
  16. }
  17. void push_down(int rt)
  18. {
  19. int x = tree[rt].lazy;
  20. tree[rt].lazy = 0;
  21. tree[rt << 1].lazy = tree[rt << 1 | 1].lazy = x;
  22. tree[rt << 1].sum += (tree[rt << 1].r - tree[rt << 1].l + 1) * x;
  23. tree[rt << 1 | 1].sum += (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1) * x;
  24. }
  25. void build(int l , int r , int rt)
  26. {
  27. tree[rt].l = l , tree[rt].r = r , tree[rt].lazy = 0;
  28. if(l == r)
  29. {
  30. tree[rt].sum = 0;
  31. return ;
  32. }
  33. int mid = l + r >> 1;
  34. build(l , mid , rt << 1);
  35. build(mid + 1 , r , rt << 1 | 1);
  36. push_up(rt);
  37. }
  38. void update_range(int L , int R , int rt , int val)
  39. {
  40. int l = tree[rt].l , r = tree[rt].r;
  41. if(L <= l && r <= R)
  42. {
  43. tree[rt].lazy += val;
  44. tree[rt].sum += (r - l + 1) * val;
  45. return ;
  46. }
  47. push_down(rt);
  48. int mid = l + r >> 1;
  49. if(L <= mid) update_range(L , R , rt << 1 , val);
  50. if(R > mid) update_range(L , R , rt << 1 | 1 , val);
  51. push_up(rt);
  52. }
  53. int query_range(int L , int R , int rt)
  54. {
  55. int l = tree[rt].l , r = tree[rt].r;
  56. if(L <= l && r <= R) return tree[rt].sum;
  57. push_down(rt);
  58. int mid = l + r >> 1 , ans = 0;
  59. if(L <= mid) ans += query_range(L , R , rt << 1);
  60. if(R > mid) ans += query_range(L , R , rt << 1 | 1);
  61. return ans;
  62. }
  63. struct Edge{
  64. int nex , to;
  65. }edge[N << 1];
  66. int head[N] , TOT;
  67. void add_edge(int u , int v)
  68. {
  69. edge[++ TOT].nex = head[u] ;
  70. edge[TOT].to = v;
  71. head[u] = TOT;
  72. }
  73. int dep[N] , sz[N] , hson[N] , HH;
  74. int col[N] , n , m , up;
  75. int cnt[N] , sum[N];
  76. vector<pair<int , int>>Q[N] , ans;
  77. void dfs(int u , int far)
  78. {
  79. dep[u] = dep[far] + 1;
  80. sz[u] = 1;
  81. for(int i = head[u] ; i ; i = edge[i].nex)
  82. {
  83. int v = edge[i].to;
  84. if(v == far) continue ;
  85. dfs(v , u);
  86. sz[u] += sz[v];
  87. if(sz[v] > sz[hson[u]]) hson[u] = v;
  88. }
  89. }
  90. void calc(int u , int far, int val)
  91. {
  92. cnt[col[u]] += val;
  93. if(val == 1)
  94. {
  95. int k = cnt[col[u]];
  96. update_range(k , k , 1 , 1);
  97. }
  98. if(val == -1)
  99. {
  100. int k = cnt[col[u]] + 1;
  101. update_range(k , k , 1 , -1);
  102. }
  103. for(int i = head[u] ; i ; i = edge[i].nex)
  104. {
  105. int v = edge[i].to;
  106. if(v == far || v == HH) continue ;
  107. calc(v , u , val);
  108. }
  109. }
  110. void dsu(int u , int far , int op)
  111. {
  112. for(int i = head[u] ; i ; i = edge[i].nex)
  113. {
  114. int v = edge[i].to;
  115. if(v == far || v == hson[u]) continue ;
  116. dsu(v , u , 0);
  117. }
  118. if(hson[u]) dsu(hson[u] , u , 1) , HH = hson[u];
  119. calc(u , far , 1);
  120. for(auto i : Q[u])
  121. {
  122. int id = i.fi , k = i.se;
  123. int res = query_range(k , k , 1);
  124. ans.pb(make_pair(id , res));
  125. }
  126. HH = 0;
  127. if(!op) calc(u , far , -1);
  128. }
  129. signed main()
  130. {
  131. ios::sync_with_stdio(false);
  132. cin.tie(0) , cout.tie(0);
  133. cin >> n >> m;
  134. rep(i , 1 , n) cin >> col[i];
  135. rep(i , 1 , n - 1)
  136. {
  137. int u , v;
  138. cin >> u >> v;
  139. add_edge(u , v) , add_edge(v , u);
  140. }
  141. rep(i , 1 , m)
  142. {
  143. int u , k;
  144. cin >> u >> k;
  145. Q[u].pb(make_pair(i , k));
  146. }
  147. build(1 , 100000 , 1);
  148. dfs(1 , 0);
  149. dsu(1 , 0 , 0);
  150. sort(ans.begin() , ans.end());
  151. for(auto i : ans) cout << i.se << '\n';
  152. return 0;
  153. }

Codeforces375D Tree and Queries的更多相关文章

  1. [Codeforces375D]Tree and Queries(莫队算法)

    题意:给定一棵树,每个节点有颜色,对于每个询问(u,k)询问以u为根节点的子树下有多少种颜色出现次数>=k 因为是子树,跟dfs序有关,转化为一段区间,可以用莫队算法求解 直接用一个数组统计出现 ...

  2. [Codeforces Round #221 (Div. 1)][D. Tree and Queries]

    题目链接:375D - Tree and Queries 题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次 题解:先对所有的询问进行 ...

  3. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  4. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

  5. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  6. CF375D Tree and Queries

    题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...

  7. CodeForces 376F Tree and Queries(假·树上莫队)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  8. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  9. CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

随机推荐

  1. dubbo配置的覆盖关系

    dubbo推荐在Provider上尽量多配置Consumer端属性: 由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,如果一个消费方同时引用多个服务,就不需要关心每个服务的参数设 ...

  2. 普通人如何站在时代风口学好AI?这是我看过最好的答案

    摘要:当前,数据.算法.算力的发展突破正推动AI应用的逐步落地. AI是什么? 根据维基百科的定义,人工智能是一种新的通用目的技术(GPT, General Purpose Technology),它 ...

  3. Graph-GCN

    前言 在大型图中,节点的低维向量embedding被证明了作为各种各样的预测和图分析任务的特征输入是非常有用的.顶点embedding最基本的基本思想是使用降维技术从高维信息中提炼一个顶点的邻居信息, ...

  4. D. Tavas and Malekas 解析(字串匹配)

    Codeforce 535 D. Tavas and Malekas 解析(字串匹配) 今天我們來看看CF535D 題目連結 題目 給你一個字串$p$和一些$index$代表字串$p$在哪些位置會和長 ...

  5. MVC IIS 403.14

    描述:HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this direc ...

  6. 21个写SQL的好习惯,你值得拥有

    前言 每一个好习惯都是一笔财富,本文分SQL后悔药, SQL性能优化,SQL规范优雅三个方向,分享写SQL的21个好习惯,谢谢阅读,加油哈~ 公众号:捡田螺的小男孩 1. 写完SQL先explain查 ...

  7. 4-Kotlin数据类型与变量

    一. 变量概念 在Kotlin中变量其实对应着内存中特定区域,变量就像是一个装"数据"的容器 1 val num1:Int = 100 2 var num2:Int = 99 关键 ...

  8. STM32入门系列-GPIO概念介绍

    GPIO(general purpose intput output)是通用输入输出端口的简称,可以通过软件来控制其输入和输出.STM32 芯片的 GPIO 引脚与外部设备连接起来,从而实现与外部通讯 ...

  9. Android Choreographer 源码分析

    Choreographer 的作用主要是配合 Vsync ,给上层 App 的渲染提供一个稳定的 Message 处理的时机,也就是 Vsync 到来的时候 ,系统通过对 Vsync 信号周期的调整, ...

  10. Vue+Antd搭配百度地图实现搜索定位等功能

    前言 最近,在做vue项目的时候有做到选择地址功能,而原项目中又引入了百度地图,所以我就打算通过使用百度地图来实现地址搜索功能啦. 本次教程可能过于啰嗦,所以这里先放上预览地址供大家预览--点我预览, ...