Weak Pair

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2387    Accepted Submission(s): 740

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
 
Source
 
满足第一个条件的话进行DFS,然后统计父亲节点里面满足 val <= k/a[u] 的个数.这里可以用 树状数组进行维护.
///pro do this : a[l]%a[l+1]%...%a[r]
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL;
const int N = ;
const LL INF = 1e19;
LL a[N],b[N],c[N];
int cnt,n,m;
int indegree[N];
struct Edge{
int v,next;
}edge[N];
int head[N],tot;
LL ans,k;
void addEdge(int u,int v,int &k){
edge[k].v = v,edge[k].next = head[u],head[u] = k++;
}
void init(){
tot = ,ans = ;
memset(head,-,sizeof(head));
memset(indegree,,sizeof(indegree));
memset(c,,sizeof(c));
}
int lowbit(int x){
return x&(-x);
}
void update(int v,int idx){
for(int i=idx;i<=cnt;i+=lowbit(i)){
c[i] += v;
}
}
LL getsum(int idx){
LL ans=;
for(int i=idx;i>=;i-=lowbit(i)){
ans+=c[i];
}
return ans;
}
void dfs(int u){
int x = upper_bound(b+,b+cnt+,k/a[u])-b-; ///upper_bound 第一个大于当前元素的第一个数的下标.
int pos = lower_bound(b+,b+cnt+,a[u])-b; ///寻找离散化之后的下标
ans+=getsum(x);
update(,pos);
for(int i=head[u];i!=-;i=edge[i].next){
dfs(edge[i].v);
}
update(-,pos);
}
int main(){
int tcase;
scanf("%d",&tcase);
while(tcase--){
init();
scanf("%d%lld",&n,&k);
m = ;
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
b[++m] = a[i];
if(a[i]==) b[++m] = INF;
else b[++m] = k/a[i];
}
sort(b+,b+m+);
cnt = unique(b+,b+m+)-(b+);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v,tot);
indegree[v]++;
}
for(int i=;i<=n;i++){
if(indegree[i]==) dfs(i);
}
printf("%lld\n",ans);
}
return ;
}

hdu 5877(树状数组+dfs)的更多相关文章

  1. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序

    [题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...

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

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

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  7. 【BZOJ-1103】大都市meg 树状数组 + DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  8. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  9. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

随机推荐

  1. 【bzoj2402】陶陶的难题II

    Portal -->bzoj2402 Solution 这题的话,看到答案的形式想到分数规划(Portal -->[learning]) 套路一波,记当前二分的\(mid\)为\(\lam ...

  2. 高性能相关、Scrapy框架

    高性能相关 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. import requests def fetch_async(url): ...

  3. 驱动之DMA的介绍与应用20170210

    本文主要介绍的是DMA相关的知识,首先: 1)在实现DMA传输时,是由DMA控制器直接掌管总线,因此,存在着一个总线控制权转移问题.即DMA传输前,CPU要把 总线控制权交给DMA控制器,而在结束DM ...

  4. JS中如何使用EL表达式中的对象

    JS中如何使用EL表达式中的对象 2017年09月25日 15:33:09 lhpnba 阅读数:4859   1.js中使用el表达式要加双引号或单引号:'${list}' 2.js变量获取el表达 ...

  5. helm 安装prometheus operator 并监控ingress

    1.helm安装 curl https://raw.githubusercontent.com/helm/helm/master/scripts/get > get_helm.shchmod 7 ...

  6. 「Python」5个开源项目

    1-OpenAI universe Universe是一个能在世界上所有的游戏.网站和其他应用中,衡量和训练AI通用智能的软件平台. Universe,AI代理通过称为虚拟网络计算或VNC发送模拟的鼠 ...

  7. C语言基本类型的字节数

  8. Python学习笔记(四十)— 内置模块(9)HTMLParser

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432002312 ...

  9. <LC刷题二>回文字符串判断之leetcode125&234

    其他刷题记录见博客首页 1,leecode125 验证回文串 原题: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. ...

  10. 免密码登录服务器python脚本

    在自动化运维平台没有做完之前,常需要登录服务器做很多维护操作,每次找好长好长的密码,那么多服务器,你会疯掉的,所以瞎搞了以下脚本.先解一下燃眉之急,哈哈 cat login_root.exp #!/u ...