题意: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序+莫队算法的更多相关文章

  1. hdu 4358 Boring counting dfs序+莫队+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  2. hdu 4358 Boring counting 离散化+dfs序+莫队算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. HDU 5145 NPY and girls(莫队算法+乘法逆元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...

  5. HDU - 4358 Boring counting (dsu on tree)

    Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...

  6. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  7. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  8. CF600E Lomsat gelral (dfs序+莫队)

    题面 题解 看到网上写了很多DSU和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...

  9. HDU - 4358 Boring counting (树上启发式合并/线段树合并)

    题目链接 题意:统计树上每个结点中恰好出现了k次的颜色数. dsu on tree/线段树合并裸题. 启发式合并1:(748ms) #include<bits/stdc++.h> usin ...

随机推荐

  1. Linux CentOs6.5误卸载自带python和yum后的解决办法

    事故背景:前几天因项目需要,在服务器上搭建python-mysql模块,结果没安装好,于是乎想卸载重装,遂在网上查询卸载python的方法,结果一不小心直接把系统的python删了个干净....... ...

  2. java后台生成图片二维码

    controller: /** * 获取登录的验证码 * @param request * @param response */ public void getLoginCode(HttpSessio ...

  3. Python3+Gurobi使用教程(一)

    Gurobi使用教程 1.Gurobi使用的一般框架 from gurobipy import * try: m=Model('modelname') except GurobiError: prin ...

  4. OpenJDK源码研究笔记(三)-RandomAccess等标记接口的作用

    标识接口是没有任何方法和属性的接口. 它仅仅表明它的类属于一个特定的类型,供其他代码来测试允许做一些事情. 下面来看一个标记接口RandomAccess. public interface Rando ...

  5. 洛谷 P2758 编辑距离

    P2758 编辑距离 题目描述 设A和B是两个字符串.我们要用最少的字符操作次数,将字符串A转换为字符串B.这里所说的字符操作共有三种: 1.删除一个字符: 2.插入一个字符: 3.将一个字符改为另一 ...

  6. hdu 1166 敌兵布阵 (线段树单点更新)

    敌兵布阵                                                         Time Limit: 2000/1000 MS (Java/Others)  ...

  7. Swift中NSDictionaryOfVariableBindings的替代方案

    有日子没写东西了,抽点时间练练笔头子,业精于勤荒于嬉~ 近期从OC转到了Swift2,因为Swift一直没有正经学正经用,所以对这门语言的理解基本算是个球...不得不感慨苹果的动作之快.Swift还没 ...

  8. OC第二课

    主要内容:实例变量可见度.方法 一.实例变量可见度 public(共同拥有的):实例变量能够在类的内部和外部使用 protected(受保护的.默认的):实例变量仅仅能在该类及其子类中使用 priva ...

  9. 基于SIP和RTP协议的开源VOIP之QuteCom简单介绍

    **************************************************************************************************** ...

  10. BZOJ5042: LWD的分科岛

    [传送门:BZOJ5042] 简要题意: 给出n个数,q个询问,每个询问输入opt,l,r,如果opt=1,则输出l到r中的最小值,否则输出最大值 题解: 直接上ST表,自信一波,结果 MLE??好吧 ...