CodeForces

来源:互联网 发布:北师大网络教育学费 编辑:程序博客网 时间:2024/05/09 14:27

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.

Example
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 1 controls the vertex 5).

昨天交了一天一直wa63我也不知道为什么一直在改
一直不过
今天上来随便删了一个daan!=ds
过了?
我感觉不对啊我特判的那个条件这句话可有可无
然后我干脆交了昨天wa63代码
我NMB的过了?!

思路是这样的首先建树
然后每个点向根部伸展看能是谁的控制点
是谁的谁+1
这是暴力的想法
但是这种做法复杂度n^2这个肯定过不了
然后用前缀和+二分来优化,然后树上差分
我还是第一次在树上前缀和和二分
学会了。。
因为搜索的时候每次都是一条链
只要在退出该子树的时候把信息更新回父节点就可以
学会了新的操作

要在l到r范围内区间全部加1
只要a[l]++,a[r+1]–
然后求谁的时候操作他的前缀和就可以。。
哇真鸡儿6

#include<iostream>#include<cstring>#include<string>#include<vector>#include<cstdio>#include<algorithm>using namespace std;vector<long long>tu[1200001], bian[1200001];long long n, q,w,qqz[1200001],ds=0;long long zanshi[1200001],qiuhe[1200000];long long zhi[1200001],zuihou[1200001];void dfs(long long gen,long long die,long long chang){    zanshi[++ds] = gen;    long long z = 1, y =ds-1;    qqz[ds] = qqz[ds - 1] + chang;    long long daan = ds;    while (z <= y)    {        long long mid = (z + y) / 2;        if (qqz[ds]-qqz[mid] <= zhi[gen])        {            daan = min(daan, mid);            y = mid - 1;        }        else z = mid + 1;    }    if (daan != ds)qiuhe[ds - 1]++,qiuhe[daan - 1]--;    for (long long a = 0; a < tu[gen].size(); a++)    {        if (die == tu[gen][a])continue;        dfs(tu[gen][a], gen, bian[gen][a]);    }    //  if (daan != ds)    zuihou[gen] = qiuhe[ds];    qiuhe[ds - 1] += qiuhe[ds];    zanshi[ds] = qiuhe[ds] = qqz[ds] = 0;    ds--;}int main(){    cin >> n;    memset(zanshi, 0, sizeof(zanshi));    for (long long a = 1; a <= n; a++)scanf("%I64d", &zhi[a]);    for (long long a = 2; a <= n; a++)    {        scanf("%I64d%I64d", &q, &w);        tu[q].push_back(a);        bian[q].push_back(w);        tu[a].push_back(q);        bian[a].push_back(w);    }    dfs(1, 0, 0);    for (long long a = 1; a <= n; a++)printf("%I64d ", zuihou[a]);}