HDU 5877 Weak Pair

本文探讨了在一个带有根节点的树形结构中寻找弱对的数量。通过使用数据结构如线段树和平衡树,文章提供了一种有效的解决方案,并详细介绍了实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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×avk.

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: 
  
  1N105 
  
  0ai109 
  
  0k1018
 

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
一道数据结构好题,从不同的角度有不同的做法,
树上直接做可以自上向下统计每个点对于祖先的贡献,用平衡树或动态线段树可以搞定,离散后可以树状数组或线段树。
自下向上可以用线段树或平衡树的启发式合并,还可以树形转线形,询问区间里小于某个值的数有几个,可以线段树加二分,
或者可持久化线段树等等。这里我用了最近学会的线段树启发式合并自下向上用拓扑排序搞定。
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 998244353;
const int N = 1e5 + 10;
int T, n, x, y, l, r;
LL m;
int fa[N], cnt[N], a[N];
int f[35 * N], L[35 * N], R[35 * N], g[N], tot;

int node()
{
	L[tot] = R[tot] = f[tot] = 0; return tot++;
}

void make(int &x, int l, int r, int u)
{
	if (!x) x = node();
	f[x] = 1;
	if (l == r) return;
	int mid = l + r >> 1;
	if (u <= mid) make(L[x], l, mid, u);
	else make(R[x], mid + 1, r, u);
}

int find(int x, int l, int r, LL u)
{
	if (!x || u < l) return 0;
	if (l == r) return f[x];
	int mid = l + r >> 1;
	if (u <= mid) return find(L[x], l, mid, u);
	return f[L[x]] + find(R[x], mid + 1, r, u);
}

void merge(int &x, int y, int l, int r)
{
	if (!x || !y) { x = x^y; return; }
	f[x] += f[y]; 
	if (l == r) return;
	int mid = l + r >> 1;
	merge(L[x], L[y], l, mid);
	merge(R[x], R[y], mid + 1, r);
}

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%lld", &n, &m);
		l = 1e9, r = 0, tot = 1;
		rep(i, 1, n)
		{
			scanf("%d", &a[i]); cnt[i] = 0;
			l = min(l, a[i]); r = max(r, a[i]);
		}
		rep(i, 1, n - 1)
		{
			scanf("%d%d", &x, &y);
			cnt[x]++;	fa[y] = x;
		}
		queue<int> p;
		LL ans = 0;
		rep(i, 1, n)
		{
			make(g[i] = 0, l, r, a[i]);
			if (!cnt[i]) p.push(i);
			if (1LL * a[i] * a[i] <= m) ans--;
		}
		while (!p.empty())
		{
			int q = p.front(); p.pop();
			ans += find(g[q], l, r, m / a[q]);
			merge(g[fa[q]], g[q], l, r);
			if (!--cnt[fa[q]]) p.push(fa[q]);
		}
		printf("%lld\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值