链接: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

Problem Description
You are given a rooted

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×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
 
 
 
思路: treap板子题,注意并没有规定1为根,根要自己找下,还有a[i]可能为0,此时需要特判下
初始化没清空左右儿子,一直RE,真实自闭
实现代码;
#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)的更多相关文章

  1. HDU 5877 Weak Pair(弱点对)

    HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Jav ...

  2. HDU 5877 Weak Pair (2016年大连网络赛 J dfs+反向思维)

    正难则反的思想还是不能灵活应用啊 题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系 我们反向思考 ...

  3. HDU 5877 Weak Pair(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...

  4. HDU 5877 Weak Pair(树状数组+dfs+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...

  5. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

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

  7. HDU - 5877 Weak Pair (dfs+树状数组)

    题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...

  8. hdu 5877 Weak Pair dfs序+树状数组+离散化

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  9. HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化

    http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...

随机推荐

  1. Full Regularization Path for Sparse Principal Component Analysis

    目录 背景 Notation Sparse PCA Semidefinite Relaxation Low Rank Optimization Sorting and Thresholding 背景 ...

  2. poj3468 线段树的懒惰标记

    题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...

  3. [Offer收割]编程练习赛97

    链接 [https://hihocoder.com/contest/offers97/problems] 题意 题目1 : 放置矩形 时间限制:10000ms 单点时限:1000ms 内存限制:256 ...

  4. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  5. iOS--LaunchImage启动页设置及问题解决

    在Assets.xcassets中使用LaunchImage来设置启动图:   一.根据不同屏幕尺寸的需求设置不同的图片,可以参照下图: 1.点击Image.xcassets 进入图片管理,然后右击, ...

  6. Java 学习使用常见的开源连接池

    目录 连接池介绍 自定义连接池 JDBC Tomcat Pool DBCP(DataBase Connection Pool) 使用配置文件来设置DBCP C3P0 Druid 连接池介绍 在说连接池 ...

  7. C99标准的柔性数组 (Flexible Array)

    [什么是柔性数组(Fliexible Array)] 柔性数组在C99中的定义是: 6.7.2.1 Structure and union specifiers As a special case, ...

  8. TCP 握手和挥手图解(有限状态机)

    1.引言 TCP 这段看过好几遍,老是记不住,没办法找工作涉及到网络编程这块,各种问 TCP .今天好好整理一下握手和挥手过程.献给跟我一样忙碌,找工作的童鞋,欢迎大神批评指正. 2.TCP 的连接建 ...

  9. Squid配置之使用帐号密码验证

      转自: https://blog.csdn.net/atco/article/details/43448885   1.安装squid使用root用户进行操作.先使用rpm检测是否已经安装了sql ...

  10. 面象对象设计原则之七:合成复用原则(Composition/Aggregate Reuse Principle, CARP)

    合成复用原则又称为组合/聚合复用原则(Composition/Aggregate Reuse Principle, CARP),其定义如下: 合成复用原则(Composite Reuse Princi ...