Description

You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to n. Then we represent the color of vertex v as cv. The tree root is a vertex with number 1.

In this problem you need to answer to m queries. Each query is described by two integers vj, kj. The answer to query vj, kj is the number of such colors of vertices x, that the subtree of vertex vj contains at least kj vertices of color x.

You can find the definition of a rooted tree by the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory).

Input

The first line contains two integers n and m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105). The next line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105). The next n - 1 lines contain the edges of the tree. The i-th line contains the numbers ai, bi (1 ≤ ai, bi ≤ nai ≠ bi) — the vertices connected by an edge of the tree.

Next m lines contain the queries. The j-th line contains two integers vj, kj (1 ≤ vj ≤ n; 1 ≤ kj ≤ 105).

Output

Print m integers — the answers to the queries in the order the queries appear in the input.

Examples
Input
8 5
1 2 2 3 3 2 3 3
1 2
1 5
2 3
2 4
5 6
5 7
5 8
1 2
1 3
1 4
2 3
5 3
Output
2
2
1
0
1
Input
4 1
1 2 3 4
1 2
2 3
3 4
1 1
Output
4
正解:莫队算法
解题报告:
  今天心血来潮要刷莫队算法的题。
  明明是一棵树,居然可以用莫队?!好吧是我大惊小怪了,dfs一遍,搞出dfs序,然后直接转成序列问题,询问、颜色全部转成序列的模式。
  一样的维护就可以了。不过需要注意,我们记录每种颜色的出现次数,和至少出现某种次数的颜色个数。开始有一点想不通怎么O(1)更新至少某种次数,感觉要gi,后来发现,反正莫队一定是每次修改一位,那么最大贡献一定是1,那么我们一路更改下去就可以了,不虚。
  好神,%%%。
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
int n,m;
int ans[MAXM];
int yan[MAXN],col[MAXN];
int first[MAXN],to[MAXN*],next[MAXN*];
int L[MAXN],R[MAXN];
int ecnt;
int cnt[MAXM];//记录每种颜色的个数
int jilu[MAXM];//至少k个颜色的个数
int nowl,nowr; struct wen{
int l,r,id,k,belong;
}q[MAXM]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void dfs(int x,int fa){
L[x]=++ecnt; col[ecnt]=yan[x];
for(int i=first[x];i;i=next[i]) {
int v=to[i];
if(v!=fa) dfs(v,x);
}
R[x]=ecnt;
} inline bool cmp(wen p,wen pp){ if(p.belong==pp.belong) return p.r<pp.r; return p.belong<pp.belong; } inline void add(int x,int type){
if(type==) {
cnt[col[x]]++;
jilu[cnt[col[x]]]++;
}else{
jilu[cnt[col[x]]]--;
cnt[col[x]]--;
}
} inline void work(){
n=getint(); m=getint();
for(int i=;i<=n;i++) yan[i]=getint();
int x,y;
for(int i=;i<n;i++) {
x=getint(); y=getint();
next[++ecnt]=first[x]; to[ecnt]=y; first[x]=ecnt;
next[++ecnt]=first[y]; to[ecnt]=x; first[y]=ecnt;
}
ecnt=; dfs(,);
int size=sqrt(n);
for(int i=;i<=m;i++) {
x=getint(); q[i].l=L[x]; q[i].r=R[x]; q[i].id=i; q[i].k=getint();
q[i].belong=(q[i].l-)/size+;
}
sort(q+,q+m+,cmp);
for(int i=q[].l;i<=q[].r;i++) {
cnt[col[i]]++;
jilu[cnt[col[i]]]++;//只需一路添加就可以了,想想就知道并没有问题
}
ans[q[].id]=jilu[q[].k];
nowl=q[].l; nowr=q[].r;
for(int i=;i<=m;i++) {
while(nowl<q[i].l) add(nowl,-),nowl++;
while(nowl>q[i].l) add(nowl-,),nowl--;
while(nowr<q[i].r) add(nowr+,),nowr++;
while(nowr>q[i].r) add(nowr,-),nowr--;
ans[q[i].id]=jilu[q[i].k];
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
} int main()
{
work();
return ;
}
 

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

  1. Codeforces 375D D. Tree and Queries

    传送门 题意: 给一棵树,每个节点有一个颜色,询问x为根的子树,出现次数大于等于k的颜色个数. 输入格式: 第一行 2 个数 n,m 表示节点数和询问数. 接下来一行 n 个数,第 i 个数 ci ​ ...

  2. CodeForces - 710F:String Set Queries (二进制分组 处理 在线AC自动机)

    ou should process m queries over a set D of strings. Each query is one of three kinds: Add a string ...

  3. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

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

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

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

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

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

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

  7. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  8. codeforces 570 D Tree Requests

    题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个 ...

  9. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. mybatis order by绑定的参数

    <select id = "queryByStartWithOrder" resultType="org.seckill.entity.SuccessKilled& ...

  2. 理解SQL Server中的权限体系(上)----主体

    原文:http://www.cnblogs.com/CareySon/archive/2012/04/10/mssql-security-principal.html 简介 权限两个字,一个权力,一个 ...

  3. Knockout学习地址

    Knockout.js是什么? Knockout是一款很优秀的JavaScript库,它可以帮助你仅使用一个清晰整洁的底层数据模型(data model)即可创建一个富文本且具有良好的显示和编辑功能的 ...

  4. sqlserver字段类型详解(转)

    bit    整型 bit数据类型是整型,其值只能是0.1或空值.这种数据类型用于存储只有两种可能值的数据,如Yes 或No.True 或False .On 或Off. 注意:很省空间的一种数据类型, ...

  5. html的解析

    Web页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验 简单地说,页面渲染就是浏览器将html代码根据CSS定义的规则显示在浏览器窗口中的这个过程.先来大致了解一下浏览器都 ...

  6. Dungeon Game ——动态规划

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  7. System.Web.HttpRequestValidationException——从客户端检测到危险的Request值

    这是比较常见的问题了,如果Web表单中有输入类似于Html标签之类的文本,在通过Request.QueryString或者Request.Form传递这些值的时候,就会触发这样的异常,出于脚本注入等安 ...

  8. 自动化测试: sikuli,一个基于界面图像的gui测试框架

    http://www.sikuli.org/ license: MIT script language: Python 下面是他的一个hello world的例子,看看也挺有意思的. 开源的世界里有很 ...

  9. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  10. 实践2.4 ELF文件格式分析

    实践2.4 ELF文件格式分析 1.ELF文件头 查看/usr/include/elf.h文件: #define EI_NIDENT (16) typedef struct { unsigned ch ...