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]);
}
}

XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)的更多相关文章

  1. Codeforces 739B Alyona and a tree(树上路径倍增及差分)

    题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...

  2. CodeForces 739B Alyona and a tree (二分+树上差分)

    <题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...

  3. XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)

    Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...

  4. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

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

  7. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  8. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  9. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

随机推荐

  1. 再次理解HTTP GET协议

    概述: 在上学的时候,以及在工作的这几年中,我一直错误了理解HTTP GET. 以前我的认知中认为GET/POST的区别在于: 1.GET长度限制 2.GET和POST的请求方式不一样(之前所理解的G ...

  2. Linux HDD information (SATA/SCSI/SAS/SSD)

    举例一: [reistlin@reistlin.com ~]$ cat /proc/scsi/scsi | grep Model Vendor: ATA Model: OCZ-VERTEX2 3.5 ...

  3. IDEA debug

    版权声明: 本文转自:https://blog.csdn.net/qq_27093465/article/details/64124330 1,rerun XXX,这个就是直接重新跑某个程序.2,这个 ...

  4. java中执行子类的构造方法时,会不会先执行父类的构造方法

    会,在创建子类的对象时,jvm会首先执行父类的构造方法,然后再执行子类的构造方法,如果是多级继承,会先执行最顶级父类的构造方法,然后依次执行各级个子类的构造方法.

  5. 什么是web资源????

    所谓 web 资源即放在 Internet 网上供外界访问的文件或程序,又根据它们呈现的效果及原理不同,将它们划分为静态资源和动态资源. 1. 什么是静态资源 静态资源是浏览器能够直接打开的,一个 j ...

  6. C++中float类型的存储

    C++中float用32位来表示,f = (-1)^S * T * 2^E,S是符号位,T是尾数,E是指数 首先我们把f表示成科学计数法的形式,然后再写出其在内存中的表示,在这里T写成1.XXX的形式 ...

  7. Scala基础:面向对象之trait

    trait类似于java中的interface,但是有所不同 Scala中的trait是一种特殊的概念: 首先先将trait作为接口使用,此时的trait就与Java中的接口 (interface)非 ...

  8. KVC的底层实现原理

    KVC是OC特有的,本质是在运行时动态的给对象发送setValue:forKey 消息,设置数值 -调用super.init 保证对象已经被创建完成 .当给对象发送setValue:forKey 消息 ...

  9. opencv3.4 win10 visual studio2017 opencv_contrib 编译

    found Intel IPP (ICV version): 2017.0.3 [2017.0.3] at: D:/opencv/opencv_3_4_0/opencv/my_build/3rdpar ...

  10. OceanBase

    OceanBase 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! OceanBase是一个支持海量数据的高性能分布式数据库系统,实现了 数千亿条记录.数百TB数据上的 ...