code forces739B 树上差分

本文介绍了一种结合树形动态规划与二分搜索的算法,用于解决一类特定问题:给定一棵带权树及各节点上的管辖距离范围,求每个节点能够直接或间接管辖的节点数量。文章详细讲解了算法思路,包括如何通过二分查找确定管辖范围内的最远节点,并利用差分技巧统计管辖节点数。

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

B. Alyona and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).

Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.

The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.

Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such thatv controls u.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.

The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).

It is guaranteed that the given graph is a tree.

Output

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Examples
input
5
2 5 1 4 6
1 7
1 1
3 5
3 6
output
1 0 1 0 0
input
5
9 7 8 6 5
1 1
2 1
3 1
4 1
output
4 3 2 1 0
Note

In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1controls the vertex 5).


给n个点和被管辖的距离范围,还有边权,问每个点能管辖多少个点

树上二分找到最后满足 管辖范围的值,然后差分一下就出来了

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;

struct node
{
    int v,w;
};

int sum[N];
int fa[N][21];
typedef long long ll;
ll dep[N];
int u[N];
int du[N];
vector<node> e[N];

void dfs(int x,int y,ll w)
{
    dep[x]=w;
    du[x]=1;
    fa[x][0]=y;
    for(int i=1;fa[fa[x][i-1]][i-1];i++)
        fa[x][i]=fa[fa[x][i-1]][i-1];
    int pos=x;
    for(int i=20;i>=0;i--)
        if(fa[pos][i]&&dep[x]-dep[fa[pos][i]]<=u[x]) pos=fa[pos][i];

    int gg=fa[pos][0];
    if(dep[x]-dep[gg]>u[x])
    du[gg]--;
    for(int i=0;i<e[x].size();i++)
    {
        int vv=e[x][i].v;
        if(vv==y) continue;
        dfs(vv,x,w+e[x][i].w);
        du[x]+=du[vv];
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&u[i]);
    memset(fa,0,sizeof(fa));
    for(int i=1;i<n;i++)
    {
        int x,w;
        scanf("%d %d",&x,&w);
        e[x].push_back((node){i+1,w});
    }
    dep[0]=0;
    dfs(1,0,0);
    for(int i=1;i<=n;i++)
        cout<<du[i]-1<<" ";
}


### Codeforces Problem 1130C 解析 用户提到的是 **Codeforces Problem 742B** 的相关内容,而问题是希望找到关于 **Problem 1130C** 的解答或解释。以下是针对 **Problem 1130C** 的解析。 #### 题目概述 在 **Codeforces Problem 1130C (Array Beauty)** 中,给定一个数组 `a` 和整数 `k`,定义子序列的美丽值为该子序列中的最小差值。目标是从数组中选取长度至少为 `k` 的子序列,使得其美丽值最大化,并返回这个最大化的美丽值。 --- #### 关键概念与算法思路 为了求解此问题,可以采用二分法结合滑动窗口技术来高效解决问题: 1. **二分搜索范围**: 子序列的美丽值可能的最大值是数组中相邻两个元素之间的最小差值,因此可以通过二分搜索的方式,在 `[0, max_diff]` 范围内寻找满足条件的最大美丽值[^5]。 2. **验证函数设计**: 对于每一个候选美丽值 `mid`,通过滑动窗口检查是否存在一个子序列,其中任意两元素之差均不大于 `mid` 并且长度不小于 `k`。如果存在,则说明当前美丽值可行;否则不可行[^6]。 3. **实现细节**: - 使用双指针维护滑动窗口。 - 记录窗口内的元素数量以及它们之间是否满足美丽值约束。 --- #### 实现代码 以下是一个基于 Python 的解决方案: ```python def can_form_subsequence(a, k, mid): count = 0 last = float('-inf') for num in a: if num >= last + mid: count += 1 last = num if count >= k: return True return False def array_beauty(n, k, a): low, high = 0, max(a) - min(a) result = 0 while low <= high: mid = (low + high) // 2 if can_form_subsequence(sorted(a), k, mid): result = mid low = mid + 1 else: high = mid - 1 return result # 输入处理 n, k = map(int, input().split()) a = list(map(int, input().split())) print(array_beauty(n, k, a)) ``` 上述代码实现了二分查找逻辑并配合辅助函数完成验证操作[^7]。 --- #### 测试样例分析 对于输入数据: ``` Input: 5 3 1 3 2 4 5 Output: 2 ``` 程序会按照如下流程执行: - 排序后的数组为 `[1, 2, 3, 4, 5]`。 - 初始二分区间为 `[0, 4]`。 - 经过多次迭代最终得出结果为 `2`,即最长符合条件的子序列美丽值。 --- #### 时间复杂度与空间复杂度 - **时间复杂度**: O(N log M),其中 N 是数组大小,M 是数组中最大值减去最小值的结果。 - **空间复杂度**: O(1),除了存储原始数组外无需额外的空间开销[^8]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值