链接:戳这里
Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Problem Description
You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a non-negative value ai is assigned.An ordered pair of nodes (u,v) is said to be weak if
(1) u is an ancestor of v (Note: In this problem a node u is not considered an ancestor of itself);
(2) au×av≤k.
Can you find the number of weak pairs in the tree?
Input
There are multiple cases in the data set.
The first line of input contains an integer T denoting number of test cases.
For each case, the first line contains two space-separated integers, N and k, respectively.
The second line contains N space-separated integers, denoting a1 to aN.
Each of the subsequent lines contains two space-separated integers defining an edge connecting nodes u and v , where node u is the parent of node v.
Constrains:
1≤N≤105
0≤ai≤109
0≤k≤1018
Output
For each test case, print a single integer on a single line denoting the number of weak pairs in the tree.
Sample Input
1
2 3
1 2
1 2
Sample Output
1
题意:
n个节点的树,节点的点权为ai,要求找出有多少个二元组(u,v)满足
1:u是v的祖先且u!=v
2:a[u]*a[v]<=K
思路:
跑DFS的过程的时候,其实就是祖先到儿子的过程,但是会有兄弟的干扰,想象一下DFS序,我们要把兄弟节点删掉
然后DFS序里面的节点都是当前v的祖先,只需要快速找出有多少个祖先满足条件
那么我们考虑a[u]*a[v]<=K,则要快速找出前面的有多少个祖先a[u]满足a[u]<=K/a[v],线段树维护就可以了
这里数据很大所以要离散化
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include