HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化
http://acm.hdu.edu.cn/listproblem.php?vol=49
给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k。则贡献加1
思路:主要的麻烦就是动态修改前缀和了。因为对于每个数字val。则找它祖先的话, <= k / val的数字,都是合法的。所以问题转化成求你现在dfs中保存的数字,有多少个是 <= k / val的,树状数组即可。
问题就是,数字太大了,这不适合树状数组,那么我们把每个数字离散成他们的下标即可,但其实这部的操作是不需要的,就是不需要用map来离散的,因为一个数组a[],排序后的数组b[],b[i]就是a[]中某一个数字,那么i就是其对应离散后的下标,怎么找这个下标呢?lower_bound即可。但是,有相同的数字的话,lower_bound总是会找第一的,就是2 2 2 2,你找2,返回的是1的。但是没事,这不影响我们的结果。
所以,lower_bound即可,然后找到有多少个数是 <= k / val的,首先找到k / val离散后的下标,那么问题来了,没有k / val这个数值怎么办?那么就找到第一个 <= k / val的就好了。int pos = upper_bound - 1; 。这个不能用lower_bound来找下标了,为什么呢,因为它可能不存在,所以找到一个比它大的了,如果存在,那刚好,不存在又蛋疼,所以用upper_bound找,-1即可。
这题比赛的时候想到怎么做,但是写不出,然后赛后也写了很久,主要是dfs那么什么时候更新,什么时候取消更新,有点乱,还是太渣了啊。。。努力吧
我dfs的思路是这样的:首先没到一个节点cur。算贡献,然后这个节点入树状数组,然后当这个节点的所有边遍历完后,把这个数删除,注意一点是数字可能有0,不能除。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
LL a[maxn];
LL b[maxn];
int n;
LL k;
struct edge {
int u, v;
int tonext;
} e[maxn];
int num, first[maxn];
bool in[maxn];
void addedge(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
LL ans;
int c[maxn];//
int lowbit (int x) { //
return x&(-x);
}
void add (int pos,int val) { //
if (pos <= ) return;
while (pos<=n) { //
c[pos] += val;
pos += lowbit(pos);
}
return ;
}
int get_sum (int pos) { //???1--pos???
if (pos <= ) return ;
int ans = ;
while (pos) {
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
void calc(int cur) {
if (a[cur] == ) {
ans += get_sum(n);
} else {
LL tofind = k / a[cur];
int pos = upper_bound(b + , b + + n, tofind) - b;
ans += get_sum(pos - );
//cout << tofind << " *****" << pos - 1 << endl;
}
// cout << cur << " " << ans << endl;
}
void dfs(int cur) {
calc(cur);
int pos = lower_bound(b + , b + + n, a[cur]) - b;
add(pos, );
for (int i = first[cur]; i; i = e[i].tonext) {
dfs(e[i].v);
}
add(pos, -);
}
void work() {
scanf("%d%I64d", &n, &k);
for (int i = ; i <= n; ++i) {
scanf("%I64d", &a[i]);
b[i] = a[i];
}
// for (int i = 1; i <= n; ++i) {
// cout << a[i] << " ";
// }
// cout << endl;
sort(b + , b + + n);
// for (int i = 1; i <= n; ++i) {
// cout << b[i] << " ";
// }
// cout << endl;
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
in[v] = ;
}
int root = -inf;
for (int i = ; i <= n; ++i) {
if (in[i] == ) {
root = i;
break;
}
}
ans = ;
dfs(root);
// cout << ans << endl;
printf("%I64d\n", ans);
return ;
}
int main () {
#ifdef LOCAL
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) {
work();
num = ;
memset(in, , sizeof in);
memset(c, , sizeof c);
memset(first, , sizeof first);
}
return ;
}
HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化的更多相关文章
- HDU - 5877 Weak Pair (dfs+树状数组)
题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...
- HDU 5877 Weak Pair(树状数组+dfs+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...
- HDU 5877 Weak Pair(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- hdu 5877 Weak Pair dfs序+树状数组+离散化
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Prob ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- Weak Pair (dfs+树状数组)
Weak Pair (dfs+树状数组) 题意 这个题目是要求:一颗树上,有n个节点,给出每个节点的权值.另外给出一个值k,问有多少对节点满足: \(power[u]*power[v]<=k\) ...
- HDU 5877 Weak Pair(弱点对)
HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Jav ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
随机推荐
- TreeSet 和TreeMap 排序
TreeSet 有两种排序方式 1. Java.lang.Comparble+compareTo new TreeSet() 用这种方法必须要求实体类实现Comparable接口,也就是说要求添加到T ...
- CSS是什么?W3C是什么?W3C盒子与IE盒子的区别?
(1)层叠样式(Cascading Style Sheets, CSS)是用来表现HTML或XML文本样式的语言. (2)W3C推荐规范(World Wide Web Consortium,W3C ...
- NodeJS”热部署“代码,实现动态调试(hotnode,可以实现热更新)
NodeJS”热部署“代码,实现动态调试 开发中遇到的问题 如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你在开发 Node.js 实现的 HTTP 应用时会 ...
- WSDL入门
WSDL: 网络服务描述语言, 是Web Service的描述语言,它全名:Web Services Description Language,是一门基于 XML 的语言,用于描述 Web Servi ...
- R: which(查询位置)、%in% (是否存在)、ifelse(判断是否):
################################################### 问题:ifelse.which.%in% 18.4.27 解决方案: > x < ...
- p1098 逆序对
传送门 题目 输入格式: 第一行,一个数n,表示序列中有n个数. 第二行n个数,表示给定的序列. 输出格式: 给定序列中逆序对的数目. 数据范围: 对于50%的数据,n≤2500 对于100%的数据, ...
- HDU 5973 Game of Taking Stones (威佐夫博弈+高精度)
题意:给定两堆石子,每个人可以从任意一堆拿任意个,也可以从两堆中拿相同的数量,问谁赢. 析:直接运用威佐夫博弈,floor(abs(a, b) * (sqrt(5)+1)/2) == min(a, b ...
- hdu1074
#include <iostream> #include <string> #include <cstring> #include <stack> #i ...
- C#String.Split (string[], StringSplitOptions)中的StringSplitOptions是什么意思,看了msdn还是不懂?
MSDN上面这样子写的: [ComVisibleAttribute(false)] public string[] Split(string[] separator,StringSplitOption ...
- Unity3d 脚本使用规则
脚本是Unity游戏开发的重要组成部分,通过脚本可以监听游戏中的相关事件和响应玩家的输入,并在游戏中安排事件发生.另外,脚本还可用于创建图形效果,控制对象的物理行为等.在Unity中使用脚本是需要注意 ...