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. 洛谷 P3951 小凯的疑惑 找规律

    目录 题面 题目链接 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例: 输出样例: 说明 思路 证明 AC代码 include<bits/stdc++.h> 题面 ...

  2. vue-cnodejs

    感谢那些无私开源的程序员,你们是最可爱的人儿~~~~ //根app app.js <template> <div id="app"> <v-heade ...

  3. (四)IO流之InputStream和OutputStream

    InputStream:定义了字节输入流的抽象类 OutputStream:定义了字节输出流的抽象类;该类所有方法返回void值 FileInputStream:继承InputStream FileO ...

  4. 轻松搞定word中让人抓狂的自动编号

    在word中使用自动编号时,如果一级编号是2,想让其后面的二级编号自动编号为2.1.2.2--,三级编号自动编号为2.1.1.2.1.2--:且在该一级编号调整为3时,后面的二级编号和三级编号的第一位 ...

  5. 云原生应用 Kubernetes 监控与弹性实践

    前言 云原生应用的设计理念已经被越来越多的开发者接受与认可,而Kubernetes做为云原生的标准接口实现,已经成为了整个stack的中心,云服务的能力可以通过Cloud Provider.CRD C ...

  6. php上传文件与图片到七牛的实例详解

    上传文件到七牛最简单的方式就是使用七牛官方最新的SDK 用composer安装PHP SDK composer require qiniu/php-sdk 上传文件到七牛 use Qiniu\Auth ...

  7. LeetCode73 Set Matrix Zeroes

    题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.(Me ...

  8. 2019-8-31-dotnet-通过-WMI-获取设备厂商

    title author date CreateTime categories dotnet 通过 WMI 获取设备厂商 lindexi 2019-08-31 16:55:59 +0800 2019- ...

  9. 关于react-router 路径改变页面没有刷新

    routert.js 中: <Router> <Switch> <Route exact path="/" component={Login}> ...

  10. vscode settings.json配置

    // 将设置放入此文件中以覆盖默认设置 { "editor.fontSize": 18, "editor.tabSize": 2, "editor.m ...