hdu 5877 Weak Pair dfs序+树状数组+离散化
Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
(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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
vector<int>v[N];
int du[N];
ll l[N<<1],a[N];
int n,len;
ll k,ans;
int tree[N<<1];
void init(int n)
{
for(int i=1;i<=n;i++)
v[i].clear();
memset(tree,0,sizeof(tree));
memset(du,0,sizeof(du));
ans=0;
}
int getpos(ll x)
{
int pos=lower_bound(l,l+len,x)-l;
return pos+1;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int c)
{
while(x<(N<<1))
{
tree[x]+=c;
x+=lowbit(x);
}
}
ll query(int x)
{
ll ans=0;
while(x)
{
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
void dfs(int u)
{
int p,q;
if(a[u])
p=getpos(k/a[u]);
else
p=(N<<1)-1;
if(a[u])
q=getpos(a[u]);
else
q=1;
ans+=query(p);
update(q,1);
for(int i=0;i<v[u].size();i++)
{
dfs(v[u][i]);
}
update(q,-1);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int flag=0;
scanf("%d%lld",&n,&k);
init(n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]),l[flag++]=k/a[i],l[flag++]=a[i];
sort(l,l+flag);
len=unique(l,l+flag)-l;
for(int i=1;i<n;i++)
{
int u,w;
scanf("%d%d",&u,&w);
v[u].push_back(w);
du[w]++;
}
for(int i=1;i<=n;i++)
if(du[i]==0)
dfs(i);
printf("%lld\n",ans);
}
return 0;
}
hdu 5877 Weak Pair dfs序+树状数组+离散化的更多相关文章
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)
题目: Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There ar ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- 计蒜客A1998 Ka Chang (分块+dfs序+树状数组)
题意 给你一个\(1e5\)的有点权的树,有\(1e5\)个操作: 1.给第\(x\)层的点加上\(y\) 2.求以\(x\)为根的子树的点权和 思路 首先处理出层数为x的所有点 操作2一般都是用df ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
随机推荐
- Redis 单机版本安装
亲装! 1.linux 系统镜像 redis 版本 使用redis-3.2.8.tar.gz(截止2017年4月的最新稳定版) 在安装之前先安装下redis 需要的环境 wget http://do ...
- ECMAScript6补全字符串长度方法padStart()和padEnd()
一.padStart() 1.定义 padStart()方法用另一个字符串(默认为空格)重复填充到对象字符串到指定长度,填充从对象字符串左侧开始,返回新的字符串. 2.语法 str.padStart( ...
- 理解CSS3属性transition
一.说明 1.1 定义和用法 transition 属性是一个简写属性,用于设置四个过渡属性: transition-property:规定设置过渡效果的CSS属性的名称. transition-du ...
- 判断SQL数据库中函数、存储过程等是否存在的方法
下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助.库是否存在if exists(select * from master..sys ...
- locust参数化
前面用篇专门讲了requests实现接口的参数关联案例,这里直接转化成locust脚本就行了 # coding:utf-8 from locust import HttpLocust, TaskSet ...
- java基础:父类与子类之间变量和方法的调用
1)父类构造函数 java中当调用某个类的构造方法的时候,系统总会调用父类的非静态初始化块进行初始化,这个调用是隐式的,而且父类的静态初始化代码 块总是会被执行,接着调用父类的一个或者多个构造器执行初 ...
- SQLtie 增删该查
建表,添加数据,更新数据,删除数据,删除表 . 先介绍三个核心方法 1.openDatabase:这个方法使用现有数据库或创建新数据库创建数据库对象. 2.transaction:这个方法允许我们根据 ...
- 023-Spring Boot 服务的注册和发现
一.概述 服务调用 1.1.nginx方式 1.2.注册中心 二.注册中心[zookeeper] 2.1.安装zookeeper3.4.11 2.2.服务提供方,需要在服务启动时吗.,把服务的信息(I ...
- 1.2 使用电脑测试MC20模块的GPS功能测试
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- PyQt4 UI设计和调用 使用eric6
使用工具eric6 安装包地址: http://eric-ide.python-projects.org/eric-download.html 1.需要安装python和pyqt为前提,然后安装eri ...