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 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).

【分析】题意就不说了,说下怎么做。很显然暴力肯定超时。对于每一个节点,用倍增的思想,fa[i][x]、cost[i][x]数组分别记录x节点往上第2的i次方个祖先编号和与那个祖先的距离。如果节点x对节点f有一个贡献,那么x对处于x和f之间的节点也应该有一个贡献,所以用前缀数组的思想,ans[x]++,ans[f]--,然后只需要在dfs的时候一个一个累加就行了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e5+;
int n,m,k,u;
int fa[][N];
ll cost[][N];
ll a[N],ans[N];
struct man{
int son;
ll co;
};
vector<man>edg[N];
void dfs(int x){
for(int i=;fa[i-][fa[i-][x]];i++){
fa[i][x]=fa[i-][fa[i-][x]];
cost[i][x]=cost[i-][x]+cost[i-][fa[i-][x]];
}
for(int i=;i<edg[x].size();i++){
man e=edg[x][i];
int v=e.son;ll c=e.co;
fa[][v]=x;cost[][v]=c;
dfs(v);
}
}
int Find(int x){
ll c=a[x];
for(int i=;i>=;i--){
if(cost[i][x]<=c&&fa[i][x]){
c-=cost[i][x];
x=fa[i][x];
}
}
return x;
}
void sum_dfs(int x){
for(int i=;i<edg[x].size();i++){
man e=edg[x][i];
int v=e.son;ll c=e.co;
sum_dfs(v);
ans[x]+=ans[v];
}
}
int main (){
ll c;
met(ans,);
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%lld",&a[i]);
for(int i=;i<=n;i++){
scanf("%d%lld",&u,&c);
man s;s.co=c;s.son=i;
edg[u].push_back(s);
}
dfs();
for(int i=;i<=n;i++){
int f=Find(i);
ans[fa[][f]]--;
ans[fa[][i]]++;
}
sum_dfs();
for(int i=;i<=n;i++)printf("%lld ",ans[i]);printf("\n");
return ;
}

codeforces 381 D Alyona and a tree(倍增)(前缀数组)的更多相关文章

  1. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. codeforces 682C C. Alyona and the Tree(dfs)

    题目链接: C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input ...

  3. 【CodeForces - 682C】Alyona and the Tree(dfs)

    Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...

  4. 【Codeforces 682C】Alyona and the Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设 ...

  5. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  6. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  7. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

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

  8. Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)

    D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...

  9. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

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

随机推荐

  1. REST 架构风格

    目前基于网络应用的架构风格主要有三种: RPC架构风格   将服务器看作是由一些过程组成,客户端调用这些过程来执行特定的任务.SOAP就是RPC风格的一种架构.过程是动词性的(做某件事),因此RPC建 ...

  2. 12款最佳Linux命令行终端工具, 20款优秀的 Linux 终端仿真器

    12款最佳Linux命令行终端工具     如果你跟我一样,整天要花大量的时间使用Linux命令行,而且正在寻找一些可替代系统自带的老旧且乏味的终端软件,那你真是找对了文章.我这里搜集了一些非常有趣的 ...

  3. Java注释@Override

    @Override指定方法覆载.它可以强制一个子类必须覆盖父类的方法. package ch14; /** * Created by Jiqing on 2016/12/27. */ public c ...

  4. Delphi名站以及高手Blog

    以前知道的: http://cnblogs.com/del (万一兄的,这个不用解释了) http://www.cnblogs.com/del/archive/2010/04/25/1720750.h ...

  5. java.lang.ClassNotFoundException和java.lang.NoClassDefFoundError的区别

    java里生成对象有如下两种方式: 1: Object obj = new ClassName(); 直接new一个对象 2: Class clazz = Class.forName(ClassNam ...

  6. js 原生对象排序

    //对象属性排序 function compare(propertyName) { return function (object1, object2) { var value1 = object1[ ...

  7. 将Python脚本封装成exe可执行文件 转

    将Python脚本封装成exe可执行文件 http://www.cnblogs.com/renzo/archive/2012/01/01/2309260.html  cx_freeze是用来将 Pyt ...

  8. ASP.NET页面优化,提高载入速度[转]

      ASP.NET页面载入速度提高的一些做法: 1.采用 HTTP Module 控制页面的生命周期. 2.自定义Response.Filter得到输出流stream生成动态页面的静态内容(磁盘缓存) ...

  9. 20150207读书笔记<深入理解计算机系统2-1>

    第二章 信息存储 (1)  多数计算机以一个字节作为最小可寻址的存储器单元. 机器级程序将存储器看成一个非常大的字节数组,称为虚拟存储器. 存储器的每个字节都由唯一的数字标识,称为它的地址. 所有可能 ...

  10. iOS打开百度地图、高德地图导航

    1.判断手机里是否已经安装了百度地图或者高德地图: BOOL hasBaiduMap = NO; BOOL hasGaodeMap = NO; if ([[UIApplication sharedAp ...