CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)
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 - 1lines 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.
Examples
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 题意:
给定一颗树,树上的每个节点都有一个颜色,以1为根节点。
每次询问,问以节点v为根节点的子树里面,有多少种颜色出现次数大于k。
思路:
想到莫队之后,dfs序和树状数组很好想了。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int col[maxn];
int num[maxn];
int vis[maxn];
int cnt,Head[maxn];
int tl[maxn],tr[maxn];
struct node{
int v,nxt;
}e[*maxn];
struct query{
int l,r,id,k;
}a[maxn]; void add_edge(int u,int v){
e[cnt].v=v;
e[cnt].nxt = Head[u];
Head[u]=cnt++;
} void dfs(int u,int fa){
tl[u]=++cnt;
num[cnt]=col[u];
for(int k=Head[u];~k;k=e[k].nxt){
if(e[k].v!=fa)dfs(e[k].v,u);
}
tr[u]=cnt;
}
int block;
bool cmp(query a,query b){
if(a.l/block!=b.l/block){return a.l<b.l;}
return a.r<b.r;
}
int lowbit(int x){
return x&-x;
} int bit[maxn]; int query(int pos){
int ans=;
while(pos){
ans+=bit[pos];
pos-=lowbit(pos);
}
return ans;
} void update(int pos,int val){
if(pos<=){return;}
while(pos<maxn){
bit[pos]+=val;
pos+=lowbit(pos);
}
} int ans[maxn]; int main()
{
memset(Head,-,sizeof(Head));
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&col[i]);
} for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
cnt=;
dfs(,); block=sqrt(cnt);
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
a[i].l=tl[x];
a[i].r=tr[x];
a[i].k=y;
a[i].id=i;
} sort(a+,a++m,cmp);
int L=,R=;
for(int i=;i<=m;i++){
while(L<a[i].l){
update(vis[num[L]],-);
vis[num[L]]--;
update(vis[num[L]],);
L++;
}
while(R<a[i].r){
R++;
update(vis[num[R]],-);
vis[num[R]]++;
update(vis[num[R]],);
}
while(L>a[i].l){
L--;
update(vis[num[L]],-);
vis[num[L]]++;
update(vis[num[L]],);
}
while(R>a[i].r){
update(vis[num[R]],-);
vis[num[R]]--;
update(vis[num[R]],);
R--;
}
ans[a[i].id]=query(maxn-)-query(a[i].k-);
}
for(int i=;i<=m;i++){
printf("%d\n",ans[i]);
}
return ;
}
CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)的更多相关文章
- CodeForces 375D Tree and Queries 莫队||DFS序
Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- 模拟赛 T3 DFS序+树状数组+树链的并+点权/边权技巧
题意:给定一颗树,有 $m$ 次操作. 操作 0 :向集合 $S$ 中加入一条路径 $(p,q)$,权值为 $v$ 操作 1 :给定一个点集 $T$,求 $T$ 的并集与 $S$ 中路径含交集的权和. ...
随机推荐
- (五)IO流之ByteArrayInput/OutputStream
ByteArrayInputStream:是把字节数组当成源的输入流 String string="hello shanghai"; ByteArrayInputStream bi ...
- Android开发-API指南-<activity-alias>[原创译文]
http://blog.sina.com.cn/s/blog_48d491300100zmg5.html
- Python学习之路7☞装饰器
一:命名空间与作用域 1.1命名空间 局部命名空间: def foo(): x=1 def func(): pass 全局命名空间: import time class ClassName:pass ...
- phpexcel使用说明3
下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...
- 【NS2】ubuntu安装和同时使用不同版本的ns2(转载)
有时候我们可能会遇到要同时安装两个ns版本的问题,比如我研究wimax/802.16,因为协议太复杂,用的是长庚大学和nist的wimax补丁.长庚大学的wimax补丁是在ns2.29下开发的,nis ...
- 使用 EnumWindows 找到满足你要求的窗口 - walterlv
原文:使用 EnumWindows 找到满足你要求的窗口 - walterlv 使用 EnumWindows 找到满足你要求的窗口 2019-04-30 13:11 在 Windows 应用开发中,如 ...
- @loj - 2250@ 「ZJOI2017」仙人掌
目录 @题目描述@ @solution@ @accepted code@ @details@ @题目描述@ 如果一个无自环无重边无向连通图的任意一条边最多属于一个简单环,我们就称之为仙人掌.所谓简单环 ...
- python-----堡垒机前戏paramiko模块及进阶
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...
- 多版本python共存,安装三方库到指定python版本
多版本python安装过程略过不提提供完美解决python启动和各版本pip问题: python3下pip安装命令如下: py -3 -m pip install xxxxxx python2下pip ...
- iptables 详细使用
检查状态 先检查是否安装了iptables $ service iptables status 安装iptables $ yum install iptables 升级iptables $ yum u ...