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 < ...
随机推荐
- 一些常用的centos命令,记忆下,属于常用的
一些常用的centos命令,记忆下,属于常用的 查询内网IP hostname -I 查询外网IP curl ifconfig.me 查看硬盘使用情况 df -h 查看系统资源使用率 top 查看系统 ...
- TortoiseSVN/Git覆盖图标失效的解决方案
之前在电脑上安装了TortoiseGit和TortoiseSVN这两种版本控制,使用一段时间之后发现,这两种版本控制的覆盖图标都无法显示,起初以为是git和svn使用的图标的不一样,有冲突,导致这两种 ...
- C#中StreamReader读取中文出现乱码
转自yhrun原文C#中StreamReader读取中文出现乱码 原因是自Windows 2000之后的操作系统在文件处理时默认编码采用Unicode 所以.NET文件的默认编码也是Unicode.除 ...
- C#获取外网IP、本机MAC地址及Ping的实现
原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...
- [转] C# 获取程序运行目录
来自 莫等闲也,原文 // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnosti ...
- 【Consul】Consul实践指导-配置文件
Agent有各种各样的配置选项,这些配置选项可以通过命令行参数的方式设定,也可用通过配置文件的方式设定--所有的配置选项都是可选的,当然也是有默认值的. 当加载配置选项时,consul是按照词典顺序从 ...
- springboot整合最新版dubbo以及dubbo-admin的安装
一.安装前准备 由于dubbo被阿里捐献给了apache,这次安装admin时,参考网上的资料,地址还是停留在之前的链接,踩了不少坑,这里记录下. dubbo-admin下载地址: 地址一:https ...
- poi操作word 2007 常用方法总结
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- UNITY 带spriterender的对象导出为prefab时主贴图丢失的BUG
从场景导出带有sprite的对象为prefab时贴图丢失的BUG.解决方案:对场景中每个sprite重新赋一下贴图,然后导出就好了,原因不明. 补充:这个有时候是因为贴图类型不是 2D AND UI ...
- 利用fetch进行POST传参
fetch(config.host+"url",{ method:"POST", mode: 'cors',跨域请求 headers: { ...