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

题意十分明确, 就是求出符合题意的有序点对个数。

首先对ai离散,离散之后的结果用rk[i]表示,然后进行二分预处理得到f[i],其中f[i]的意义为:其他的点和i这个节点满足weakpair要求的权值最大名次(名次权值小的排在前面)。

然后就开始跑一遍DFS,树状数组维护一下答案,就好了。

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n)                for(int i(0); i <  (n); ++i)
#define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define dec(i,a,b) for(int i(a); i >= (b); --i)
#define for_edge(i,x) for(int i = H[x]; i; i = X[i]) #define LL long long
#define ULL unsigned long long
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define INF 1 << 30 const int N = 300000 + 10;
const int M = 10000 + 10;
const int Q = 1000 + 10;
const int A = 30 + 1; struct Node{
LL num;
int id;
friend bool operator < (const Node &a, const Node &b){
return a.num < b.num;
}
} tree[N]; int E[N << 1], X[N << 1], H[N << 1];
LL a[N];
int T, et;
int n;
LL k;
int x, y;
LL rk[N], f[N];
LL now;
int l, r;
bool pa[N];
int root;
LL c[N];
LL ans;
bool v[N]; inline void addedge(int a, int b){
E[++et] = b, X[et] = H[a], H[a] = et;
} inline void add(LL x, LL val){
for (; x <= n; x += (x) & (-x))
c[x] += val;
} inline LL query(LL x){
LL ret(0);
for (; x; x -= (x) & (-x)) ret += c[x];
return ret;
} void dfs(int x){
add(rk[x], 1);
for_edge(i, x) if (!v[E[i]]) dfs(E[i]), v[E[i]] = true;
add(rk[x], -1);
ans += query(f[x]);
} int main(){ scanf("%d", &T);
while (T--){
et = 0;
scanf("%d%lld", &n, &k);
rep(i, 1, n) scanf("%lld", a + i);
memset(v, false, sizeof v);
memset(pa, true, sizeof pa);
memset(tree, 0, sizeof tree);
memset(H, 0, sizeof H);
rep(i, 1, n - 1){
scanf("%d%d", &x, &y);
addedge(x, y);
pa[y] = false;
} rep(i, 1, n){
tree[i].num = a[i];
tree[i].id = i;
} sort(tree + 1, tree + n + 1);
rk[tree[1].id] = 1;
rep(i, 2, n)
if (tree[i].num == tree[i - 1].num) rk[tree[i].id] = rk[tree[i - 1].id];
else rk[tree[i].id] = rk[tree[i - 1].id] + 1; rep(i, 1, n){
now = k / a[i];
l = 1; r = n;
if (tree[1].num > now) f[i] = 0;
else{
while (l + 1 < r){
int mid = (l + r) >> 1;
if (tree[mid].num <= now) l = mid;
else r = mid - 1;
} if (tree[r].num <= now) l = r;
f[i] = rk[tree[l].id];
} }
root = 0;
rep(i, 1, n) if (pa[i]){ root = i; break;}
memset(c, 0, sizeof c);
ans = 0;
v[root] = true;
dfs(root);
printf("%lld\n", ans); } return 0; }

HDU5877Weak Pair的更多相关文章

  1. c++ pair 使用

    1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...

  2. 论Pair的重要性

    这些天我在用React和D3做图表,从已经实现的图表里复制了一些坐标轴的代码,发现坐标轴上的n个点里,只有第一个点下面能渲染出文字提示,其余点下面都无法渲染出文字. 和组里的FL一起百思不得其解好几天 ...

  3. 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  4. pair的使用

    #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...

  5. 【C++】pair

    STL的pair,有两个值,可以是不同的类型. template <class T1, class T2> struct pair; 注意,pair在头文件utility中,不要inclu ...

  6. hackerrank Similar Pair

    传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...

  7. uva12546. LCM Pair Sum

    uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...

  8. C++标准库 -- pair

    头文件:<utility> 可访问属性: first 第一个值 second 第二个值 可访问方法: swap(pair) 和另外一个pair交换值 其他相关方法: make_pair(v ...

  9. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

随机推荐

  1. this.$router 和this.$route 的区别

    1. this.$router: 表示全局路由器对象,项目中通过router路由参数注入路由之后,在任何一个页面都可以通过此方法获取到路由器对象,并调用其push(), go()等方法: 2. thi ...

  2. CodeForces 768E Game of Stones 打表找规律

    题意: 在经典Nim博弈的基础上增加了新的限制:如果从这堆石子中移走\(x\)个石子,那么之后就不能再从这堆移走\(x\)个. 分析: 因为之前的操作会对后面的转移有影响,所以在保存状态时还要记录哪些 ...

  3. PostgreSql基础命令及问题总结

    本章内容: 1.基本命令 基本命令 1.psql -U cdnetworks_beian -d cdnetworks_beian         #-U指定用户,-d指定数据库 2.\l        ...

  4. export、export default、module.export区别

    在es6里面定义模块,导出模块时可以使用export.export default 这2者区别: 在同一个文件里面可以有多个export, 一个文件里面只能有1个export default //a. ...

  5. 69、Android 布局中轻松实现图片的全屏、居中、平铺

    public void paint() { if (item.laying_mode != 1)//平铺或者充满 { new AsyncTask<Void, Void, Void>() { ...

  6. Hadoop架构的初略总结(2)

    Hadoop架构的初略总结(2) 回顾一下前文,我们总结了以下几个方面.我们为什么需要Hadoop:Hadoop2.0生态系统的构成:Hadoop1.0中HDFS和MapReduce的结构模型. 我们 ...

  7. 从xml文件中绑定数据到DropDownList控件上

    参考了2篇文章: http://www.cnblogs.com/JuneZhang/archive/2010/11/23/1885671.html http://blog.sina.com.cn/s/ ...

  8. 第六篇:python基础_6 内置函数与常用模块(一)

    本篇内容 内置函数 匿名函数 re模块 time模块 random模块 os模块 sys模块 json与pickle模块 shelve模块 一. 内置函数 1.定义 内置函数又被称为工厂函数. 2.常 ...

  9. 初识laytpl

    laytpl-精致巧妙的JavaScript模板引擎 这两天在做一个mui项目,列表需要循环很多的数据.在公司同事的指引下认识了这个新的模板--laytpl.我只想说,很好用们很巧妙. 废话不多说,直 ...

  10. HTML 改变文字方向

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...