hdu 5877(树状数组+dfs)
Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2387 Accepted Submission(s): 740
(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?
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
2 3
1 2
1 2
///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)的更多相关文章
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序
[题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...
- HDU 5877 Weak Pair(树状数组+dfs+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- 【BZOJ-1103】大都市meg 树状数组 + DFS序
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2009 Solved: 1056[Submit][Sta ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
随机推荐
- [CodeVs1050]棋盘染色2(状态压缩DP)
题目大意:有一个5*N(≤100)的棋盘,棋盘中的一些格子已经被染成了黑色,求最少对多少格子染色,所有的黑色能连成一块. 这题卡了我1h,写了2.6k的代码,清明作业一坨还没做啊...之前一直以为这题 ...
- [JOI 2015 Final] 分蛋糕 2
link 试题分析 容易发现性质,选择的是一段区间,但是贪心无法去维护这件事情,所以考虑$dp$,且我们只要去设计关于$JOI$的选择. 设$dp(i,j)$为现在要在$[l,r]$区间内选择,然后就 ...
- Codeforces 894.C Marco and GCD Sequence
C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces 894.B Ralph And His Magic Field
B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...
- kotlin Hello World 以及关键字
hello world:(支持函数式编程,不需要放到 class 中) fun main(args: Array<String>) { println("Hello World& ...
- OA项目中的论坛模块设计与实现
1.论坛是什么?论坛与贴吧有什么区别? 简单的说论坛和贴吧都是发表言论和讨论的一个平台. 贴吧是论坛的一个部分. 2.关于论坛模块的需求分析? 首先我们看看论坛的几个设计页面: 这个主要是论坛的版块设 ...
- Linux常用网络工具:批量主机服务扫描之netcat
netcat又叫做瑞士军刀,是黑客和系统管理员常用的网络工具,最初开发的目的是文件传输,后来发展出很多强大的功能,比如也可以完成批量主机服务扫描. 之前介绍了另一个更常用的批量主机服务扫描工具:nma ...
- hashlib模块--加密
用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib m = ha ...
- jquery读取html5的data-属性
前端代码的工作无非就是接收后端发来的数据,展示到前端页面:又或者,给无数的按钮,图片,段落等绑定各种事件.那么我们在绑定事件是需要拿取HTML页面的元素,以及在拿取的元素给定各式各样的自定义属性.当需 ...
- HashMap源码翻译
/* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...