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 ...
随机推荐
- poj_3371
一道模拟题,写的有点麻烦 #include<iostream> #include<cstring> #include<cstdio> #include<alg ...
- PHP str_replace() 和str_ireplace()函数
PHP str_replace() 和str_ireplace()函数 实例 把字符串 "Hello world!" 中的字符 "world" 替换为 &quo ...
- Traversing a list
The most common way to traverse the elements of a list is with a for loop. The syntax is the same as ...
- if语句练习
输入年月日,首先判断该年是平年闰年并且计算该天是该年的第几天: 判断男女体重是否标准: 体重判断里边出现一个问题:如果性别输入的不是男也不是女,那么会执行输出“请输入正确的性别”:然后底下会继续输出“ ...
- Android RecyclerView和ScrollView嵌套使用
我们的recyclerView有多个layoutmanager,通过重写layoutmanager的方法就可以让recyclerView和ScrollView嵌套了.但是请注意,如果recyclerV ...
- PostgreSQL Replication之第九章 与pgpool一起工作(5)
9.5 检查复制 如果所有的节点都处于开机并运行的状态.我们就可以在集群上运行我们的第一个操作了.在我们的例子中,我们将简单地连接到pgpool并创建一个新的数据库.createdb 是一个命令行工具 ...
- sql中--行处理数据的两种方式
--创建待使用的表格CREATE TABLE Orders ( OrderID INT , CostValue DECIMAL(18, 2) );WITH cte_temp AS ( SELECT 1 ...
- ipad mini2 升级9.0.2后解锁白屏解决
解锁白屏是个什么现象?就是当你用手指滑动解锁后出现输入密码的界面后,1秒之内屏幕变白,中间一个黑色的苹果,几秒之后重新回到滑动解锁的界面.我出现这个现象不是因为升级了9.0.2,而是升级了9.0.2之 ...
- tomcat web容器工作原理
Tomcat的模块结构设计的相当好,而且其Web 容器的性能相当出色.JBoss直接就使用了Tomcat的web容器,WebLogic的早期版本也是使用了Tomcat的代码.Web容器的工作过程在下面 ...
- Threading and Tasks in Chrome
Threading and Tasks in Chrome Contents Overview Threads Tasks Prefer Sequences to Threads Posting a ...