CodeForces 376F Tree and Queries(假·树上莫队)
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
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.
题意:给你一颗根为一有n个点的树,每个点有一个颜色值,给出m个询问,询问x子树中出现颜色数大于等于k的颜色数
题解:dfs序一遍,子树查询就变成了区间查询,然后就可以考虑莫队了,这个莫队也非常高妙,因为每次每个点只会加一或者减一,然后就可以动态维护后缀和了,这个听起来很高级,仔细看看代码还是很简单的。
代码如下:
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int l,r,k,id;
}q[]; int sz,n,m,a[],ans[],dfsn[],size[],w[],c[],sum[],cnt[],tot,tot1;
vector<int> g[]; int block(int x)
{
return (x+)/sz;
} int cmp(node a,node b)
{
if(block(a.l)==block(b.l))
{
return a.r<b.r;
}
return a.l<b.l;
} void add(int x)
{
cnt[c[x]]++;
sum[cnt[c[x]]]++;
} void del(int x)
{
sum[cnt[c[x]]]--;
cnt[c[x]]--;
} void dfs(int now,int f)
{
dfsn[now]=++tot;
c[tot]=w[now];
size[now]=;
for(int i=;i<g[now].size();i++)
{
if(g[now][i]==f)
{
continue;
}
dfs(g[now][i],now);
size[now]+=size[g[now][i]];
}
} int main()
{
scanf("%d%d",&n,&m);
sz=sqrt(n);
for(int i=;i<=n;i++)
{
scanf("%d",&w[i]);
}
for(int i=;i<n;i++)
{
int from,to;
scanf("%d%d",&from,&to);
g[from].push_back(to);
g[to].push_back(from);
}
dfs(,);
for(int i=;i<=m;i++)
{
int x,k;
scanf("%d%d",&x,&k);
q[++tot1].k=k;
q[tot1].l=dfsn[x];
q[tot1].r=dfsn[x]+size[x]-;
q[tot1].id=i;
}
sort(q+,q+tot1+,cmp);
int nowl=,nowr=;
for(int i=;i<=m;i++)
{
while(nowl<q[i].l) del(nowl++);
while(nowl>q[i].l) add(--nowl);
while(nowr<q[i].r) add(++nowr);
while(nowr>q[i].r) del(nowr--);
ans[q[i].id]=sum[q[i].k];
}
for(int i=;i<=m;i++)
{
printf("%d\n",ans[i]);
}
}
CodeForces 376F Tree and Queries(假·树上莫队)的更多相关文章
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- Tree and Queries CodeForces - 375D 树上莫队
http://codeforces.com/problemset/problem/375/D 树莫队就是把树用dfs序变成线性的数组. (原数组要根据dfs的顺序来变化) 然后和莫队一样的区间询问. ...
- spoj COT2 - Count on a tree II 树上莫队
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的, 受益匪浅.. #include <iostream> #include < ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)
题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...
- SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from ...
- COT2 - Count on a tree II(树上莫队)
COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N ...
- Codeforces 852I Dating 树上莫队
Dating 随便树上莫队搞一搞就好啦. #include<bits/stdc++.h> #define LL long long #define LD long double #defi ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
随机推荐
- Java Ant Build详解
转载地址:http://www.cnblogs.com/wufengxyz/archive/2011/11/24/2261797.html 1,什么是antant是构建工具2,什么是构建概念到处可查到 ...
- php header 302重定向失效问题
E:\html\pim\php_weili_activities\application\controllers\user.php public function login() { if ($thi ...
- 一、linux搭建jenkins+github详细步骤
事情缘由: 现在在做的主要工作是通过jenkins+postman实现api的自动化测试,想要达到的效果是,api自动化测试定时跑脚本的同时,github有新的代码提交,jenkins会自动检测部署新 ...
- jQuery之事件和动画
1.加载DOM $(document).ready(function(){ }) 简写形式: $(function(){ }) 事件绑定: 合成事件 事件冒泡 移除事件 JQuery中的动画 show ...
- **python中的类和他的成员
面向对象是一种编程方式,此编程方式的实现基于对 类 和 对象 的使用. Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 在这里,为文章中使用 ...
- 颜色模式中8位,16位,24位,32位色彩是什么意思?会有什么区别?计算机颜色格式( 8位 16位 24位 32位色)<转>
颜色模式中8位,16位,24位,32位色彩是什么意思?会有什么区别简单地说这里说的位数和windows系统显示器设置中的颜色位数是一样的.表示的是能够显示出来的颜色的多少. 8位的意思是说,能够显示出 ...
- 【315】Windows 之间代码自动传文件
对于 Windows 内部自动复制/移动文件可以通过 批处理 来完成,对于不同的电脑之间的实现也是相同的方法,但是需要将一台电脑的对应文件夹设置成 共享,只要在另一台电脑能够直接访问共享的文件夹,就可 ...
- 开发团队(Team)的主要职责和特征
角色介绍 开发团队是Scrum团队的三个角色之一. 开发团队包括架构师.开发工程师.测试人员.数据库管理员和UI设计师等,这几类人的跨职能组合.具备的技能足以实现产品开发. Team的主要职责 1.S ...
- 02.全文检索和数据库like的区别
全文检索主要应用领域:搜索引擎(百度,搜狗).站内搜索(微博搜索).电商网站(京东,淘宝) 现在不缺乏做java的人,但是缺乏有互联网背景的做Java的人.具有互联网技术的Java人才.比如说大数据, ...
- Opencv 图片直方图
#include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...