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 < ...
随机推荐
- Normalize.css与Reset CSS:定义浏览器统一的默认样式
今天在chrome上测试我的网页,发现一个<p>段落多出了一些margin,而我自己没有设定.打开f12调试,发现在一个“user agent style”栏下定义了这个margin,去g ...
- UNIX网络编程——socket的keep-alive(转)
第一部分 [需求] 不影响服务器处理的前提下,检测客户端程序是否被强制终了. [现状] 服务器端和客户端的Socket都设定了keepalive属性. 服务器端设定了探测次数等参数,客户端.服务器只是 ...
- C# event 事件学习
C# event 事件学习 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-26 章节: 简单事件编写 模拟 WPF 控件传递 ...
- 【转】内存耗用:VSS/RSS/PSS/USS
Terms VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存) PSS- Prop ...
- ASP .NET core 入门基础内容备份
model 里边设置主键 : [key]可以自定义主键 默认是名称为ID类型为int的字段 设置显示格式: [DisplayFormat(DataFormatString="{0:显示的格式 ...
- aop中通知详情
- flask 数据迁移
python flasky.py shell db.create_all() from app.models import User mhc = User("mhc") >& ...
- Oracle-11g 回缩表高水位
回缩表高水位的意义: 所有的 Oracle 段都有一个在段内容纳数据的上线,即高水位线(high water mark).HWM 是一个标记,很像水库的丽水最高水位,即使表内数据全部删除,HWM 也还 ...
- Sqlserver时间函数用法(二)
--1. 当前系统日期.时间 select getdate() --2015-01-06 09:27:27.277 --2.时间操作 dateadd 在向指定日期加上一段时间的基础上,返回新的 dat ...
- Ajax与Controller的参数交互
理论 jQuery.ajax( options )中重要参数设置 jQuery.ajax( options ) : 通过 HTTP 请求加载远程数据.通过jquery.ajax与SpringMVC的C ...