Weak Pair

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

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 weakif
  (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
/*
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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ( ...

  6. 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 ...

  7. 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 ...

  8. 2016 ACM/ICPC Asia Regional Dalian Online(更新到五道题)

    1006 Football Games 这道题输入也很阴险!!! 这道题过题姿势最优雅的,不是if else if else if.那样很容易wa的. 如果没有平手选项, 赢得加一分的话, 可以用La ...

  9. 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  ...

随机推荐

  1. 201621123060《JAVA程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...

  2. 1013团队Beta冲刺day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  3. mysql5.7在windows下面的主从复制配置

    目标:自动同步Master 服务器上面的Demo数据库到Slave 服务器的Demo数据库中. 对于一些操作系统比较强而使用频率又不高的东西,往往好久不去弄就忘记了,所以要经常记录起来,方便日后查阅. ...

  4. linux 下 nc 命令的使用

    netcat被誉为网络安全界的'瑞士军刀',一个简单而有用的工具,透过使用TCP或UDP协议的网络连接去读写数据.它被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强 ...

  5. 从数据恢复角度解析RAID6结构原理

    [什么是RAID]    RAID的概念描述在互联网上比比皆是,用最简单的原理描述,就是在定义存储方式时允许在一部分数据缺失的情况下不影响全部数据,类似于通讯领域的纠错码.不同的冗余模式形成了不同的R ...

  6. nyoj 疯牛

    疯牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...

  7. Nginx配置小结

    前两天区听了一堂Nginx的课,然后翻了一下自己之前的Nginx的笔记,做了一个简单的小结. 全局变量 $args : 这个变量等于请求行中的参数,同$query_string $content_le ...

  8. python3 常用模块

    一.time与datetime模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们 ...

  9. [扩展推荐] —— Laravel Log 增强

    Laravel Log Enhancer 是 Laravel 5.6  的一个扩展包,可以在 Laravel 日志中添加额外的数据. 得益于 Laravel 5.6 中日志的更新,这个包利用这些特性扩 ...

  10. emqtt 试用(六)系统主题

    $SYS-系统主题 EMQ 消息服务器周期性发布自身运行状态.MQTT 协议统计.客户端上下线状态到 $SYS/ 开头系统主题. $SYS 主题路径以 "$SYS/brokers/{node ...