Codeforces 740D dfs+二分

来源:互联网 发布:淘宝怎样取消退款 编辑:程序博客网 时间:2024/05/21 10:40

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 thatv 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 ≤ 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
52 5 1 4 61 71 13 53 6
output
1 0 1 0 0
input
59 7 8 6 51 12 13 14 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).



题意:对于两个点  如果dis(u,v)<=val(v) 且v是u的子孙  则称u掌控v  现在求每个结点掌控点的个数


题解:从1结点开始dfs  开一个栈  保存dis和lab

所以dfs到任意一个结点时  因为栈里面的dis是单调递增的  所以二分找出来第一个能掌控的点  然后这个点到当前点的父亲的掌控数都+1

现在具体说一下二分

假设现在的树只是一条线  当前dfs到v  栈里面存的就是

dis(1,1),1   dis(1,2),2  dis(1,3),4  dis(1,4),4 ..... dis(1,v),v

所以要使得u能掌控v

 val(v)>=dis(u,v)  dis(u,v)=dis(1,v)-dis(1,u)

所以只要用lowerbound找dis(1,v)-val(v)的第一个位置即可

更新的时候也不需要用线段树更新  在开始点的标记+1  在当前点的标记-1  然后从下往上travel即可


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;ll val[200005],head[200005],cnt=0,ans[200005],num=0,cha[200005];struct point{ll num,lab;bool operator <(const point& a)const{return num<a.num;}};point q[200005];struct node{ll to,nex,cap;}edge[200005];void add(ll u,ll v,ll w){edge[cnt].to=v;edge[cnt].cap=w;edge[cnt].nex=head[u];head[u]=cnt++;}void dfs(ll t,ll ss){ll dd=lower_bound(q+1,q+1+num,(point){ss-val[t],1})-q;if(dd<=num){cha[t]++;cha[q[dd].lab]--;}q[++num]=(point){ss,t};ll i;for(i=head[t];~i;i=edge[i].nex){ll v=edge[i].to;dfs(v,ss+edge[i].cap);num--;}}ll travel(ll t){if(!(~head[t]))return cha[t];for(ll i=head[t];~i;i=edge[i].nex){ans[t]+=travel(edge[i].to);}return ans[t]+cha[t];}int main(){int n,i,j,x,y;scanf("%lld",&n);memset(head,-1,sizeof(head));for(i=1;i<=n;i++)scanf("%lld",&val[i]);for(i=2;i<=n;i++){scanf("%lld%lld",&x,&y);add(x,i,y);}dfs(1,0);travel(1);for(i=1;i<=n;i++)printf("%lld ",ans[i]);printf("\n");return 0;}


0 0
原创粉丝点击