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 - 1lines 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

题意:
给定一颗树,树上的每个节点都有一个颜色,以1为根节点。
每次询问,问以节点v为根节点的子树里面,有多少种颜色出现次数大于k。
思路:
想到莫队之后,dfs序和树状数组很好想了。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int col[maxn];
int num[maxn];
int vis[maxn];
int cnt,Head[maxn];
int tl[maxn],tr[maxn];
struct node{
int v,nxt;
}e[*maxn];
struct query{
int l,r,id,k;
}a[maxn]; void add_edge(int u,int v){
e[cnt].v=v;
e[cnt].nxt = Head[u];
Head[u]=cnt++;
} void dfs(int u,int fa){
tl[u]=++cnt;
num[cnt]=col[u];
for(int k=Head[u];~k;k=e[k].nxt){
if(e[k].v!=fa)dfs(e[k].v,u);
}
tr[u]=cnt;
}
int block;
bool cmp(query a,query b){
if(a.l/block!=b.l/block){return a.l<b.l;}
return a.r<b.r;
}
int lowbit(int x){
return x&-x;
} int bit[maxn]; int query(int pos){
int ans=;
while(pos){
ans+=bit[pos];
pos-=lowbit(pos);
}
return ans;
} void update(int pos,int val){
if(pos<=){return;}
while(pos<maxn){
bit[pos]+=val;
pos+=lowbit(pos);
}
} int ans[maxn]; int main()
{
memset(Head,-,sizeof(Head));
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&col[i]);
} for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
cnt=;
dfs(,); block=sqrt(cnt);
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
a[i].l=tl[x];
a[i].r=tr[x];
a[i].k=y;
a[i].id=i;
} sort(a+,a++m,cmp);
int L=,R=;
for(int i=;i<=m;i++){
while(L<a[i].l){
update(vis[num[L]],-);
vis[num[L]]--;
update(vis[num[L]],);
L++;
}
while(R<a[i].r){
R++;
update(vis[num[R]],-);
vis[num[R]]++;
update(vis[num[R]],);
}
while(L>a[i].l){
L--;
update(vis[num[L]],-);
vis[num[L]]++;
update(vis[num[L]],);
}
while(R>a[i].r){
update(vis[num[R]],-);
vis[num[R]]--;
update(vis[num[R]],);
R--;
}
ans[a[i].id]=query(maxn-)-query(a[i].k-);
}
for(int i=;i<=m;i++){
printf("%d\n",ans[i]);
}
return ;
}

CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)的更多相关文章

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

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

  2. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  3. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

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

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

  5. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  6. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  7. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  8. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  9. 模拟赛 T3 DFS序+树状数组+树链的并+点权/边权技巧

    题意:给定一颗树,有 $m$ 次操作. 操作 0 :向集合 $S$ 中加入一条路径 $(p,q)$,权值为 $v$ 操作 1 :给定一个点集 $T$,求 $T$ 的并集与 $S$ 中路径含交集的权和. ...

随机推荐

  1. 新一代视频AI服务 —— 阿里云智能视觉重磅发布

    3月27日下午,第51期阿里云产品发布会-智能视觉产品隆重发布,本次产品发布会首次面向全网用户深入的解读了智能视觉的前世今生. 行业背景 随着人工智能的技术不断成熟,AI逐渐在各行业内落地.在新零售领 ...

  2. hdu1536 sg打表

    标记数组用bool型防止超时.输入的f[ ]要排序. #include<stdio.h> #include<string.h> #include<algorithm> ...

  3. thinkphp5.0 路由规则配置

    开启路由‘url_route_on’ => true 首页路由'/' =>'home/index/index' 其它路由(1)'route' => 'home/index/route ...

  4. Java练习 SDUT-3349_答答租车系统(面向对象综合练习)

    答答租车系统(面向对象综合练习) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 各位面向对象的小伙伴们,在学习了面向对 ...

  5. Ubuntu修改root密码,ssh 允许root用户登录

    1,切换为root用户 2,passwd root(or others) 3,输两次密码 4,重启. ssh允许root用户登录: 1,vim /etc/ssh/sshd_config 2,修改Per ...

  6. spingboot项目在windows环境中运行时接收参数及日志中文乱码

    1.logback.xml配置 appender中添加 <param name="Encoding" value="UTF-8" /> <co ...

  7. POJ-2502_Subway

    Subway Time Limit: 1000MS Memory Limit: 65536K Description You have just moved from a quiet Waterloo ...

  8. Java练习 SDUT-1689_斐波那契?

    斐波那契? Time Limit: 1000 ms Memory Limit: 32768 KiB Problem Description 给出一个数列的递推公式,希望你能计算出该数列的第N个数.递推 ...

  9. Python基础:07迭代器

    迭代器是在版本 2.2 被加入Python 的,它为类序列对象提供了一个类序列的接口.Python 的迭代无缝地支持序列对象,而且它还允许迭代非序列类型,包括用户定义的对象.它的出现,对列表迭代.字典 ...

  10. Jmeter处理数据库

    安装环境: jmeter版本:3.1版本 java1.8版本 安装步骤: 1.下载连接mysql数据库jar包,地址:https://pan.baidu.com/s/10k6zD6CU4mo7xYJF ...