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. C# 中访问修饰符

    1.public 完全公开的,公共的 2. private 私有的,只能在当前类的内部访问, 不可修饰类 3.protected 受保护的,只能在当前类的内部以及其子类中访问,不能用来修饰类 4.in ...

  2. 文本分类-TextCNN

    简介 TextCNN模型是由 Yoon Kim提出的Convolutional Naural Networks for Sentence Classification一文中提出的使用卷积神经网络来处理 ...

  3. Python3实现机器学习经典算法(四)C4.5决策树

    一.C4.5决策树概述 C4.5决策树是ID3决策树的改进算法,它解决了ID3决策树无法处理连续型数据的问题以及ID3决策树在使用信息增益划分数据集的时候倾向于选择属性分支更多的属性的问题.它的大部分 ...

  4. day-14 回归中的相关系数和决定系数概念及Python实现

    衡量一个回归模型常用的两个参数:皮尔逊相关系数和R平方 一.皮尔逊相关系数 在统计学中,皮尔逊相关系数( Pearson correlation coefficient),又称皮尔逊积矩相关系数(Pe ...

  5. Python3 小工具-ARP欺骗

    在kali中使用 from scapy.all import * import optparse import os def send(pkt,interface): for p in pkt: se ...

  6. JAVA集合类(大公司面试喜欢问的)

     分类: 核心JAVA(11)  版权声明:本文为博主原创文章,未经博主允许不得转载. 看了一些所谓大公司的Java面试问题,发现对于JAVA集合类的使用都比较看重似的,而自己在这方面还真的是所真甚少 ...

  7. 十二:NodeManager

    NM负责启动和管理节点上的containers.AM通过containers来运行任务. Health Checker Service 创建检查服务     NM运行一个检查服务来检查节点的状态,该服 ...

  8. 【转】Hbuilder MUI 页面刷新及页面传值问题

    文章来源:http://www.111cn.net/sys/CentOS/67213.htm 一.页面刷新问题 1.父页面A跳转到子页面B,B页面修改数据后再跳回A页面,刷新A页面数据 (1).父页面 ...

  9. 硬件PCB Layout布局布线Checklist检查表(通用版)

    按部位分类 技术规范内容 1 PCB布线与布局 PCB布线与布局隔离准则:强弱电流隔离.大小电压隔离,高低频率隔离.输入输出隔离.数字模拟隔离.输入输出隔离,分界标准为相差一个数量级.隔离方法包括:空 ...

  10. iOS- UIScrollView、UIPageControl分页浏览图片

    1.先介绍下UIScrollView的常见属性 @property(nonatomic) CGPoint contentOffset; // 记录UIScrollView滚动的位置 @property ...