题意:给定一棵树,每个节点有颜色,对于每个询问(u,k)询问以u为根节点的子树下有多少种颜色出现次数>=k

因为是子树,跟dfs序有关,转化为一段区间,可以用莫队算法求解

直接用一个数组统计出现次数>=k的颜色

Code

#include <cstdio>
#include <algorithm>
#include <cmath>
#define N 100010
using namespace std; int n,m,A[N],bl[N],Ans[N],dfn[N],sum[N],tot,head[N],sz[N],col[N],tw[N];
struct node{int to,nex;}e[N*2];
struct info{
int l,r,k,id;
friend bool operator <(info a,info b){
return (bl[a.l]==bl[b.l])?a.r<b.r:a.l<b.l;
}
}q[N]; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} void upd(int x,int d){
if(d>0) sum[++col[tw[x]]]++;
else sum[col[tw[x]]--]--;
} void Link(int u,int v){
e[++tot].nex=head[u];e[tot].to=v;head[u]=tot;
} void dfs(int u,int fa){
dfn[u]=++tot,sz[u]=1,tw[tot]=A[u];
for(int i=head[u];i;i=e[i].nex)
if(e[i].to!=fa) dfs(e[i].to,u),sz[u]+=sz[e[i].to];
} int main(){
n=read(),m=read();int blo=sqrt(n);
for(int i=1;i<=n;++i) A[i]=read(),bl[i]=i/blo+1;
for(int i=1;i<n;++i){
int u=read(),v=read();
Link(u,v),Link(v,u);
}
tot=0,dfs(1,0);
for(int i=1;i<=m;++i){
int u=read(),k=read();
q[i]=(info){dfn[u],dfn[u]+sz[u]-1,k,i};
}
sort(q+1,q+m+1);
for(int i=1,l=1,r=0;i<=m;++i){
for(;l<q[i].l;l++) upd(l,-1);
for(;l>q[i].l;l--) upd(l-1,1);
for(;r<q[i].r;r++) upd(r+1,1);
for(;r>q[i].r;r--) upd(r,-1);
Ans[q[i].id]=sum[q[i].k];
}
for(int i=1;i<=m;printf("%d\n",Ans[i++]));
return 0;
}

[Codeforces375D]Tree and Queries(莫队算法)的更多相关文章

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

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

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

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

  3. cf375D. Tree and Queries(莫队)

    题意 题目链接 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. Sol 想到了主席树和启发式 ...

  4. 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)

    题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...

  5. HDU 3333 Turing Tree 莫队算法

    题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...

  6. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  7. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  8. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

  9. XOR and Favorite Number(莫队算法+分块)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. 在Git上创建新分支(实用性高!!!)

    在github上创建仓库: Create a new repository on the command line touch README.md git init git add README.md ...

  2. http 状态码集合

    HTTP常见状态码 200 301 302 404 500   HTTP状态码(HTTP Status Code) 状态码并不是每个都有,为了后期扩展.[update20170505] 一些常见的状态 ...

  3. 在android开发中如何使用JavaMail程序

    javaMail,是提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.我们可以基于JavaMail开发出类似于Microsoft  ...

  4. linux下composer+laravel随笔

    1.composer中文网:https://www.phpcomposer.com/   laravel中文网:https://d.laravel-china.org/ 2.composer是的作用是 ...

  5. framework7中一行的字如果过多就省略号显示的CSS写法

    .order-info-title { text-overflow: ellipsis !important; white-space: nowrap !important; overflow: hi ...

  6. DFS+BFS(POJ3083)

    题目链接:http://poj.org/problem?id=3083 解题报告:这个题目,搜最短路,没有什么问题.优先走左边,走右边,有很多说法,思路大概都相同,都是记录当前朝向,根据数学公式(i+ ...

  7. node执行环境

    nodejs本质上是一个javascript的执行环境,只是由于他的封装,加上更多web底层的一个处理,赋予了更多的能力,那么执行环境到底是什么呢,我们到浏览器里面体验看看,在chrome里面控制台, ...

  8. Ghostbusters(并查集,最小生成树)

    Ghostbusters 时间限制: 1 Sec  内存限制: 128 MB提交: 33  解决: 7[提交] [状态] [讨论版] [命题人:admin] 题目描述 The Bureau of Ap ...

  9. python同时遍历数组的索引和元素

    1.一般要同时遍历数组的索引和元素需要先确定数组的长度length(元素个数),然后使用range函数来生成数组的索引,最后使用该索引来访问数组的元素. 具体做法如下: l = [2,7,11,15] ...

  10. LIS的string用法

    题目链接 使用的是string里的find函数 stl大法好 #include<iostream> #include<cstdio> #include<cstring&g ...