CoderForces-375D
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 ≤ 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).
Output
Print m integers — the answers to the queries in the order the queries appear in the input.
Example
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
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的更多相关文章
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- 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 ...
- coderforces 731c
题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...
- coderforces 721b
题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 375D:Tree and Queries
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- CoderForces 280B(记忆化搜索)
题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...
- codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】
传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...
- CoderForces 689A Mike and Cellphone (水题)
题意:给定一个手机键盘数字九宫格,然后让你判断某种操作是不是唯一的,也就是说是不是可以通过平移也能实现. 析:我的想法是那就平移一下,看看能实现,就四种平移,上,下,左,右,上是-3,要注意0变成8, ...
随机推荐
- Linux下编写-makefile-详细教程(跟我一起写-Makefile-Markdown整理版)
目录 概述 关于程序的编译和链接 Makefile 介绍 Makefile的规则 一个演示例子 make是怎样工作的 makefile中使用变量 让make自己主动推导 另类风格的makefile 清 ...
- pat 1027 Colors in Mars(20 分)
1027 Colors in Mars(20 分) People in Mars represent the colors in their computers in a similar way as ...
- 记录工作遇到的死锁问题(Lock wait timeout exceeded; try restarting transaction)
1.问题背景 刚来新公司不久,对业务还不太熟悉,所以领导先安排我维护原有系统.大概介绍下项目背景,项目分为核心业务部分在项目A中,与第三方交互的业务在项目B中,前端发起请求调用A项目接口,并在A项目中 ...
- 概率的基本概念&离散型随机变量
使用excel可以直接计算二项分布和超几何分布:
- 彻底搞懂CSS伪类选择器:is、not
本文介绍一下Css伪类:is和:not,并解释一下is.not.matches.any之前的关系 :not The :not() CSS pseudo-class represents element ...
- 检测当前IE浏览器的版本
检测当前IE浏览器的版本(注意:在非IE浏览器中是看不到效果的) 使用示例如下:低于IE8弹窗提示 <!--[if lte IE 8]><script>alert('您当前浏览 ...
- ubuntu 16.04上源码编译libjpeg-turbo和使用教程 | compile and use libjpeg-turbo on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/9f626e7a/,欢迎阅读! compile and use libjpeg-turbo on ubuntu 16.04 Seri ...
- 达梦"记录超长"警告
出现"记录超长"背景介绍: 导入数据库时,出现数据库记录超长警告,导致数据无法正常导入! 1.重新建库,把页大小改大 这种方式是在建立数据库实例的时候进行的 修改[页大小] 2.把 ...
- k8s 随记
1.kubelet参数解析:https://blog.csdn.net/qq_34857250/article/details/84995381 2.如何在github中查找k8s代码关键字? 现在我 ...
- PostGIS 用Navicat工具添加自增
1.新建查询,新增序列(从66开始,每次自增1): CREATE SEQUENCE seq_area INCREMENT 1 MINVALUE 66 MAXVALUE 999999999 START ...