HDU5877Weak Pair
Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
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?
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
1
2 3
1 2
1 2
1
题意十分明确, 就是求出符合题意的有序点对个数。
首先对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的更多相关文章
- c++ pair 使用
1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...
- 论Pair的重要性
这些天我在用React和D3做图表,从已经实现的图表里复制了一些坐标轴的代码,发现坐标轴上的n个点里,只有第一个点下面能渲染出文字提示,其余点下面都无法渲染出文字. 和组里的FL一起百思不得其解好几天 ...
- 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 ...
- pair的使用
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...
- 【C++】pair
STL的pair,有两个值,可以是不同的类型. template <class T1, class T2> struct pair; 注意,pair在头文件utility中,不要inclu ...
- hackerrank Similar Pair
传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...
- uva12546. LCM Pair Sum
uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...
- C++标准库 -- pair
头文件:<utility> 可访问属性: first 第一个值 second 第二个值 可访问方法: swap(pair) 和另外一个pair交换值 其他相关方法: make_pair(v ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
随机推荐
- TI C6000优化手册——让代码看起来像钉子
DSP芯片的出现,是为了解决大量的数字运算问题.通过集成专用的加法器.乘法器.地址产生器.复杂逻辑等硬件单元,DSP能实现比普通单片机更快速的数字运算,使处理器更适用于实时性高.复杂度强的处理场合.也 ...
- Linux命令之---diff
命令介绍 diff命令可以酌行比较纯文本文件内的内容,并输出文件的差异. 命令格式 diff [option] [file1] [file2] 举例子 1)比较俩文本文件 [root@king ~]# ...
- #3 working with data stored in files && securing your application
This chapter reveals that you can use files and databases together to build PHP application that waa ...
- COGS:313. [POI2001] 和平委员会
313. [POI2001] 和平委员会 ★★☆ 输入文件:spo.in 输出文件:spo.out 评测插件时间限制:1 s 内存限制:128 MB 题目描述 根据宪法,Bytelan ...
- 第八届蓝桥杯C/C++ B组省赛----分巧克力
分巧克力 问题描述 儿童节那天有K位小朋友到小明家做客.小明拿出了珍藏的巧克力招待小朋友们. 小明一共有N块巧克力,其中第i块是Hi x Wi的方格组成的长方形. 为了公平起见,小明需要从这 N 块巧 ...
- jq阻止ajax进行多次提交
在函数定义全局变量..var Stch=falseif (Stch==true){alert('请不要重新提交');}else{Stch=true;$.ajax({type:"POST&qu ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- bugku 普通的二维码
记录下对进制转换实现的重新思考. 扫描二维码扫到了一句垃圾话. 拖到winhex里面. 一开始以为是十进制直接转ascii,发现错误. 后来发现,最大数是7,八进制转换吧. 我是打算用python的l ...
- Visual C++ 连连看游戏源代码
点击下载
- 聊聊、Java 命令 第二篇
第一篇类之间没有依赖关系,所以比较简单,这一篇来看看有依赖的类怎么编译和执行. (一)Java 运行 class 有依赖 Person 是一个接口,只有一个 getName 方法.Man 则实现了 P ...