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. C#语音特性 很实用

    1.隐式类型 Var 2.匿名类型 new{} 3.自动属性 [prop TAB]  public string Title { get; set; } 4.初始化器  var myObj1 = ne ...

  2. ubantu14.04安装storm伪分布式

    1.安装jdk 安装:sudo apt-get install openjdk-7-jdk 配置: 修改文件 sudo nano /etc/profile , 添加以下内容: 立即执行使之生效: 2. ...

  3. nowcoder 鹏

     鹏   时间限制:C/C++ 2秒,其他语言4秒空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 化而为鸟,其名为鹏.鹏之背,不知其 ...

  4. 领扣(LeetCode)用队列实现栈 个人题解

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  5. element 根据某多个属性合并列

    日常渲染 methods: { arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 没办法循环判断具体是那一列 所以就只好写了多个 ...

  6. Java虚拟机之栈

    一.程序计数器(寄存器):PCR 作用:记住下一条JVM指令的执行地址. 特点:①线程私有的 ②不会存在内存溢出 二.虚拟机栈 1.定义 虚拟机栈:线程运行所需要的内存空间. 栈帧:一个栈帧对应一个方 ...

  7. Glibc编译报错:*** These critical programs are missing or too old: as ld gcc

    Binutils版本升级 这里是binutils版本过低导致, 查看已部署版本 上传离线升级包 [root@sdw1 glibc]# tar -zxvf binutils-2.32.tar.gz [r ...

  8. 个人收藏-未整理--wince

    Wince 6.0 教程---第一课 环境搭建 分类: WINCE 2009-09-10 08:47 7622人阅读 评论(5) 收藏 举报 wincemicrosoftnetworkservicew ...

  9. 【Android - IPC】之Messenger简介

    参考资料: 1.<Android开发艺术探索>第二章2.4.3 2.[Messenger完全解析] 1.Messenger概述 Messenger,译为“信使”,是Android中一种基于 ...

  10. Android Binder机制介绍

    做过Android开发的同学可能有些体会,入门初期,工作内容主要是实现各式各样的UI界面,以及实现应用的业务逻辑.在这个阶段,我们会逐渐熟悉View系统,逐渐学会实现各种各样的界面以及动画效果.再往后 ...