codeforces 375D. Tree and Queries (莫队+分块)

本文介绍了一种使用莫队算法结合分块技术解决特定树形结构查询问题的方法。通过实例详细展示了如何针对一系列查询,高效计算子树中颜色相同的顶点数至少达到指定阈值的颜色种类数量。
摘要由CSDN通过智能技术生成

D. Tree and Queries
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

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



题解:莫队+分块

对于出现次数也分块维护,每次O(sqrt(n))查询

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define N 500003
using namespace std;
int n,m,c[N],a[N],belong[N],l[N],r[N],ans[N];
int tot,point[N],v[N],nxt[N],sz,cnt[N],num[N],block;
struct data{
	int l,r,k,id;
}q[N];
int cmp(data a,data b)
{
	return belong[a.l]<belong[b.l]||belong[a.l]==belong[b.l]&&a.r<b.r;
}
void add(int x,int y)
{
	tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; 
	tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void dfs(int x,int fa)
{
	a[++sz]=c[x]; l[x]=sz;
	for (int i=point[x];i;i=nxt[i]){
		if (v[i]==fa) continue;
		dfs(v[i],x);
	}
	r[x]=sz;
}
void addx(int x,int val)
{
	num[belong[x]]-=v[x];
	v[x]+=val;
	num[belong[x]]+=v[x];
}
void change(int pos,int val)
{
	int t=a[pos];
	if (cnt[t]) addx(cnt[t],-1);
	cnt[t]+=val;
	if (cnt[t]) addx(cnt[t],1);
}
int query(int l,int r)
{
	int t=0;
	if (belong[l]==belong[r]){
		for (int i=l;i<=r;i++)
		 if (v[i]) t+=v[i];
		return t; 
	}
	for (int i=l;i<=belong[l]*block;i++) 
	 if (v[i]) t+=v[i];
	for (int i=(belong[r]-1)*block+1;i<=r;i++) 
	 if (v[i]) t+=v[i];
	for (int i=belong[l]+1;i<belong[r];i++)
	 t+=num[i];
	return t;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&c[i]);
	for (int i=1;i<n;i++) {
		int x,y; scanf("%d%d",&x,&y);
		add(x,y);
	}
	dfs(1,0); block=ceil(sqrt(n));
	for (int i=1;i<=n;i++) belong[i]=(i-1)/block+1;
	int mx=0;
	for (int i=1;i<=m;i++) {
	 scanf("%d%d",&q[i].l,&q[i].k),q[i].id=i;
	 q[i].r=r[q[i].l];  q[i].l=l[q[i].l];
	 mx=max(mx,q[i].k);
    }
    mx=max(mx,n);
    memset(v,0,sizeof(v));
	sort(q+1,q+m+1,cmp);
	block=ceil(sqrt(mx));
	for (int i=1;i<=mx;i++) belong[i]=(i-1)/block+1;
	int l=1,r=1; change(1,1);
	for (int i=1;i<=m;i++) {
		while (q[i].l<l) change(--l,1);
		while (q[i].l>l) change(l++,-1);
		while (q[i].r>r) change(++r,1);
	    while (q[i].r<r) change(r--,-1);
	    ans[q[i].id]=query(q[i].k,mx);
	}
	for (int i=1;i<=m;i++) printf("%d\n",ans[i]);
}



### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值