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

题意:

给你一棵树,n个点, 以1为根,每个点有一个值,每条边有边权

我们规定假如u的子树中存在一个点v, 使得dist(u,v)<=a[v], dist(u,v)表示u 到 v的路径上的边权之和, a[v] 是v的点权

那么我们称u控制v

现在需要你算出每个点各控制了几个点

题解:

很显然被想到倍增统计路径和,然后你可以找到最远的那个被控制的点,然后该点到控制点的路径上全部加一,树上差分一下就行了

但是你A不了XJOI的,毕竟人家卡内存,所以还是去学所谓的树上二分吧,搬原题然后只改内存,这种素质我是第一次见。

代码如下:

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pii pair<int,int>
#define mp make_pair
#define int long long
using namespace std; int n,a[],fa[][],dis[][],d[],ans[];
vector<pii> g[]; void dfs(int now,int f,int dist)
{
fa[][now]=f;
dis[][now]=dist;
for(int i=;i<=;i++)
{
fa[i][now]=fa[i-][fa[i-][now]];
}
for(int i=;i<=;i++)
{
dis[i][now]=dis[i-][now]+dis[i-][fa[i-][now]];
}
for(int i=;i<g[now].size();i++)
{
if(g[now][i].first==f) continue;
dfs(g[now][i].first,now,g[now][i].second);
}
} int find(int x,int val)
{
int pos=x,nowd=;
for(int i=;i>=;i--)
{
if(dis[i][pos]+nowd<=val&&fa[i][pos])
{
nowd+=dis[i][pos];
pos=fa[i][pos];
}
}
return pos;
} void dfs2(int now,int f)
{
int pos=find(now,a[now]);
d[fa[][pos]]--;
d[fa[][now]]++;
for(int i=;i<g[now].size();i++)
{
if(g[now][i].first==f) continue;
dfs2(g[now][i].first,now);
}
} void dfs3(int now,int f)
{
ans[now]=d[now];
for(int i=;i<g[now].size();i++)
{
if(f==g[now][i].first) continue;
dfs3(g[now][i].first,now);
ans[now]+=ans[g[now][i].first];
}
} signed main()
{
scanf("%lld",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
}
for(int i=;i<=n;i++)
{
int to,cost;
scanf("%lld%lld",&to,&cost);
g[to].push_back(mp(i,cost));
g[i].push_back(mp(to,cost));
}
dfs(,,);
dfs2(,);
dfs3(,);
for(int i=;i<=n;i++)
{
printf("%lld ",ans[i]);
}
}

最新文章

  1. 【腾讯优测干货分享】如何降低App的待机内存(四)——进阶:内存原理
  2. 11月8日下午Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格
  3. js图文讲解
  4. 菜鸟日记-HTML-表格与表单
  5. git泄漏原理
  6. Linux Ubuntu download
  7. 【Hibernate】无外键多表查询
  8. EasyUI开发的驾校管理系统
  9. 51NOD 1222&#160;最小公倍数计数 [莫比乌斯反演 杜教筛]
  10. MySQL之pymysql模块
  11. Existing lock /var/run/yum.pid: another copy is running as pid 解决办法
  12. Activiti流程变量五步曲 ——by fightingKing
  13. 《机器学习实战》之一:knn(python代码)
  14. 七、文件IO——I/O处理方式和文件锁
  15. 为什么SQL用UPDATE语句更新时更新行数会多3行有触发器有触发器有触发器有触发器有触发器有触发器
  16. js 学习记录(一)
  17. asp.net学习——Response对象
  18. 简单的SpringWebFlow例子及遇到的问题
  19. mysql 查询锁表
  20. RPC和REST

热门文章

  1. Python+Selenium爬虫实战一《将QQ今日话题发布到个人博客》
  2. POI-Excel表格导入和导出
  3. fileinput模块可以循环一个或多个文本文件的内容
  4. mysql彻底删除
  5. SYN Flood 防范
  6. percona xtradb cluster test
  7. GameObject.Active
  8. centOS系统安装MySQL教程
  9. 虚拟机上linux与windows之间复制粘贴
  10. ssh 连接缓慢解决方法