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 ...
随机推荐
- 网页里如何使用js屏蔽鼠标右击事件
图片.png 在后台管理系统里面,遇到了这样的一个问题,右击ztree菜单,弹出修改界面,但是,现在确实这样的,右击默认弹出功能提示的框框,看上去似乎很影响自己想要的功能,只能禁用了,那么,网页里如何 ...
- Eclipse中项目进行发布到Tomcat中的位置
Eclips配置tomcat默认是发布到.metadate\plugins\目录下的,wtpwebapps.这样在实际的tomcat目录下,就找不到发布的项目.我们可以自己进行设置,在控制栏中找到se ...
- angularjs 事件向上向下传播
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- BZOJ4477: [Jsoi2015]字符串树
[传送门:BZOJ4477] 简要题意: 给出一棵n个点的树,树上的边都代表一个字符串,给出Q个询问,每个询问输入x,y和字符串s,求出x到y的路径上以s为前缀的字符串个数 题解: 自己yy了一波可持 ...
- bzoj1051: [HAOI2006]受欢迎的牛(强联通)
1051: [HAOI2006]受欢迎的牛 题目:传送门 题解: 今天又做一道水题... 强联通啊很明显 水个模板之后统计一下每个强联通分量中点的个数,再统计一下出度... 不难发现:缩点之后当且仅当 ...
- linux下修改完profile文件的环境变量后如何立即生效
方法1: 让/etc/profile文件修改后立即生效 ,可以使用如下命令: # . /etc/profile 注意: . 和 /etc/profile 有空格 方法2: 让/etc/profile文 ...
- NOIP2017 小凯的疑惑 解题报告(赛瓦维斯特定理)
题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小 凯想知道在无法准确支付的物品中,最贵的 ...
- Automation testing tool comparison - UFT & CodedUITest
Ease of Use - Recording and Playback Functionality UFT provides 4 models to record a new test. Norma ...
- Case study: word play
For the exercises in this chapter we need a list of English words. There are lots of word lists avai ...
- ListView 适配器实现getviewtypcount() 数组越界IndexOutOfBoundException
ListView中Item的多布局显示,需要用到了getViewTypecount和getItemViewType这两个重写方法,但是做完后出现了如下提示错误:java.lang.ArrayIndex ...