hdu 5877 Weak Pair (Treap)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877
题面;
Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5706 Accepted Submission(s): 1617
tree of N
nodes, labeled from 1 to N
. To the i
th 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
×a
v
≤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
≤10
9
0≤k≤1018
2 3
1 2
1 2
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ll long long
#define ls t[x].ch[0]
#define rs t[x].ch[1]
const ll M = 2e5 +;
const ll inf = 1e18+;
ll rt,sz,ans,a[M],n,k;
struct node{
ll ch[],cnt,siz,val,rd;
}t[M];
vector<ll>g[M];
void up(ll x){
t[x].siz = t[ls].siz + t[rs].siz+t[x].cnt;
} void rotate(ll &x,ll d){
ll son = t[x].ch[d];
t[x].ch[d] = t[son].ch[d^];
t[son].ch[d^] = x; up(x); up(x=son);
} void ins(ll &x,ll val){
if(!x){
x = ++sz;
t[x].cnt = t[x].siz = ;
t[x].val = val,t[x].rd = rand();
return ;
}
t[x].siz ++;
if(t[x].val == val){
t[x].cnt++; return ;
}
ll d = t[x].val < val; ins(t[x].ch[d],val);
if(t[x].rd > t[t[x].ch[d]].rd) rotate(x,d);
} void del(ll &x,ll val){
if(!x) return ;
if(t[x].val == val){
if(t[x].cnt > ){
t[x].cnt--,t[x].siz--;return ;
}
bool d = t[ls].rd > t[rs].rd;
if(ls == ||rs == ) x = ls+rs;
else rotate(x,d),del(x,val);
}
else t[x].siz--,del(t[x].ch[t[x].val<val],val);
} ll rk(ll x,ll val){
if(!x) return ;
if(t[x].val == val) return t[ls].siz+t[x].cnt;
if(t[x].val > val) return rk(ls,val);
return rk(rs,val)+t[ls].siz+t[x].cnt;
} void dfs(ll u,ll f){
ll num = inf;
if(a[u]!=) num = k/a[u];
ans += rk(rt,num);
ins(rt,a[u]);
for(ll i = ;i < g[u].size();i ++){
ll v = g[u][i];
if(v == f) continue;
dfs(v,u);
}
del(rt,a[u]);
} ll d[M]; void init(){
for(ll i = ;i < M;i ++){
t[i].val = ;d[i] = ;t[i].cnt=,t[i].siz = ;
t[i].ch[] = ; t[i].ch[] = ;
}
} int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
ll t,x,y;
cin>>t;
while(t--){
rt = ,ans = ,sz = ;
init();
cin>>n>>k;
for(ll i = ;i <= n;i ++) cin>>a[i];
for(ll i = ;i < n;i ++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
d[y]++;
}
for(ll i = ;i <= n;i ++)
if(d[i]==) {
dfs(i,); break;
}
cout<<ans<<endl;
for(ll i = ;i <= n ;i ++) g[i].clear();
}
}
hdu 5877 Weak Pair (Treap)的更多相关文章
- HDU 5877 Weak Pair(弱点对)
HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Jav ...
- HDU 5877 Weak Pair (2016年大连网络赛 J dfs+反向思维)
正难则反的思想还是不能灵活应用啊 题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系 我们反向思考 ...
- HDU 5877 Weak Pair(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...
- HDU 5877 Weak Pair(树状数组+dfs+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- 2016 ACM/ICPC Asia Regional Dalian Online HDU 5877 Weak Pair treap + dfs序
Weak Pair Problem Description You are given a rooted tree of N nodes, labeled from 1 to N. To the ...
- HDU - 5877 Weak Pair (dfs+树状数组)
题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...
- hdu 5877 Weak Pair dfs序+树状数组+离散化
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Prob ...
- HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化
http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...
随机推荐
- spring security运行流程图(转)
原文:http://blog.csdn.net/u011511684/article/details/31394493 示例下载地址:http://download.csdn.net/detail/u ...
- 【kindle笔记】之 《浪潮之巅》- 2018-1-
<浪潮之巅> 这本书推荐自最爱的政治课老师. 政治课老师张巍老师.我会一直记得你的. 以这样的身份来到这个学校,他人的质疑,自己的忐忑,老板的不公.犹犹豫豫谨小慎微地前进. 第一次听到这样 ...
- JEECG 单点登录 SSO
jeecg中用户登录的唯一性-CSDN问答https://ask.csdn.net/questions/656639 JEECG 集成KiSSO单点登录实现统一身份认证 - zhangdaiscott ...
- 转:MD5(Message-Digest Algorithm 一种哈希算法)
什么是MD5算法 MD5讯息摘要演算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码杂凑函数,可以产生出一个128位元(16位元组)的散列值(hash val ...
- 【学亮IT手记】利用字节流复制图片
- 日期选择器date、week、time、datetime、datetime-local类型
下面只写两个类型的代码案例,其他都大同小异 date类型: <!DOCTYPE html> <html> <head> <meta charset=" ...
- 微信小程序登录授权并获取手机号
一.请求发送 携带 code 到后台换取 openid var that = this; wx.login({ success(res) { console.log(res); var code = ...
- ubunto启动chrome报错
/usr/bin/google-chrome-stable[5199:5199:0703/143543.136117:ERROR:zygote_host_impl_linux.cc(88)] Runn ...
- PDO连接mysql数据库加载慢
今天在使用PDO连接mysql操作数据库的时候,发现速度特别慢,都1~2s的时间,不知道怎么回事,后来一步一步排除到new PDO 导致过慢的原因, 这个尴尬了...,调试了半天都没想到问下度娘,才知 ...
- zabbix添加监控Mysql
起因:zabbix自带的mysql监控模板直接使用会显示“不支持的”因为key的值是通过Mysql用户查看"show global status"信息或者用mysqladmin命令 ...