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. mysql desc esc 基本命令总结

    asc 按升序排列desc 按降序排列 下列语句部分是Mssql语句,不可以在access中使用. SQL分类:DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)DML—数据操 ...

  2. Spark- 使用hiveContext时提交作业报错

    在spark上操作hive时不需要搭建hive环境,只需要从现有的hive集群中hive的conf目录下拷贝 hive-site.xml 到spark的conf目录下即可提交程序运行 出现报错 Cau ...

  3. 使用mybatis报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.String]

    报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.St ...

  4. EntityFramework之领域驱动设计实践

    EntityFramework之领域驱动设计实践 - 前言 EntityFramework之领域驱动设计实践 (一):从DataTable到EntityObject EntityFramework之领 ...

  5. 一般处理程序ashx中用session存储数据

    如果要使用session的话,在handler的代码中添加System.Web.SessionState的引用,并让这个handler继承IRequiresSessionState接口,一定要继承这个 ...

  6. 通过ISBN获取豆瓣详细书籍资料

    手里有四十几万的图书馆书籍的isbn编号,通过isbn去请求豆瓣书籍的详细资料. # -*- coding: utf-8 -*- # @Time : 18-10-2 下午10:27 # @Author ...

  7. Linux命令四

    作业一: 1) 开启Linux系统前添加一块大小为20G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 安装的是命令行界面 3) 为新加的硬盘分区,一个主分区大小为10G,剩余空间给扩展分区,在扩 ...

  8. CSS:Tutorial three

    1.CSS Links links can be styled differently depending on what state they are in. The four links stat ...

  9. 旧书重温:0day2【5】shellcode变形记

    紧接上一篇,结合第一篇 //这篇文章主要成功溢出一个带有缓冲区溢出的小程序,其中我们的shellcode被strcpy截断了所以我们需要变形shellcode,这个实验中也出现了很多意想不到的拦路虎, ...

  10. MPLS基础一

    多协议标签交换(MPLS) 是一种用于快速数据包交换和路由的体系,具有管理各种不同形式通信流的机制. 内容:RID     /     MTU     /      认证    /     TTL   ...