Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

 

B. Alyona and a tree

题目连接:

http://codeforces.com/contest/739/problem/B

Description

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.

Sample Input

5
2 5 1 4 6
1 7
1 1
3 5
3 6

Sample Output

1 0 1 0 0

Hint

题意

给你一棵树,带有边权,然后u被v统治的前提是,u在v的子树里面,且dis(u,v)<=a[u]

现在问你每个点,统治多少个点。

题解:

考虑每个点x,如果他的祖先p,deep[x]-a[x]<=deep[p]的话,那么就会被+1。

我们按照dfs的顺序去更新的话,显然就是每次使得一条链区间加1。

所以你树链剖分,或者用前缀和去维护,就好了。

#include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>

using namespace std;

int n;

long long a[200005];
long long dep[200005];

vector< pair<int ,long long> >e[200005];
vector< pair<long long,int > >path;

int ans[200005];

void dfs(int u)
{
    long long ts=dep[u]-a[u];
    path.push_back(make_pair(dep[u],u));
    int id=lower_bound(path.begin(),path.end(),make_pair(ts,-1))-path.begin()-1;
    if(id>=0)
    {
        ans[path[id].second]--;
    }

    for(int i=0;i<e[u].size();i++)
    {
        pair<int ,long long> v=e[u][i];
        dep[v.first]=dep[u]+v.second;
        dfs(v.first);
        ans[u]+=(1+ans[v.first]);
    }
    path.pop_back();
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<=200000;i++)
        {
            e[i].clear();
            dep[i]=0;
            ans[i]=0;
        }


        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }

        for(int i=1;i<=(n-1);i++)
        {
            int p;long long w;
            scanf("%d%lld",&p,&w);
            e[p].push_back(make_pair(i+1,w));
        }

        dfs(1);

        for(int i=1;i<=n;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n");
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值