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
 
Source
题意:给你一颗树,给点的权值,和树的边,求每个点v的祖先u,并且a[u]*a[v]<=K;
思路:利用dfs序可以快速的得到每个点的祖先,每次更新a[u],找到k/a[v]>=a[u]的个数树状数组优化;
#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序+树状数组+离散化的更多相关文章

  1. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)

    题目: Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There ar ...

  3. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  4. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  5. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  6. 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 ...

  7. 计蒜客A1998 Ka Chang (分块+dfs序+树状数组)

    题意 给你一个\(1e5\)的有点权的树,有\(1e5\)个操作: 1.给第\(x\)层的点加上\(y\) 2.求以\(x\)为根的子树的点权和 思路 首先处理出层数为x的所有点 操作2一般都是用df ...

  8. 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 ...

  9. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

随机推荐

  1. LightOJ1089

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26806 题目大意:略 题目思路:前缀和与离散化 可用线段树做,但是 ...

  2. 【BZOJ2150】部落战争 最小流

    [BZOJ2150]部落战争 Description lanzerb的部落在A国的上部,他们不满天寒地冻的环境,于是准备向A国的下部征战来获得更大的领土. A国是一个M*N的矩阵,其中某些地方是城镇, ...

  3. SharePoint服务器端对象模型 之 使用CAML进行数据查询

    (一)概述 在SharePoint的开发应用中,查询是非常常用的一种手段,根据某些筛选.排序条件,获得某个列表或者某一些列表中相应的列表条目的集合. 除去列表上的查询之外,在SharePoint中还大 ...

  4. Facebook背后的软件

    Facebook的数据规模使得很多传统的解决方案根本不适用,或者无法分解来处理.保持一个拥有5亿用户的系统一直稳定可靠的运行,并不是一件很容易的事情.这篇文章介绍了一下Facebook使用的软件. F ...

  5. MySQL将语句写入到binlog二进制日志中

    由于二进制日志是公共资源,所有线程都要写二进制日志,所以一定要避免两个线程同时更新二进制日志.因此,在事件组写二进制日志时,二进制日志将获得一个互斥锁LOCK_log,然后在事件组写完后释放,由于服务 ...

  6. JS单例

    s = (function S(){ var bean; function get(){ if(bean){ return bean }else{ bean = T(); return bean; } ...

  7. centos7 yum安装MongoDB

    centos7 yum安装MongoDB   原文博客地址http://xgs888.top/post/view?id=64 centos7 yum安装mongodb: 1:创建仓库 vi /etc/ ...

  8. gearman background后台job状态获取

    GearmanClient background job有一个方法叫: public array GearmanClient::jobStatus ( string $job_handle ) Get ...

  9. Eclipse Find/Replace

    1.Eclipse内容助手 选中Regular expressions,使用正则表达式进行匹配.图中出现了小黄灯,Ctrl+Space显示出帮助信息. 2.Wrap search(循环检索)选中后,检 ...

  10. notepad++运行python代码

    notepad++运行代码 在菜单栏中点击运行或按F5,在弹出的对话框中输入下面命令 cmd /k E:\py3.6\python.exe "$(FULL_CURRENT_PATH)&quo ...