Codeforces Round #381 (Div. 2) D. Alyona and a tree

D. Alyona and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes

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.

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


思路:

1、dfs 求出每一个点距离树根的距离 dis[N],用时间戳 in[N],out[N]数组记录进出点的时间并将子节点的时间戳包含在父亲节点的时间戳之内;

2、对于每一个点,值为val[N],处理出 node(1,i,dis[i]-val[i]) 与 node(2,i,dis[i]); 两组数据,并按第3个值升序排序;

3、再维护一个树状数组更新、求和即可。


#include <queue>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <assert.h>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a));
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define mod 1000000007
#define fre  freopen("input.txt","r",stdin)
#define fot  freopen("out.txt","r",stdout)
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
using namespace std;
#define N 200005
#define M 15
int n;
int in[N],out[N];
struct edge
{
   int v,next,dis;
}e[N];
int val[N];
LL  dis[N];
int head[N];
int ans[N];
struct node
{
   int id,num;
   LL length;
   node(){}
   node(int Id, int Num, LL Length){          //注意,这里是longlong  length
   id = Id , num = Num , length = Length;
   }
   bool friend operator<(node a,node b){
   if(a.length == b.length) return a.id < b.id;
   return a.length < b.length;
   }
}no[N<<1];
int cot;
void dfs(int x,LL len) 
{
   dis[x] = len;
   ++cot;
   in[x] = cot;
   for(int i=head[x]; ~i; i=e[i].next){
      int v = e[i].v;
      dfs(v, len+e[i].dis);
   }
   out[x]=cot;
}

int q[N];                                       //树状数组
inline int getsum(int x)
{
   int ans = 0;
   while(x>0){
      ans += q[x];
      x -= x & -x;
   }
   return ans;
}
inline void add(int x)
{
   while(x<=n){
      q[x]++;
      x += x & -x;
   }
}
int main()
{
   scan(n);
   REP(i,1,n+1) scan(val[i]);
   int cnt = 0;
   mst(head,-1);
   REPP(i,2,n){
   int a,b;
   scann(a,b);
   e[cnt].v = i;
   e[cnt].next = head[a];
   e[cnt].dis  = b;
   head[a] = cnt++;
   }
   dfs(1,0);
   cnt = 0;
   REP(i,1,n+1){
   no[cnt++]=node(1,i,dis[i]-val[i]);
   no[cnt++]=node(2,i,dis[i]);
   }
   sort(no,no+cnt);
   REP(i,0,cnt){ 
   if(no[i].id == 1){                              //  dis[i] - val[i]  用于对其他点进行更新,然而同时会使自己加一次,所以最后要 -1
     // cout<<i<<" "<<no[i].num<<" "<<in[no[i].num]<<endl;
      add(in[no[i].num]);
   }else{
   ans[no[i].num] = getsum(out[no[i].num]) - getsum(in[no[i].num]-1);
   }
   }
   REP(i,1,n+1)  printf("%d ",ans[i] - 1);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值