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 + 树状数组 + 其实不用离散化的更多相关文章

  1. HDU - 5877 Weak Pair (dfs+树状数组)

    题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...

  2. HDU 5877 Weak Pair(树状数组+dfs+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...

  3. HDU 5877 Weak Pair(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...

  4. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

  5. hdu 5877 Weak Pair dfs序+树状数组+离散化

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  6. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  7. Weak Pair (dfs+树状数组)

    Weak Pair (dfs+树状数组) 题意 这个题目是要求:一颗树上,有n个节点,给出每个节点的权值.另外给出一个值k,问有多少对节点满足: \(power[u]*power[v]<=k\) ...

  8. HDU 5877 Weak Pair(弱点对)

    HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Jav ...

  9. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. B+树索引和哈希索引的明显区别是:

    如果是等值查询,那么哈希索引明显有绝对优势,因为只需要经过一次算法即可找到相应的键值:当然了,这个前提是,键值都是唯一的.如果键值不是唯一的,就需要先找到该键所在位置,然后再根据链表往后扫描,直到找到 ...

  2. shock编程

    Socket接口是TCP/IP网络的API,Socket接口定义了许多函数或例程,程序员可以用它们来开发TCP/IP网络上的应用程序.要学Internet上的TCP/IP网络编程,必须理解Socket ...

  3. hdu 5600 N bulbs 想法+奇偶讨论

    http://acm.hdu.edu.cn/showproblem.php?pid=5600 本文重在分析该题目的思路,代码极其短,但是想到这个题目的思路却是挺复杂的过程. 思路 自己拿到题目也想到了 ...

  4. 批量创建10个系统帐号tianda01-tianda10并设置密码

    #.添加用户 useradd tianda01 #.非交互式给密码 echo "pass"|passwd --stdin tianda #.- 加0思路 ()..} () #随机密 ...

  5. [51nod1101]换零钱

    题意:给定钱,计算其能换成零钱的分类种数. 解题关键:完全背包计数. $dp[i][j]$表示前i个物品构成j元的种类数,然后优化一维. #include<bits/stdc++.h> u ...

  6. python调用Linux下so文件

    1.通过C语言编写一个简单max函数,生成一个max.so链接库 /* * # -shared 为链接库 让编译器知道是要编译一个共享库 * # -fPIC(Position Independent ...

  7. Spring Boot 高效数据聚合之道

    项目地址和示例代码: https://github.com/lvyahui8/spring-boot-data-aggregator 背景 接口开发是后端开发中最常见的场景, 可能是RESTFul接口 ...

  8. Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute

    DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...

  9. 6.6 安装IDEA

    非常感谢Kevin指导.让我简化了安装步骤.安装包可以直接到我的公司文件夹中sunny文件夹中获取. 首先准备好安装包: 然后打开终端: 解压,进入bin目录,执行idea.sh;或者,直接运行: b ...

  10. 【转】‘svn’不是内部或外部命令,也不是可运行的程序

    解决方法:windows安装svn的时候默认是不安装 svn command line这个东西的,重新打开svn的安装exe,选择modify,将“command line client tools” ...