Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3952 Accepted Submission(s): 1179
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?
(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
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
Recommend
wange2014
题意:给你一棵树,树上每个节点有一个值a[i],求有多少对u,v满足a[u]*a[v]<=k
解题思路:对所有值离散一下,在DFS的时候用树状数组统计一下就可以了,或者可以用线段树启发式合并的方法
dfs+树状数组:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int t, n,root,cnt,m,u,v;
int x[100009],vis[100009];
int s[100009], nt[100009], e[100009];
LL w[100009], a[100009],kk,ans;
int lowbit(int k) {return k&-k;}
void add(int k, int val) {for (int i = k; i <m ; i += lowbit(i)) x[i] += val;}
int getsum(int k){int sum = 0;for (int i = k; i; i -= lowbit(i))sum += x[i];return sum;}
void dfs(int k)
{
if (k != root)
{
LL g = kk /w[k];
int p = upper_bound(a+1, a+m, g) - a-1;
ans += getsum(p);
}
int p = lower_bound(a + 1, a + m, w[k]) - a;
add(p, 1);
for (int i = s[k];~i; i=nt[i]) dfs(e[i]);
add(p, -1);
}
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d%lld", &n, &kk);
memset(s, -1, sizeof s);
memset(vis, 0, sizeof vis);
memset(x, 0, sizeof x);
m = 1, cnt=1;
for (int i = 1; i <= n; i++) scanf("%d", &w[i]), a[m++] = w[i];
sort(a + 1, a + m);
m = unique(a + 1, a + m) - a;
for (int i = 0; i < n - 1; i++)
{
scanf("%d%d", &u, &v);
vis[v]++;
nt[cnt] = s[u], s[u] = cnt, e[cnt++] = v;
}
for (int i = 1; i <= n; i++)
if (!vis[i]) root = i;
ans = 0;
dfs(root);
printf("%lld\n", ans);
}
return 0;
}
线段树启发式合并:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int t, n, x, y;
LL m, l, r, a[100009];
int fa[100009], vis[100009];
int s[100009], sum[35 * 100009], L[35 * 100009], R[35 * 100009], tot;
void make(int &k, LL l, LL r, LL p)
{
L[tot] = R[tot] = 0, sum[tot] = 1, k = tot++;
if (l == r) return;
LL mid = l + r >> 1;
if (p <= mid) make(L[k], l, mid, p);
else make(R[k], mid + 1, r, p);
}
int query(int k, LL l, LL r, LL val)
{
if (l == r || !k) return sum[k];
LL mid = l + r >> 1;
if (val <= mid) return query(L[k], l, mid, val);
return sum[L[k]] + query(R[k], mid + 1, r, val);
}
void merge(int &now, int pre, LL l, LL r)
{
if (!now || !pre) { now = now^pre; return; }
sum[now] += sum[pre];
if (l == r) return;
LL mid = l + r >> 1;
merge(L[now], L[pre], l, mid);
merge(R[now], R[pre], mid + 1, r);
}
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d%lld", &n, &m);
l = 1e9, r = 0, tot = 1;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
l = min(l, a[i]); r = max(r, a[i]);
}
for (int i = 1; i<n; i++)
{
scanf("%d%d", &x, &y);
vis[x]++; fa[y] = x;
}
queue<int> q;
LL ans = 0;
for (int i = 1; i <= n; i++)
{
make(s[i], l, r, a[i]);
if (!vis[i]) q.push(i);
if (1LL * a[i] * a[i] <= m) ans--;
}
while (!q.empty())
{
int pre = q.front();
q.pop();
ans += query(s[pre], l, r, m / a[pre]);
merge(s[fa[pre]], s[pre], l, r);
if (!--vis[fa[pre]]) q.push(fa[pre]);
}
printf("%lld\n", ans);
}
return 0;
}