HDU 4358 Boring counting dfs序+莫队算法
题意:N个节点的有根树,每个节点有一个weight。有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种。
这是第一次写莫队算法,之前也只是偶有耳闻。
看了别人的代码打的,还是贴上来吧。
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define MAXN 100005
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
int n, k, Q;
vector<int> p;
vector<int> G[MAXN];
int a[MAXN], sum[MAXN];
int L[MAXN], R[MAXN], res[MAXN];
int t, ans;
struct Node
{
int left, right, id, pos;
Node(int left = , int right = , int id = , int pos = ):left(left), right(right), id(id), pos(pos){};
}m[MAXN];
bool compare(Node a, Node b)
{
return (a.pos < b.pos ||(a.pos == b.pos && a.right < b.right));
}
void dfs(int x, int father)
{
t++;
L[x] = t;
for(int i = ; i < G[x].size(); i++){
if(G[x][i] == father) continue;
dfs(G[x][i], x);
}
R[x] = t;
}
void work(int x, int v)
{
if(sum[x] == k){
ans--;
}
sum[x] += v;
if(sum[x] == k){
ans++;
}
} int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
for(int cas = ; cas <= T; cas++){
scanf("%d%d", &n, &k);
p.clear();
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
p.push_back(a[i]);
}
sort(p.begin(), p.end());
for(int i = ; i <= n; i++){
a[i] = lower_bound(p.begin(), p.end(), a[i]) - p.begin();
}
int x, y;
for(int i = ; i <= n; i++){
G[i].clear();
}
for(int i = ; i < n; i++){
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
t = ;
dfs(, -);
scanf("%d", &Q);
int u = sqrt(n);
for(int i = ; i <= Q; i++){
scanf("%d", &x);
m[i] = Node(L[x], R[x], i, L[x] / u);
}
sort(m + , m + Q + , compare);
memset(sum, , sizeof(sum));
int left = , right = ;
ans = ;
work(a[right], );
for(int i = ; i <= Q; i++){
while(right < m[i].right){
right++;
work(a[right], );
}
while(right > m[i].right){
work(a[right--], -);
}
while(left < m[i].left){
work(a[left++], -);
}
while(left > m[i].left){
left--;
work(a[left], );
}
res[m[i].id] = ans;
}
printf("Case #%d:\n", cas);
for(int i = ; i <= Q; i++){
printf("%d\n", res[i]);
}
if(cas != T){
printf("\n");
}
}
}
HDU 4358 Boring counting dfs序+莫队算法的更多相关文章
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- hdu 4358 Boring counting 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- HDU 5145 NPY and girls(莫队算法+乘法逆元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...
- HDU - 4358 Boring counting (dsu on tree)
Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- CF600E Lomsat gelral (dfs序+莫队)
题面 题解 看到网上写了很多DSU和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...
- HDU - 4358 Boring counting (树上启发式合并/线段树合并)
题目链接 题意:统计树上每个结点中恰好出现了k次的颜色数. dsu on tree/线段树合并裸题. 启发式合并1:(748ms) #include<bits/stdc++.h> usin ...
随机推荐
- react-native 运行提示红屏 error: bundling failed: ambiguous resolution: module `/User/xxx/Project/ico/index.js` tries to require `react-native`, but there are several files providing this module. You can de
运行 react-native start 报错 执行这2个进行清除缓存问题 yarn start -- --reset-cache npm start -- --reset-cache
- vue父子组件通信传值
父组件 -> 子组件 通过props来进行通信 父组件代码: <Children :dataName = "dataContent" /> //dataName: ...
- 关于 Error: No PostCSS Config found in 的错误
问题描述: 项目在本地运行不报错,上传到 GitHub 之后,再 clone 到本地, npm install 安装完成之后再执行 npm run dev 这时报错 Error: No PostCSS ...
- 紫书 例题8-9 UVa 1451 (数形结合)
这道题用了数形结合, 真的牛逼, 完全想到不到还可以这么做 因为题目求的是平均值, 是总数除以个数, 这个时候就可以联系 到斜率, 也就是说转化为给你一堆点, 让你求两点之间的最大斜率 要做两个处理 ...
- MySql5.7免安装版配置过程(ubuntu16.04)
MySql5.7免安装版配置过程(ubuntu16.04) 原创 2017年02月07日 16:58:24 标签: 1001 编辑 删除 一.安装环境: 操作系统:ubuntu16.04 数据库:my ...
- inux 虚拟机桥接模式 静态ip设置,桥接才是王道
修改/etc/sysconfig/network-scripts 目录下的 ifcfg-eth0 [root@yangcb network-scripts]# cat ifcfg-eth0 DEVI ...
- 程序员之---C语言细节12(指针和数组细节,"//"的可移植性说明)
主要内容:指针和数组细节,"//"的可移植性说明 #include <stdio.h> int main(int argc, char **argv) { int a[ ...
- android获取自己定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度
android获取自己定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度 1.获取自己定义控件height 在本Activity中获取当前Activity中控件的height: Button button ...
- MongoDB查询、索引和聚合
初始化mongodb数据库 > use deng switched to db deng > db.createCollection("jingdong") #无參数 ...
- windows下python3 使用cx_Oracle,xlrd插件进行excel数据清洗录入
我们在做数据分析,清洗的过程中,很多时候会面对各种各样的数据源,要针对不同的数据源进行清洗,入库的工作.当然python这个语言,我比较喜欢,开发效率高,基本上怎么写都能运行,而且安装配置简单,基本上 ...