传送门: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_JUDGE
FIN
#endif
scanf("%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;
}

CodeForces 375D Tree and Queries的更多相关文章

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

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

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

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

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

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

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

  5. codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队

    题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...

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

  7. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

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

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

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

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

随机推荐

  1. APP上线前,如何做运营推广工作?

    http://www.cocoachina.com/market/20150723/12731.html 一 竞品分析 1.选择竞品,做好定位(选择两个产品最好,最多三个). 如何获取竞品? A 百度 ...

  2. 搜索docker镜像

    docker最简单的方式莫过于从现有的容器镜像开始. Docker官方网站专门有一个页面来存储所有可用的镜像,网址是:index.docker.io. 可以通过浏览这个网页来查找你想要使用的镜像,或者 ...

  3. 在window.onload中使用setTimeout

    window.onload = function(){ function n(i){ alert(1); } setTimeout('n(1)',2000); } 以上代码会报错:n() is not ...

  4. Oracle错误——引发ORA-01843:无效的月份。

    问题 引发ORA-01843:无效的月份. 解决 改动client会话日期的语言: ALTER SESSION SET nls_date_language='american';

  5. 巨蟒python全栈开发-第11阶段 ansible3_1入门四个模块command&shell&script&copy

    大纲 1.系统安装与机器克隆 2.ansible介绍和host-pattern格式 3.command模块 4.shell模块 5.script模块 6.copy模块

  6. hdu 2089 不要62【数位dp】

    HDU 2089 求给定区间内不含62和4的数的个数. 数位dp入门.从这里我清楚了一些数位dp的用法.比如limit是判断是否达到上界,而且需要判断(!limit)..比如若题目要求不含11的个数, ...

  7. Android学习:导入工程时报错The import android cannot be resolved

    今天在导入别人的工程时,出现了一个这个问题The import android cannot be resolved 就是找不到import android.support.v7.app.Action ...

  8. day13 SQLAlchemy

    ORM:也叫关系对象映射 本篇要点: 原生模块 pymsql ORM框架 SQLAchemy pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 需 ...

  9. [asp.net]登录协同工作平台安全解决方式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/david_520042/article/details/25372207 [摘要]公司领导说登录验证 ...

  10. 00docker安装和简介

    Docker是用于开发.装载和运行应用的开放平台.Docker项目的目标是实现轻量的操作系统级虚拟化解决方案,它提供了一种在容器中安全隔离地运行应用程序的方式.可以在宿主机上运行多个容器. Docke ...