博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Weak Pair HDU - 5877(dfs+树状数组+离散化+二分)
阅读量:4136 次
发布时间:2019-05-25

本文共 1777 字,大约阅读时间需要 5 分钟。

You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a non-negative value ai is 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
给你一个树,去统计弱点对(两个点的权值积小于k),问有多少个这样的弱点对。
跟上一篇博客一样,也是dfs+树状数组。但是1e18的数据量,用树状数组肯定不行,所以需要离散化一下。在二分的时候,我们二分到了一个位置,用树状数组去更新。跑完一遍子树的时候,我们就统计这个子树里有多少对,然后直到跑完这个树。
代码如下:

#include
#include
#include
#include
#include
#include
#include
#define ll long longusing namespace std;const int maxx=1e5+100;int head[maxx*2];bool vis[maxx];ll a[maxx],b[maxx],c[maxx*2];struct edge{ int to; int next;}e[maxx*2];int n,tot,sign,size;ll k,ans;/*-------------事前准备---------------*/ void init(){ memset(head,-1,sizeof(head)); memset(c,0,sizeof(c)); memset(vis,0,sizeof(vis)); tot=0;}void add(int x,int y){ e[tot].to=y,e[tot].next=head[x],head[x]=tot++;}/*-------------树状数组----------------*/ int lowbit(int x){return x&-x;}void update(int x,ll add){ while(x

努力加油a啊,(o)/~

转载地址:http://myxvi.baihongyu.com/

你可能感兴趣的文章
HDU 25919 新生晚会(水题组合问题)
查看>>
HDU 2612 Find a way(两次bfs)
查看>>
HDU 2188 选拔志愿者(简单博弈+记忆化)
查看>>
HDU 5493 Queue(线段树)
查看>>
HDU 5563 Clarke and five-pointed star(暴力)
查看>>
HDU 3951 Coin Game(博弈取对称思路)
查看>>
HDU 4920 Matrix multiplication(简单矩阵相乘+技巧减少Mod次数)
查看>>
HDU 4525 威威猫系列故事——吃鸡腿(水题,合并递推公式就行)
查看>>
HDU 4561 连续最大积
查看>>
HDU 4557 非诚勿扰 (简单模拟)
查看>>
HDU 4550 卡片游戏(贪心+细心)
查看>>
蓝桥杯 算法训练 集合运算
查看>>
蓝桥杯 暗恋 简单搜索或者暴力或者bfs
查看>>
蓝桥杯 拦截导弹 动态规划(最长下降子序列+最长上升子序列)
查看>>
蓝桥杯 方格取数 (多线程DP)
查看>>
蓝桥杯 未名湖边的烦恼 (简单暴力dfs)
查看>>
蓝桥杯 黑白无常 (简单暴力枚举)
查看>>
蓝桥杯 基础练习 2n皇后问题 (简单dfs暴力+优化剪枝)
查看>>
蓝桥杯 基础训练 完美的代价(转)
查看>>
蓝桥杯 算法训练 矩阵乘方(矩阵快速幂取模)
查看>>