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序+树状 ...
随机推荐
- SharePoint服务器端对象模型 之 访问网站和列表数据(Part 4)
(四)栏/字段 SharePoint中的字段(中文版中叫做"栏")与传统的数据栏类似,也有不同类型的区别,不过SharePoint中内置的栏类型除了按照数据类型(如数字.日期和时间 ...
- TestClass必须是public的
运行一个测试类遇到一下问题: namespace TestSample.Sample { [TestClass] class CynthiaTest { [Te ...
- ASP非法赋值
Microsoft VBScript 运行时错误 错误 '800a01f5' 非法赋值: 'isCloudSpeedupMz' /records/config/class-records.asp,行 ...
- netty http客户端从web服务器获取消息遇到的HttpChunk问题
有时候,Web服务器生成HTTP Response是无法在Header就确定消息大小的,这时一般来说服务器将不会提供Content-Length的头信息,而采用Chunked编码动态的提供body内容 ...
- kubernetes 搭建教程
http://blog.csdn.net/u011563903/article/details/71037093
- Java基础—复用类
复用代码是Java众多引人注目的功能之一. 一般而言,实现代码重用java提供了两种方式:组合以及继承. 组合:新的类由现有类的对象所组成.(复用现有代码的功能,而非它的形式) 继承:按照现有类的类型 ...
- JavaScript setInterval()執行clearInterval() 再恢復setInterval()
clearInterval() 方法可取消由 setInterval() 设置的 timeout. clearInterval() 方法的参数必须是由 setInterval() 返回的 ID 值. ...
- Nginx图片及样式文件不记录访问日志
1.Nginx作为web服务器是可直接在server配置文件中做如下配置: server { listen ; server_name www.fansik.com; access_log /data ...
- 【读书笔记】Java核心技术-基础知识-反射
在网页中运行Java程序称为applet. 反射 这项功能被大量地应用于JavaBeans中,它是Java组件的体系结构. 能够分析类能力的程序称为反射(reflective).反射机制的功能及其强大 ...
- socket()模块和套接字对象的内建方法
一.socket()模块函数 要使用socket.socket()函数来创建套接字,其语法如下: socket(socket_family,socket_type,protocol=0) 如上所述,s ...