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 < ...
随机推荐
- histroy.back和histroy.go的区别
histroy.back(-1):直接返回当前页的上一页,数据全部消失,是个新的页面: histroy.go(-1):直接返回当前页的上一页,不过表单里的数据全部还在: histroy.back(0) ...
- 开启 3389 的 cmd 命令
方法一: 测试环境 Windows 2003 server 查看开启的端口 没有开启 3389 端口 执行语句 wmic RDTOGGLE WHERE ServerName='%COMPUTERNAM ...
- django之管理静态文件
管理静态文件 项目中的CSS.图片.js都是静态文件 配置静态文件 在settings 文件中定义静态内容 STATIC_URL = '/static/' STATICFILES_DIRS = [ o ...
- C# Data Parse
一.DateTime 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方法二:Convert.ToDateTime( ...
- delphi IOS 通知 TNotification
delphi IOS 通知 TNotification http://blogs.embarcadero.com/ao/2013/05/01/39450 TNotification http://d ...
- Sublime Text:初学者不知道的那些事
来源:Duchessjojo@译言 我是Sublime Text代码编辑器的忠实粉丝.我和诸多Mac程序员一样,最初使用的是Textmate代码编辑器.在Sublime Text 2发行后,我才开始转 ...
- 【303】C# 复制窗体 & 修改名称
参考:C#复制粘贴窗体 参考:VS修改项目解决方案名称 一.复制窗体 在“解决方案资源管理器”(以下简称:管理器)中选择要复制的窗体,比如要复制Form2,则在Form2.cs上右单击,选择复制. 在 ...
- MyBatis 与 Hibernate对比
- ie8、9 post 跨域
//显示浮层postAjax:function(url,param,callback){ var loadScore = layer_.load(1,{shade: [0.8,'#393D49']}) ...
- Log4J的配置文件详解
来自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记 ...