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 verticesx, 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.

Example

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

Note

A subtree of vertex v in a rooted tree with root r is a set of vertices{u : dist(r, v) + dist(v, u) = dist(r, u)}. Where dist(x, y) is the length (in edges) of the shortest path between vertices x and y.

题解:首先,通过dfs,可以把查询一棵树的子树转化为查询一段区间[l,r]。接下来,把整个区间分成n/sqrt(n)+1份,将查询按照l所在的区间排序,在同一区间内,将查询按r排序(由大到小)。对于查询,用一个数组表示某颜色数为x的颜色的个数,查询这个数组用分块法查询。

AC代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<vector>
using namespace std; typedef long long ll;
const int maxn = + ;
const int SIZE = ;
int bnt[maxn], block[maxn / SIZE + ], cnt[maxn];
int l[maxn], r[maxn], dfs_clock;
int c[maxn], cval[maxn], ans[maxn], n, m;
vector<int> g[maxn]; struct Query
{
int l, r, k, id;
Query() {}
Query(int lx, int rx, int kk, int i)
{
l = lx;
r = rx;
k = kk;
id = i;
}
bool operator <(const Query &q) const
{
if (l / SIZE != q.l / SIZE)
return l<q.l;
return r>q.r;
}
}querys[maxn]; void dfs(int u, int fa)
{
l[u] = ++dfs_clock;
cval[l[u]] = c[u];
int sz = g[u].size();
for (int i = ; i<sz; ++i)
{
int v = g[u][i];
if (v == fa)
continue;
dfs(v, u);
}
r[u] = dfs_clock;
} void Insert(int k, int x)
{
bnt[k] += x;
block[k / SIZE] += x;
}
void Add(int v)
{
int p = ++cnt[cval[v]];
Insert(p, );
Insert(p - , -);
} void Dec(int v)
{
int p = --cnt[cval[v]];
Insert(p, );
Insert(p + , -);
} int cal(int k)
{
int sum = , p = k / SIZE;
for (int i = k; i<(p + )*SIZE; ++i)
sum += bnt[i];
for (int i = p + ; i*SIZE<maxn; ++i)
sum += block[i];
return sum;
} void solve()
{
memset(cnt, , sizeof(cnt));
memset(bnt, , sizeof(bnt));
memset(block, , sizeof(block));
Insert(, n);
Add();
int l = , r = ;
for (int i = ; i<m; ++i)
{
while (querys[i].l<l) Add(--l);
while (querys[i].r>r) Add(++r);
while (querys[i].l>l) Dec(l++);
while (querys[i].r<r) Dec(r--);
ans[querys[i].id] = cal(querys[i].k);
}
} int main()
{
cin >> n >> m;
for (int i = ; i <= n; ++i)
g[i].clear();
dfs_clock = ;
for (int i = ; i <= n; ++i)
cin >> c[i];
int u, v, k;
for (int i = ; i<n - ; ++i)
{
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
} dfs(, -); for (int i = ; i<m; ++i)
{
cin >> v >> k;
querys[i] = Query(l[v], r[v], k, i);
}
sort(querys, querys + m);
solve();
for (int i = ; i < m; ++i)
cout << ans[i] << endl;
return ;
}

CoderForces-375D的更多相关文章

  1. coderforces #387 Servers(模拟)

    Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  2. coderforces #384 D Chloe and pleasant prizes(DP)

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. coderforces 731c

    题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...

  4. coderforces 721b

    题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. codeforces 375D:Tree and Queries

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

  6. CoderForces 280B(记忆化搜索)

    题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...

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

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

  8. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  9. CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】

    传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...

  10. CoderForces 689A Mike and Cellphone (水题)

    题意:给定一个手机键盘数字九宫格,然后让你判断某种操作是不是唯一的,也就是说是不是可以通过平移也能实现. 析:我的想法是那就平移一下,看看能实现,就四种平移,上,下,左,右,上是-3,要注意0变成8, ...

随机推荐

  1. Jenkins+pipeline+参数构建+人工干预确定

    Jenkins+pipeline+参数构建+人工干预 实现了以下功能 1. 可以选择环境,单选:可以选择需要发布的项目,多选 2.发布过程可视化 3. 可以人工干预是否继续发布. 初始化配置需要很久, ...

  2. 理解Spark运行模式(三)(STANDALONE和Local)

    前两篇介绍了Spark的yarn client和yarn cluster模式,本篇继续介绍Spark的STANDALONE模式和Local模式. 下面具体还是用计算PI的程序来说明,examples中 ...

  3. 让块元素在div中水平居中,并且垂直居中的五种方法

    在写代码前,先做下准备工作,写两个div,设置下div的大小,把小的div放在大的div里面.可以给小的div设置下颜色,方便观看. 方法一:写一个伪元素,将它设置为行内块元素,高度与父元素相同,写一 ...

  4. fiddler工具使用大全

    Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888,我们也可以通过设置进行修改. 代理就是在 ...

  5. DNS name

    DNS name 指的反向解析的域名.

  6. myBaits持久性框架

    动态 SQL 博客交流群:1018996617   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL ...

  7. VueRouter爬坑第二篇-动态路由

    VueRouter系列的文章示例编写时,项目是使用vue-cli脚手架搭建. 项目搭建的步骤和项目目录专门写了一篇文章:点击这里进行传送 后续VueRouter系列的文章的示例编写均基于该项目环境. ...

  8. SSH 免密登录服务器

    本文详解如何以多种方法实现ssh免密码登陆远程服务器 阅读须知: 1.以下方法操作时请不要随意切换目录. 2.xxx为私钥,xxx.pub是公钥(默认一般文件名为id_rsa和id_rsa.pub,可 ...

  9. asp.net core 从 3.0 到 3.1

    asp.net core 从 3.0 到 3.1 Intro 今天 .net core 3.1 正式发布了,.net core 3.1 正式版已发布,3.1 主要是对 3.0 的 bug 修复,以及一 ...

  10. P2669 金币

    题目描述 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收到三枚金币:之后四天(第七.八.九.十天),每 ...