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 ≤ 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 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 ;
}

Codeforces Round #381 (Div. 2) D dfs序+树状数组的更多相关文章

  1. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  2. 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)

    [链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...

  3. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  4. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  5. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  6. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  7. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  8. 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组

    题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

  9. [BZOJ1103][POI2007]大都市meg dfs序+树状数组

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

随机推荐

  1. Linux搭建mysql、apache、php服务总结

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2018 年 04 月 22 日 原地址:https://niaobulashi.com/archives/linux-mysql-apache- ...

  2. (原)HUD绘画贴图解析

    @小道:临时存放       1\主过程 说明: a\调用DrawTextureSimple时,会将UTexure封装成CavarsItem, 若是正交投射函数执行双,最后CavarsItem.Dra ...

  3. CsvHelper文档-2读

    CsvHelper文档-2读 这个库默认不需要做任何设置就可以很容易的使用它.如果你的类属性名称直接匹配csv的标题名称,那么可以按照下面的实例来用: (以下所有的代码都需要引用using csvhe ...

  4. LeetCode 94 ——二叉树的中序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...

  5. joomla 出现The file Cache Storage is not supported on this platform;

    错误提示:The file Cache Storage is not supported on this platform:在这个平台上不支持文件缓存存储 出现这样的原因很简单,有两个文件夹不可写,这 ...

  6. FromHandle临时对象一探究竟

    我们在调用CWnd::GetDlgItem()函数时,MSDN告诉我们:The returned pointer may be temporary and should not be stored f ...

  7. iOS开发UUIView动画方法总结

    #动画设置 UIView动画实现 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *myView; @ ...

  8. alpha阶段个人总结(201521123031林庭亦)

    一.个人总结 第一部分:硬的问题 第二部分:软的问题,在成长路上学到了什么? 1 当你看到不靠谱的设计.糟糕的代码.过时的文档和测试用例的时候,不要想 "既然别人的代码已经这样了,我的代码也 ...

  9. IE BHO的IObjectWithSite接口

    Internet Explorer的BHO的对象必须实现IObjectWithSite接口.该接口是IE用来对插件进行管理和通讯的一个接口,其有SetSite和GetSite两个方法,当IE加载和卸载 ...

  10. C# 中常用的索引器

    使用 C# 中的索引器和 JavaScript 中访问对象的属性是很相似. 之前了解过索引器,当时还把索引器和属性给记混了, 以为索引器就是属性,下面写下索引器和属性的区别,以及怎么使用索引器 先说明 ...