题意:给定一棵树,每个节点有颜色,对于每个询问(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. python反爬之用户代理

    # requests是第三方库,需要安装 pip install requests import requests import random # 通常很多网站都会设置检测请求头中的User-Agen ...

  2. 文本框textarea根据输入内容自适应高度 和输入中文和数字换行解决方法

    textarea内容可从后台读取或手动输入,常规输入后中文和数字会出现换行问题 <style> #textarea { display: block; margin: 0 auto; ov ...

  3. Odoo (OpenERP/TinyERP)-10.0 (Debian 8)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: odoo-10.0 commercial erp odoo open source openerp tinyerp 服务优惠价: 按服务商许可协议 ...

  4. oracle自动异地备份数据库

    需求:实现oracle自动异地备份数据库 分析:1.oracle备份数据库     2.自动备份(定时)   3.非本地备份(因为如果备份到本地的话,如果硬件设备损坏可能导致数据丢失) 知识点:1.备 ...

  5. leetcode: 贪心

    1. jump game Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. Arduino-定义串口

    在一个老外写的代码中找到了一个非常好的定义串口的方法!   Arduino用下面这种方法定义串口可以方便的把协议应用的任意的端口,大大提高了代码的修改性和移植性.       以下是范例:       ...

  7. 命令行输入Jmeter提示不是内部或外部命令,处理方式:添加环境变量

    命令行输入Jmeter提示不是内部或外部命令,需要在环境变量path中添加jmeter的bin目录绝对路径 我的电脑 > 右击 >属性 > 高级系统变量 > 环境变量 > ...

  8. framework7滑动删除列表触发chrome 报错解决办法

    使用 <div class="list-block"> <ul> <li class="swipeout"> <div ...

  9. Hash模板

    ;//一般为靠近总数的素数 struct Hashtable { int x;//hash存的值 Hashtable * next; Hashtable() { next = ; } }; Hasht ...

  10. ELKB是什么?

    ELKB是四个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana ,Beats. Elasticsearch是个开源分布式搜索引擎,提供搜集.分析.存储数据三大 ...