D. 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 that vcontrols 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).

 题意:给你一棵树 有点权和边权  对于每个结点 若从当前点i到其子树中点j的边权之和小于等于j点权则表示i可以控制j点 计算每个结点能控制的点的个数

 题解:div(i,j)<=a[j]     

         d[j]-d[i]<=a[j]  d[j]代表j点到root的边权和

         d[j]-a[j]<=d[i]

预处理出每个点 M[j].w=d[j]-a[j]

转化为i个子树中M[j].w<=d[i] 的点的个数

利用dfs序 将树转换为区间

利用树状数组计算每次查询  树状数组中存的是点的位置

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <stack>
  7 #include <queue>
  8 #include <cmath>
  9 #include <map>
 10 #define ll  __int64
 11 #define mod 1000000007
 12 #define dazhi 2147483647
 13 using namespace  std;
 14 ll n;
 15 ll a[200005];
 16 ll d[200005];
 17 ll v[200005];
 18 ll nedge=0;
 19 ll pre[200005];
 20 ll in[200005];
 21 ll out[200005];
 22 ll tree[200005];
 23 ll re[200005];
 24 struct node
 25 {
 26     ll to,pre;
 27     ll we;
 28 }N[200005];
 29 
 30 struct xx
 31 {
 32     ll w,pos;
 33 }M[200005];
 34 bool cmp1(struct  xx aa,struct xx bb)
 35 {
 36     return aa.w<bb.w;
 37 }
 38 struct yy
 39 {
 40     ll  l,r;
 41     ll  pos;
 42     ll  we;
 43 }S[200005];
 44 bool  cmp2 (struct yy aa,struct yy bb)
 45 {
 46     return aa.we<bb.we;
 47 }
 48 void add1(ll from,ll to,ll w)
 49 {
 50     nedge++;
 51     N[nedge].we=w;
 52     N[nedge].to=to;
 53     N[nedge].pre=pre[from];
 54     pre[from]=nedge;
 55 }
 56 ll dfn=0;
 57 ll jishu=0;
 58 void getdfs(ll root,ll sum)
 59 {
 60     in[root]=++dfn;
 61     d[root]=sum;
 62     M[jishu].w=d[root]-a[root];
 63     M[jishu].pos=dfn;
 64     jishu++;
 65     for(ll i=pre[root];i;i=N[i].pre)
 66     {
 67         sum+=N[i].we;
 68         getdfs(N[i].to,sum);
 69         sum-=N[i].we;
 70     }
 71     out[root]=dfn;
 72 }
 73 ll lowbit(ll xx)
 74 {
 75     return xx&(-xx);
 76 }
 77 void add2 (ll x,ll y)
 78 {
 79     for(ll i=x;i<=n;i+=lowbit(i))
 80         tree[i]+=y;
 81 }
 82 ll getsum (ll x)
 83 {
 84     ll ans=0;
 85     for(ll i=x;i>=1;i-=lowbit(i))
 86         ans+=tree[i];
 87     return ans;
 88 }
 89 int main()
 90 {
 91     memset(pre,0,sizeof(pre));
 92     scanf("%I64d",&n);
 93     for(ll i=1;i<=n;i++)
 94         scanf("%I64d",&a[i]);
 95     ll exm1,exm2;
 96     for(ll i=1;i<=n-1;i++)
 97     {
 98         scanf("%I64d %I64d",&exm1,&exm2);
 99         add1(exm1,i+1,exm2);
100     }
101     getdfs(1,0);
102     for(ll i=1;i<=n;i++)
103     {
104         S[i].l=in[i]+1;
105         S[i].r=out[i];
106         S[i].pos=i;
107         S[i].we=d[i];
108     }
109     sort(M,M+jishu,cmp1);
110     sort(S+1,S+1+n,cmp2);
111     ll start=0;
112     for(ll i=1;i<=n;i++)
113     {
114         while(start<jishu&&M[start].w<=S[i].we)
115         {
116             add2(M[start].pos,1);
117             start++;
118         }
119         re[S[i].pos]=getsum(S[i].r)-getsum(S[i].l-1);
120     }
121     for(ll i=1;i<=n;i++)
122         printf("%I64d ",re[i]);
123     printf("\n");
124     return 0;
125 }