传送门:https://codeforces.com/problemset/problem/375/D

题意:

给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内满足某种颜色的数量大于k的颜色一共有多少种

题解:

冷静分析,胡乱分析,询问次数这么多,但是并没有修改操作,倘若把询问离线下来就好了

可是询问问的是每个点的树

这个时候我们的dfs序就可以派上用场了

dfs序:"所谓DFS序, 就是DFS整棵树依次访问到的结点组成的序列"

"DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题"

通过对树进行一次dfs序操作,我们可以得到每个点他的子树所在的一段区间,这样我们就可以得到每个询问在dfs序下询问的区间了,那么我们怎么统计区间内颜色数量大于k的种类数量呢?

由于我们得到了离线下来的询问区间,用莫队算法可以在O(nsqrt(n))的时间内得到所有询问的答案

由于每次询问是查询区间内颜色种类的数量之和,我们需要用一个区间求和,单点修改的数据结构来维护莫队时统计答案和修改操作,因此我们可以用树状数组或者线段树来维护

代码:

/***        ┏┓    ┏┓*        ┏┛┗━━━━━━━┛┗━━━┓*        ┃       ┃  *        ┃   ━    ┃*        ┃ >   < ┃*        ┃       ┃*        ┃... ⌒ ...  ┃*        ┃       ┃*        ┗━┓   ┏━┛*          ┃   ┃ Code is far away from bug with the animal protecting          *          ┃   ┃   神兽保佑,代码无bug*          ┃   ┃           *          ┃   ┃        *          ┃   ┃*          ┃   ┃           *          ┃   ┗━━━┓*          ┃       ┣┓*          ┃       ┏┛*          ┗┓┓┏━┳┓┏┛*           ┃┫┫ ┃┫┫*           ┗┻┛ ┗┻┛*/
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
struct EDGE {int v, nxt;
} edge[maxn * 2];
int head[maxn];
int tot;
void add_edge(int u, int v) {edge[tot].v = v;edge[tot].nxt = head[u];head[u] = tot++;
}
int n, m;
int col[maxn];
int dfn[maxn], st[maxn], ed[maxn], cnt;
int vis[maxn];
int num[maxn];
int bit[maxn];
int ans[maxn];
void dfs(int u, int fa, int depth) {st[u] = ++cnt;num[cnt] = col[u];for(int i = head[u]; i != -1; i = edge[i].nxt) {int v = edge[i].v;if(v != fa) {dfs(v, u, depth + 1);}}ed[u] = cnt;
}
vector<int> vec[maxn];
int v[maxn];
int k[maxn];
struct node {int l, r, id, k;
} q[maxn];
int pos[maxn];
bool cmp(node a, node b) {if(pos[a.l] == pos[b.l]) return a.r < b.r;return pos[a.l] < pos[b.l];
}
int lowbit(int x) {return x & -x;
}
void update(int pos, int x) {if(pos <= 0) return;while(pos < maxn) {bit[pos] += x;pos += lowbit(pos);}
}
int query(int pos) {int ans = 0;while(pos) {ans += bit[pos];pos -= lowbit(pos);}return ans;
}
void add(int x) {update(vis[num[x]], -1);vis[num[x]]++;update(vis[num[x]], 1);
}
void del(int x) {update(vis[num[x]], -1);vis[num[x]]--;update(vis[num[x]], 1);
}
int main() {
#ifndef ONLINE_JUDGEFIN
#endifscanf("%d%d", &n, &m);for(int i = 1; i <= n; i++) {scanf("%d", &col[i]);}memset(head, -1, sizeof(head));tot = cnt = 0;for(int i = 1, u, v; i <= n - 1; i++) {scanf("%d%d", &u, &v);add_edge(u, v);add_edge(v, u);}dfs(1, 0, 1);int sz = sqrt(cnt);for(int i = 1; i <= cnt; i++) {pos[i] = i / sz;}for(int i = 1; i <= m; i++) {scanf("%d%d", &v[i], &k[i]);q[i].l = st[v[i]];q[i].r = ed[v[i]];q[i].id = i;q[i].k = k[i];}sort(q + 1, q + m + 1, cmp);int L = 1, R = 0;for(int i = 1; i <= m; i++) {while(L < q[i].l) {L++;del(L - 1);}while(L > q[i].l) {add(L - 1);L--;}while(R < q[i].r) {R++;add(R);}while(R > q[i].r) {del(R);R--;}ans[q[i].id] = query(maxn - 1) - query(q[i].k - 1);}for(int i = 1; i <= m; i++) {printf("%d\n", ans[i]);}return 0;
}

