Weak Pair

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

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
分析:dfs+树状数组+离散化;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,h[maxn],tot,q[maxn],fa[maxn],num;
ll ans,a[maxn],b[maxn],c[maxn];
struct node
{
int to,nxt;
}e[maxn];
void add(int x,int y)
{
tot++;
e[tot].to=y;
e[tot].nxt=h[x];
h[x]=tot;
}
void gao(int x,int y)
{
for(int i=x;i<=num+;i+=(i&(-i)))
q[i]+=y;
}
int get(int x)
{
int ret=;
for(int i=x;i;i-=(i&(-i)))
ret+=q[i];
return ret;
}
void dfs(int now)
{
ans+=get(num+)-get(a[now]-);
gao(b[now],);
for(int i=h[now];i;i=e[i].nxt)
{
dfs(e[i].to);
}
gao(b[now],-);
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
ans=;
tot=;
j=;
ll p;
memset(h,,sizeof h);
memset(fa,,sizeof fa);
memset(q,,sizeof q);
scanf("%d%lld",&n,&p);
rep(i,,n){
scanf("%lld",&a[i]);
if(a[i]==)b[i]=1e19;
else b[i]=p/a[i];
c[j++]=a[i],c[j++]=b[i];
}
sort(c,c+j);
num=unique(c,c+j)-c;
rep(i,,n)a[i]=lower_bound(c,c+num,a[i])-c+,b[i]=lower_bound(c,c+num,b[i])-c+;
rep(i,,n-)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
fa[y]=x;
}
rep(i,,n)if(!fa[i])dfs(i);
printf("%lld\n",ans);
}
//system("Pause");
return ;
}

2016大连网络赛 Weak Pair的更多相关文章

  1. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota ...

  2. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

  3. 2016大连网络赛 Football Games

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) P ...

  4. 2016大连网络赛 Different GCD Subarray Query

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  5. 2016大连网络赛 Function

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Probl ...

  6. 2016大连网络赛 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) P ...

  7. HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)

    还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...

  8. 大连网络赛 1006 Football Games

    //大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...

  9. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

随机推荐

  1. javaWEB总结(7):HttpServlet和HttpServletRequest

    前言:HttpServletRequest对象封装了客户端进行HTTP协议请求时的所有信息,HttpServletRequest继承了ServletRequest,所以和ServletRequest一 ...

  2. MFC下DLL编程(图解)

    MFC下DLL编程(图解) DLL(Dynamic Link Library,动态链接库)是微软公司为Windows和OS/2操作系统设计一种供应用程序在运行时调用的共享函数库.DLL是应用程序的一种 ...

  3. 第十九节,基本数据类型,集合set

    集合set,无序,是不允许重复内容的,也就是不允许重复元素,如果有重复,会自动忽略,可接收可迭代类型 (一般用于需要判断和处理交集时候用到) 集合与字典的区别是,集合没有键只有值,字典是有键的字典是一 ...

  4. Java中try-catch-finally的一点理解

    在只有try-catch语句中,如果catch块中出现了return语句或者抛出了异常,那么catch之后的语句是执行不到的:但是如果将代码放入finally中,即使catch中出现了return语句 ...

  5. tsung: an open-source multi-protocol distributed load testing tool

     ROPERTIES::type: KnowledgeBase_Cloud:END: 开源.多协议.分布式的压力测试工具   Item Summary tsung-recorder start 通过p ...

  6. 期刊:DOI :10.3969/j.issn.1001-2400.2012.01.008

    DOI:10.3969/j.issn.1001-2400.2012.01.008 “/” 分为前缀和后缀两部分中间用一斜杠分开,前缀中又以小圆点分为两部分. "DOI":一篇期刊论 ...

  7. Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别

    在下面的示例中,客户端指的是浏览器,服务器指的是网站服务器主机. 为了更好的理解这些知识点,你应该简单了解典型的http网站是如何工作的. 普通的http: 客户端从服务器端请求网页 服务器作出相应的 ...

  8. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

  9. perl的USE和require

    来源: http://www.cnblogs.com/itech/archive/2010/11/22/1884345.html 相同: 都可以用来引用module(.PM). 不同: 1) 区别在于 ...

  10. hh monitor

    http://theholyjava.wordpress.com/2012/09/21/enabling-jmx-monitoring-for-hadoop-and-hive/ http://blog ...