hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 439 Accepted Submission(s): 155
(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
/*
hdu 5877 线段树 problem:
给你一棵n个节点的有根树,每个节点有价值a[i]. 问有多少个点对(u,v)满足:u是v的祖先且a[u]*a[v] <= k solve:
先找出这个树的根节点.
因为要求u是v的祖先,所以相当于v的父亲到根节点的所有点. 所以可以在树的遍历的时候把走过点的值存入线段树中,当走到第i个节点值
求出线段树中[1,k/a[i]]总共有多少个值就行. 然后递归回退时在把这个点删掉.
数据很大所以再进行一下离散化处理. hhh-2016-09-11 09:22:59
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#define lson i<<1
#define rson i<<1|1
#include <map>
#define ll long long using namespace std; const int maxn = 200100; int a[maxn]; struct Node
{
int l,r;
int val;
} tree[maxn <<2]; void push_up(int i)
{
tree[i].val= tree[lson].val + tree[rson].val;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].val = 0;
if(l == r)
{
return ;
}
int mid = (tree[i].l + tree[i].r) >> 1;
build(lson,l,mid);
build(rson,mid+1,r);
push_up(i);
} void update(int i,int k,int va)
{
if(tree[i].l == tree[i].r && tree[i].l == k)
{
tree[i].val += va;
return;
}
int mid = (tree[i].l + tree[i].r) >> 1;
if(k <= mid)
update(lson,k,va);
else
update(rson,k,va);
push_up(i);
} int query(int i,int l,int r)
{
if(l > r)
return 0;
if(tree[i].l >= l && tree[i].r <= r)
{
return tree[i].val;
}
int tans = 0;
int mid = (tree[i].l + tree[i].r ) >> 1;
if(l <= mid)
tans += query(lson,l,r);
if(r > mid)
tans += query(rson,l,r);
return tans;
} struct node
{
int next;
int to;
} edge[maxn];
ll k;
int ta,tot,n;
int head[maxn];
int deep[maxn];
int t[maxn];
void addedge(int from,int to)
{
edge[tot].to=to;
edge[tot].next=head[from];
head[from]=tot++;
}
ll ans = 0;
void dfs(int u,int fa)
{
int o=lower_bound(t,t+ta,k/a[u])-t;
ans+=query(1,0,o); int tk=lower_bound(t,t+ta,a[u])-t;
update(1,tk,1);
for(int i=head[u]; ~i; i=edge[i].next)
{
int v=edge[i].to;
dfs(v,u);
}
update(1,tk,-1);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %I64d",&n,&k);
int cnt=0;
for(int i=1; i<=n; i++)
{
scanf("%I64d",&a[i]);
t[cnt++]=a[i];
}
for(int i=1; i<=n; i++) t[cnt++]=k/a[i]; sort(t,t+cnt);
ta=unique(t,t+cnt)-t; tot=0;
memset(head,-1,sizeof(head));
memset(deep,0,sizeof(deep)); for(int i=0; i<n-1; i++)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v);
deep[v]++;
} ans=0;
build(1,0,ta);
for(int i=1; i<=n; i++)
if(deep[i]==0)
dfs(i,-1);
printf("%I64d\n",ans);
}
return 0;
}
hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)的更多相关文章
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)
Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K ...
- 2016 ACM/ICPC Asia Regional Dalian Online(更新到五道题)
1006 Football Games 这道题输入也很阴险!!! 这道题过题姿势最优雅的,不是if else if else if.那样很容易wa的. 如果没有平手选项, 赢得加一分的话, 可以用La ...
- 2016 ACM/ICPC Asia Regional Dalian Online HDU 5877 Weak Pair treap + dfs序
Weak Pair Problem Description You are given a rooted tree of N nodes, labeled from 1 to N. To the ...
随机推荐
- 学号:201621123032 《Java程序设计》第5周学习总结
1:本周学习总结 1.1: 写出你认为本周学习中比较重要的知识点关键词 接口interface,comparator接口和comparable接口. 1.2:尝试使用思维导图将这些关键词组织起来. 2 ...
- Python 双向链表
操作 is_empty() 链表是否为空 length() 链表长度 travel() 遍历链表 add(item) 链表头部添加 append(item) 链表尾部添加 insert(pos, it ...
- Tornado websocket应用
应用场景 WebSocket 的特点如下 适合服务器主动推送的场景(好友上线,即时聊天信息,火灾警告,股票涨停等) 相对于Ajax和Long poll等轮询技术,它更高效,不耗费网络带宽和计算资源 它 ...
- Struts2之Struts2的下载与安装
Struts2的下载 登陆struts的官网 下载Full Distribution这个选项的struts2的包. 这是Struts2的完整版,里面包括Struts2的实例应用,空实例应用,核心库,源 ...
- LR回放https协议脚本失败: 错误 -27778: 在尝试与主机“www.baidu.com”connect 时发生 SSL 协议错误
今天用LR录制脚本协议为https协议,回放脚本时出现报错: Action.c(14): 错误 -27778: 在尝试与主机"www.baidu.com"connect 时发生 S ...
- 原生ajax的请求函数
ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务器.发送请求.接收响应 ...
- Browser Object Model
BOM:浏览器提供的一系列对象 window对象是BOM最顶层对象 * 计时器setInterval(函数,时间)设置计时器 时间以毫秒为单位 clearInterval(timer) 暂停计时器se ...
- video与audio的使用
HTML5 DOM 为 <audio> 和 <video> 元素提供了方法.属性和事件. 这些方法.属性和事件允许您使用 JavaScript 来操作 <audio> ...
- python 基础 set 集合类型补充
为啥今天又重提这个数据类型呢?平时用的少,等要用起来的时候才发现,自己对这块啥都不知道了,so,今天就把这块再梳理一下咯. 一.set集合,是一个无序且不重复的元素集合.这一点是非常重要的. 二.集合 ...
- python 内置函数之lambda-filter-reduce-apply-map
(1)lambda lambda是Python中一个很有用的语法,它允许你快速定义单行最小函数.类似于C语言中的宏,可以用在任何需要函数的地方. 基本语法如下: 函数名 = lambda args1, ...