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).
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 ≤ n; ai ≠ 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).
Print m integers — the answers to the queries in the order the queries appear in the 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
2
2
1
0
1
4 1
1 2 3 4
1 2
2 3
3 4
1 1
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 ;
}