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 ...
随机推荐
- numpy基础篇-简单入门教程4
np.set_printoptions(precision=3),只显示小数点后三位 np.random.seed(100) rand_arr = np.random.random([2, 2]) n ...
- 现代C++
C++ 是世界上最常用的编程语言之一. 编写良好的 C++ 程序是快速.高效的. 该语言比其他语言更加灵活,因为你可以使用它来创建各种应用,包括有趣刺激的游戏.高性能科学软件.设备驱动程序.嵌入式程序 ...
- hadoop-12-安装ambari-agent
hadoop-12-安装ambari-agent 在所有的机器上面安装ambari-agent 1, cd /etc/yum.repos.d/vi 三个文件vi ambari.repo#VERSION ...
- 更新 hadoop eclipse 插件
卸载hadoop 1.1.2插件.并安装新版hadoop 2.2.0插件. 假设直接删除eclipse plugin文件夹下的hadoop 1.1.2插件,会导致hadoop 1.1.2插件残留在ec ...
- iOS开发之block解析
1. block的本质是一个Objective-C的对象,为什么这么说? 在Objective-C中,runtime会在执行时依据对象的isa指针的指向,来度额定这个对象的类型,也能够觉得一个对象,它 ...
- 如何解读「量子计算应对大数据挑战:中国科大首次实现量子机器学习算法」?——是KNN算法吗?
作者:知乎用户链接:https://www.zhihu.com/question/29187952/answer/48519630 我居然今天才看到这个问题,天……本专业,有幸听过他们这个实验的组会来 ...
- python中对单例模式的理解
class Foo(object): instance = None def __init__(self): pass def process(self): ' @classmethod #版本1单例 ...
- POJ 2110 二分+暴搜
题意: 给你一个矩阵 ,你能往各个方向走(不走出去就行),每次只能上下左右走一格,问路径上的点权最大值和最小值的差最小是多少. 思路: 首先 二分最后的答案, 暴力枚举当前的区间是啥. DFS 就OK ...
- Linux基础04
** Linux基本操作常用命令(四) ** Linux系统管理命令 1.top:查看系统资源,每隔三秒刷新一次,按q:退出浏览状态 2.free:查看内存信息,-m,以MB单位显示 3.netsta ...
- form表单提交的时候,传过去的值是键值对的形式
效果展示 第一种需求,点击input的时候,input的value发生改变 $('.group-wrapper input').click(function(){ $(this).val(0); // ...