[Codeforces375D]Tree and Queries(莫队算法)
题意:给定一棵树,每个节点有颜色,对于每个询问(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(莫队算法)的更多相关文章
- SPOJ COT2 Count on a tree II 树上莫队算法
题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...
- CodeForces 375D Tree and Queries 莫队||DFS序
Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...
- cf375D. Tree and Queries(莫队)
题意 题目链接 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. Sol 想到了主席树和启发式 ...
- 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)
题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...
- HDU 3333 Turing Tree 莫队算法
题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...
- HDU 4358 莫队算法+dfs序+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)T ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
随机推荐
- python面试题——框架和其他(132题)
一.框架对比 (1)django.flask.tornado框架的比较? Django:简单的说Django是一个大而全的Web框架,内置了很多组件,ORM.admin.Form. ModelForm ...
- Ienumerable和Ienumerator的使用
using UnityEngine; using System.Collections; public class TestCoroutine : MonoBehaviour { void Start ...
- python模块详解 random os
random模块 常用方法 random.random() 随机产生一个小于1的浮点数 import random print(random.random()) #0.4153761818276826 ...
- Strut2_声明式异常处理
Service 往外抛异常 public List<Category> list() throws SQLException{ Connection conn = DB.createCon ...
- 将 Azure SQL 内数据下载到本地,满足企业的「数据收集」
嫌长不看版 本文介绍了通过复制和导出两个操作,将 Azure SQL 数据库中的内容转移至其他位置(例如本地环境)的具体做法.借此可以帮助用户在 Azure 中运行数据库的同时,在本地或指定的其他位置 ...
- 如何将windows日志转成syslog格式并发到远程sysylog服务器
安装Snare, 随便找了个版本下载下来,安装一路next,除了中间让你输入一次http的管理登录口令. 2,配置 之后打开URL:http://192.168.37.23:6161/,输入默 ...
- 笨办法学Python(十八)
习题 18: 命名.变量.代码.函数 标题包含的内容够多的吧?接下来我要教你“函数(function)”了!咚咚锵!说到函数,不一样的人会对它有不一样的理解和使用方法,不过我只会教你现在能用到的最简单 ...
- server 2008 64位安装Rational错误
Administrator has detected that this is a terminal server session.Administrator does not support run ...
- 2018.9.8pat秋季甲级考试
第一次参加pat考试,结果很惨,只做了中间两道题,还有一个测试点错误,所以最终只得了不到50分.题目是甲级练习题的1148-1151. 考试时有些紧张,第一题第二题开始测试样例都运行不正确,但是调试程 ...
- 【转载】#370 - Subscribe to an Event by Adding an Event Handle
You subscribe to a particular event in C# by defining an event handler-code that will be called when ...