2 seconds
256 megabytes
standard input
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.
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.
Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.
5
2 5 1 4 6
1 7
1 1
3 5
3 6
1 0 1 0 0
5
9 7 8 6 5
1 1
2 1
3 1
4 1
4 3 2 1 0
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序 将树转换为区间
利用树状数组计算每次查询 树状数组中存的是点的位置
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll n;
ll a[];
ll d[];
ll v[];
ll nedge=;
ll pre[];
ll in[];
ll out[];
ll tree[];
ll re[];
struct node
{
ll to,pre;
ll we;
}N[]; struct xx
{
ll w,pos;
}M[];
bool cmp1(struct xx aa,struct xx bb)
{
return aa.w<bb.w;
}
struct yy
{
ll l,r;
ll pos;
ll we;
}S[];
bool cmp2 (struct yy aa,struct yy bb)
{
return aa.we<bb.we;
}
void add1(ll from,ll to,ll w)
{
nedge++;
N[nedge].we=w;
N[nedge].to=to;
N[nedge].pre=pre[from];
pre[from]=nedge;
}
ll dfn=;
ll jishu=;
void getdfs(ll root,ll sum)
{
in[root]=++dfn;
d[root]=sum;
M[jishu].w=d[root]-a[root];
M[jishu].pos=dfn;
jishu++;
for(ll i=pre[root];i;i=N[i].pre)
{
sum+=N[i].we;
getdfs(N[i].to,sum);
sum-=N[i].we;
}
out[root]=dfn;
}
ll lowbit(ll xx)
{
return xx&(-xx);
}
void add2 (ll x,ll y)
{
for(ll i=x;i<=n;i+=lowbit(i))
tree[i]+=y;
}
ll getsum (ll x)
{
ll ans=;
for(ll i=x;i>=;i-=lowbit(i))
ans+=tree[i];
return ans;
}
int main()
{
memset(pre,,sizeof(pre));
scanf("%I64d",&n);
for(ll i=;i<=n;i++)
scanf("%I64d",&a[i]);
ll exm1,exm2;
for(ll i=;i<=n-;i++)
{
scanf("%I64d %I64d",&exm1,&exm2);
add1(exm1,i+,exm2);
}
getdfs(,);
for(ll i=;i<=n;i++)
{
S[i].l=in[i]+;
S[i].r=out[i];
S[i].pos=i;
S[i].we=d[i];
}
sort(M,M+jishu,cmp1);
sort(S+,S++n,cmp2);
ll start=;
for(ll i=;i<=n;i++)
{
while(start<jishu&&M[start].w<=S[i].we)
{
add2(M[start].pos,);
start++;
}
re[S[i].pos]=getsum(S[i].r)-getsum(S[i].l-);
}
for(ll i=;i<=n;i++)
printf("%I64d ",re[i]);
printf("\n");
return ;
}