题目链接
现有一棵 n 个点的树,点的编号从 1 起,树以 1 为根,每个点 i 都一个颜色 ci,接下来有 m 个询问,每次询问以 vj 为根的子树中,求有多少种颜色,这些颜色在子树中出现的次数至少为 kj。
首先,想到是维护子树,我们就往Dsu上面靠,再者,又是要维护颜色个数大于等于K的,所以我们就要想办法把颜色维护,我的做法是,我们可以确定放进子树中的颜色的种类数,然后呢,我们现在要得到出现次数至少为k的,所以我们可以维护用总的颜色数减去出现次数小于等于K-1的颜色的种类数。这里就是通过树状数组维护了。
所以,总的复杂度就是。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, Q, head[maxN], cnt, col[maxN];
struct Eddge
{
int nex, to;
Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN << 1];
inline void addEddge(int u, int v)
{
edge[cnt] = Eddge(head[u], v);
head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
struct Question
{
int kth, ith;
Question(int a=0, int b=0):kth(a), ith(b) {}
friend bool operator < (Question e1, Question e2) { return e1.kth < e2.kth; }
};
vector<Question> vt[maxN];
int siz[maxN], dfn[maxN], tot, rid[maxN], Wson[maxN];
void pre_dfs(int u, int fa)
{
siz[u] = 1; dfn[u] = ++tot; rid[tot] = u;
int maxx = 0;
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(v == fa) continue;
pre_dfs(v, u);
siz[u] += siz[v];
if(siz[v] > maxx)
{
maxx = siz[v];
Wson[u] = v;
}
}
}
int ans[maxN] = {0}, col_siz[maxN] = {0}, num[maxN] = {0}, col_num, trie[maxN] = {0};
inline void update(int x, int val) { while(x <= 100000) { trie[x] += val; x += lowbit(x); } }
inline int query(int x) { int sum = 0; while(x) { sum += trie[x]; x -= lowbit(x); } return sum; }
void dfs(int u, int fa, bool keep)
{
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(v == fa || v == Wson[u]) continue;
dfs(v, u, false);
}
if(Wson[u])
{
dfs(Wson[u], u, true);
}
for(int i=head[u], v, id; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(v == fa || v == Wson[u]) continue;
for(int j=dfn[v]; j<dfn[v] + siz[v]; j++)
{
id = rid[j];
if(col_siz[col[id]]) update(col_siz[col[id]], -1);
else col_num++;
col_siz[col[id]]++;
update(col_siz[col[id]], 1);
}
}
if(col_siz[col[u]]) update(col_siz[col[u]], -1);
else col_num++;
col_siz[col[u]]++;
update(col_siz[col[u]], 1);
int len = (int)vt[u].size();
Question now;
for(int i=0; i<len; i++)
{
now = vt[u][i];
ans[now.ith] = col_num - query(now.kth - 1);
}
if(!keep)
{
for(int i=dfn[u], id; i<dfn[u] + siz[u]; i++)
{
id = rid[i];
update(col_siz[col[id]], -1);
col_siz[col[id]]--;
if(col_siz[col[id]]) update(col_siz[col[id]], 1);
else col_num--;
}
}
}
inline void init()
{
cnt = tot = col_num = 0;
for(int i=1; i<=N; i++) head[i] = -1;
}
int main()
{
scanf("%d%d", &N, &Q);
init();
for(int i=1; i<=N; i++) scanf("%d", &col[i]);
for(int i=1, u, v; i<N; i++)
{
scanf("%d%d", &u, &v);
_add(u, v);
}
pre_dfs(1, 0);
for(int i=1, vi, ki; i<=Q; i++)
{
scanf("%d%d", &vi, &ki);
vt[vi].push_back(Question(ki, i));
}
dfs(1, 0, false);
for(int i=1; i<=Q; i++) printf("%d\n", ans[i]);
return 0;
}