转载于:https://www.cnblogs.com/buerdepepeqi/p/10897505.html

CodeForces 375D Tree and Queries相关推荐

  1. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  2. CodeForces - 375D Tree and Queries(树上启发式合并)

    题目链接:点击查看 题目大意:给出一棵有根树,每个节点都有一个编号代表颜色,现在给出m个询问,每个询问的形式为u k,需要回答以u为根节点的子树中,数量超过k的颜色有多少种 题目分析:树上启发式合并模 ...

  3. CodeForces - 375D Tree and Queries 树启 + 思维

    传送门 题意: 思路: 很明显子树问题会想到树启,让后如何updateupdateupdate呢?一个显然的思路就是维护一个树状数组,查询次数>=kj>=k_j>=kj​的个数.但是 ...

  4. CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】

    传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...

  5. cf375D. Tree and Queries

    cf375D. Tree and Queries 题意: 给你一颗有根树,每个点都有一个颜色,有m次询问,问以u为根的子树中,相同颜色数量超过k的有多少种颜色? 题解: 这个题做法很多,有莫队分块,这 ...

  6. CodeForces - 1328E Tree Queries(dfs序/LCA)

    题目链接:点击查看 题目大意:给出一棵以点 1 为根节点的树,接下来有 m 次询问,每次询问给出 k 个点,题目问我们能否找到一个点 u ,使得从根节点到点 u 的简单路径,到 k 个点的每个点的距离 ...

  7. CF375D Tree and Queries(dsu on tree)

    题解: 个人感觉这个题目是一个挺不错的题的.(这里就不说dsu on tree的这个过程是什么了) 我们在dsu on tree计算答案时需要有一个小小技巧去优化一下时间复杂度. 就是当我们用一个cn ...

  8. Codeforces 911F Tree Destruction

    Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...

  9. CodeForces - 1217F Forced Online Queries Problem(线段树分治+并查集撤销)

    题目链接:点击查看 题目大意:给出 nnn 个点,初始时互相不存在连边,需要执行 mmm 次操作,每次操作分为两种类型: 1xy1 \ x \ y1 x y:如果 (x,y)(x,y)(x,y) 之间 ...

最新文章

  1. 视觉在无人驾驶中的应用及分类_机器视觉可以应用于水果自动分类拣选,你见过吗?...
  2. 六十六、Leetcode数组系列(中篇)
  3. 春运12306的bug
  4. docker启动报错  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 9876 -j DNAT --
  5. LeetCode 332. 重新安排行程(欧拉路径)
  6. 六度分离(HDU-1869)
  7. 10 个最佳的支持触摸操作的 JavaScript 框架
  8. MySQL集群和主从复制分别适合在什么场景下使用
  9. 电路分析基础知识点总结
  10. SSH和SSM对比(学完后的总结)
  11. windows配置环境变量和path环境后即时生效
  12. 如何检查java代码有误_Java代码查错题
  13. ThinkPad蓝牙鼠标出现延迟、断开连接等问题的解决办法
  14. 浅谈Redis数据类型
  15. linux用户自动输入密码,Linux自动输入密码登录用户
  16. 花了1个月时间,把Python库全部整理出来了,覆盖所有,建议收藏
  17. 字符串匹配 - Overview
  18. 头文件 INTRINS.H 的用法
  19. 模块-E18-D80NK红外避障传感器
  20. 专业订制|软件开发|系统开发|网页设计|做网站|企业建站|网站建设

热门文章

  1. Docker入门六部曲——服务
  2. c/c++中的const
  3. 时间处理_pandas_时间处理小结
  4. logistic 损失函数的解释
  5. BERT大火却不懂Transformer?读这一篇就够了 原版 可视化机器学习 可视化神经网络 可视化深度学习...20201107
  6. TensorFlow实现超参数调整
  7. TensorFlow编程结构
  8. CUDA 7 流并发性优化
  9. 嵌入式Linux设备驱动程序:在运行时读取驱动程序状态
  10. HDR sensor 原理介绍