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. V4L2学习(五)VIVI虚拟摄像头驱动

    概述 前面简单分析了内核中虚拟摄像头驱动 vivi 的框架与实现,本文参考 vivi 来写一个虚拟摄像头驱动,查询.设置视频格式相对简单,难点在于 vb2_buf 的处理过程. 数据采集流程分析 在我 ...

  2. Sicily 8843 Ranking and Friendship

    http://soj.me/8843 题意:几个人想做好朋友,朋友之间相差位置小于等于k,且长度相同分析:排序,将长度相同的放在一起.若长度相同,第i个人能放进去的条件是位置相差下雨等于k.      ...

  3. OpenResty安装与hello world

    安装环境:CentOS 7.0 1. 安装编译工具.依赖库 yum -y install readline-devel pcre-devel openssl-devel gcc 2. 下载openre ...

  4. vue-cli 中引入 jq

    vue-cli webpack 引入jquery   今天费了一下午的劲,终于在vue-cli 生成的工程中引入了jquery,记录一下.(模板用的webpack) 首先在package.json里的 ...

  5. leetcode 【 Copy List with Random Pointer 】 python 实现

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. 【NOIP 2012】借教室

    题目 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然希望 ...

  7. RT-Thread学习之——静态线程和动态线程

    RT-Thread中支持静态和动态两种定义方式. 用线程来举例的话,rt_thread_init对应静态定义方式,rt_thread_create对应动态定义方式. 使用静态定义方式时,必须先定义静态 ...

  8. [oldboy-django][4python面试]有关csrf跨站伪造请求攻击

    1 csrf定义 - csrf定义:Cross Site Request Forgery,跨站请求伪造 举例来说: 网站A伪造了一个图片链接: <a href="http://www. ...

  9. PAT1031

    一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...

  10. 基于kubuntu的C/C++开发环境搭建

    基于kubuntu的环境搭建 系统: kubuntu 14.04 中文输入法: SICM ibus fcitx:sougou 中文输入法的安装比较复杂,由于各种的不兼容,可能会出现各种的问题: 终端配 ...