Weak Pair

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

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
 
思路:将公式au*av<=k变换为 au<=k/av。 在遍历结点v的过程中,统计au<=k/av的节点u的个数。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = ;
int n, bit[MAXN+MAXN], deg[MAXN], len, vis[MAXN];
LL val[MAXN], k;
vector<int> arc[MAXN];
LL res;
LL buf[MAXN+MAXN];
void add(int i, int x)
{
while(i < MAXN + MAXN)
{
bit[i] += x;
i += (i & (-i));
}
}
int sum(int i)
{
int s = ;
while(i > )
{
s += bit[i];
i -= (i & (-i));
}
return s;
}
void dfs(int u)
{
int id = lower_bound(buf, buf + len, k / val[u]) - buf + ;
int pre = sum(id);
for(int i = , size = arc[u].size(); i < size; i++)
{
dfs(arc[u][i]);
}
int post = sum(id);
res += (post - pre); int index = lower_bound(buf, buf + len, val[u]) - buf + ;
add(index, );
}
int main()
{
// freopen("input.in", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %I64d", &n, &k);
memset(bit, , sizeof(bit));
memset(deg, , sizeof(deg));
len = ;
res = ;
for(int i = ; i <= n; i++) arc[i].clear();
for(int i = ; i <= n; i++)
{
scanf("%I64d", &val[i]);
buf[len++] = val[i];
buf[len++] = k / val[i];
}
for(int i = ; i < n - ; i++)
{
int u, v;
scanf("%d %d", &u, &v);
arc[u].push_back(v);
deg[v]++;
}
sort(buf, buf + len);
for(int i = ; i <= n; i++)
{
if(deg[i] == )
{
dfs(i);
break;
}
}
printf("%I64d\n", res);
}
return ;
}

HDOJ5877(dfs序+离散化+树状数组)的更多相关文章

  1. BZOJ 2819: Nim dfs序维护树状数组,倍增

    1.随机选两个堆v,u,询问若在v到u间的路径上的石子堆中玩Nim游戏,是否有必胜策略,如果有,vfleaking将会考虑将这些石子堆作为初始局面之一,用来坑玩家.2.把堆v中的石子数变为k. 分析: ...

  2. BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...

  3. HDU 6203 ping ping ping(dfs序+LCA+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意: n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V 无法连 ...

  4. 2018.06.30 BZOJ4765: 普通计算姬(dfs序+分块+树状数组)

    4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MB Description "奋战三星期,造台计算机".小G响应号召,花了三小时 ...

  5. POJ 2763 Housewife Wind(DFS序+LCA+树状数组)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11419   Accepted: 3140 D ...

  6. 【BZOJ1103】大都市meg(DFS序,树状数组)

    题意:有一颗树,1号点为根,保证编号小的点深度较小,初始状态每条边都没有被标记,要求实现两个操作在线: A:将连接x,y的边标记 W:查询从1到x的路径上有多少条边未被标记 n<=2*10^5 ...

  7. BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)

    本来还想链剖的,结果才发现能直接树状数组的= = 记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- - CODE: ...

  8. 【Tyvj2133&BZOJ1146】网络管理Network(树套树,DFS序,树状数组,主席树,树上差分)

    题意:有一棵N个点的树,每个点有一个点权a[i],要求在线实现以下操作: 1:将X号点的点权修改为Y 2:查询X到Y的路径上第K大的点权 n,q<=80000 a[i]<=10^8 思路: ...

  9. 【POJ3321】Apple Tree(DFS序,树状数组)

    题意:给一棵n个节点的树,每个节点开始有一个苹果,m次操作 1.将某个结点的苹果数异或 1 2.查询一棵子树内的苹果数 n,m<=100000   思路:最近一段时间在思考树上统计问题的算法 发 ...

随机推荐

  1. 计算机网络 - IP和端口

    计算机网络分层模型 OSI分层模型:物理层.数据链路层.网络层.传输层.会话层.表示层.应用层: TCP/IP分层模型:物理+数据链路层.网络层.传输层.应用层: IP地址 IP地址是一个32位的整数 ...

  2. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  3. Softmax回归 softMax回归与logistic回归的关系

    简介 在本节中,我们介绍Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签  可以取两个以上的值. Softmax回归模型对于诸如MNIST手写数字分 ...

  4. selenium学习笔记(智能等待)

    博主在尝试对百度首页用selenium完成自动登录的功能 反复多次尝试元素定位方法也未写错.最后发现问题原因: 脚本运行速度快于页面加载速度 如百度首页登录例子.脚本已经开始寻找登录弹窗 但是页面仍在 ...

  5. .NET中 数据库连接

    (转自:http://www.iwms.net/n459c12.aspx) SQL Server ODBC  Standard Security:"Driver={SQL Server};S ...

  6. 什么是Activity,详细介绍Activity

    首先,Activity是Android系统中的四大组件之一,可以用于显示View.Activity是一个与用记交互的系统模块,几乎所有的Activity都是和用户进行交互的,但是如果这样就能说Acti ...

  7. Activiti 教程

    Activiti入门教程:http://blog.csdn.net/column/details/activitizhou.html Activiti 5.15 用户手册:http://www.cnb ...

  8. HAWQ取代传统数仓实践(七)——维度表技术之维度子集

    有些需求不需要最细节的数据.例如更想要某个月的销售汇总,而不是某天的数据.再比如相对于全部的销售数据,可能对某些特定状态的数据更感兴趣等.此时事实数据需要关联到特定的维度,这些特定维度包含在从细节维度 ...

  9. Gif图片验证码类

    新开发的安全验证码类,支持生成Gif图片验证码(带噪点,干扰线,网格,随机色背景,随机自定义字体,倾斜,Gif动画). 上图: 字体及字体文件的路径需要在类中$FontFilePath及$FontFi ...

  10. tslib: Selected device is not a touchscreen (must support ABS_X and ABS_Y events)

    /************************************************************************************ * tslib: Selec ...