【CF8389】Alyona and a tree 树上差分 + 二分

题目链接


链接

概述

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 that v 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 ≤ n, 1 ≤ 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.

分析

树上差分和普通的查分思想其实基本相同,我们可以结合LCA算法统计对给定的多条路径,每一个节点经过的次数。当然,这题没有这么复杂,在dfs的过程中,每一条路径u-v都满足u是v的祖先,我们对于dfs中的每一条路径进行统计,用二分法找出对于路径路u,v上dis > a[u]的右边界,然后差分,最后dfs统计答案即可,时间复杂度O(nlogn),对于给定的数据范围是没有问题的

代码

//CF739B
#include <cstdio>
#include <cctype>
#include <cstring>

typedef long long ll;
inline int read(){
    int x = 0, op = 1; char ch = getchar();
    while (!isdigit(ch)){
        if (ch == '-') op = -1; ch = getchar();}
    while (isdigit(ch)){
        x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
    return x * op;
}

void write(int x){
    if (x < 0) putchar('-'), x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

const int N = 2e5 + 10;
int n, cnt;
int head[N], sum[N], diff[N];
ll d[N], path[N], a[N];
struct edge{
    int to, nxt, val;
}e[N];

void add(int u, int v, int w){
    e[++cnt] = {v, head[u], w};
    head[u] = cnt;
}

void dfs(int u, int id, ll dis){
    d[id] = dis;
    path[id] = u;
    int l = 1, r = id;
    while (l <= r){
        int mid = (l + r) / 2;
        if (a[u] >= dis - d[mid]) r = mid - 1;
        else l = mid + 1;
    }
    diff[path[r]]--;
    diff[path[id - 1]]++;
    for (int i = head[u]; ~i; i = e[i].nxt)
        dfs(e[i].to, id + 1,dis + e[i].val);
}


void dfs_ans(int u){
    sum[u] = diff[u];
    for (int i = head[u]; ~i; i = e[i].nxt) {
        int v = e[i].to;
        dfs_ans(v);
        sum[u] += sum[v];
    }
}

int main() {
    memset(head, -1, sizeof head);
    n = read();
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 2, fa, w; i <= n; ++i) {
        fa = read(), w = read();
        add(fa, i, w);
    }
    dfs(1, 1, 0);
    dfs_ans(1);

    for (int i = 1; i <= n; ++i)
        write(sum[i]), putchar(' ');
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值