code forces739B 树上差分

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<<" ";
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值