B. Alyona and a tree

题目连接:

http://codeforces.com/contest/739/problem/B

Description

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.

Sample Input

5

2 5 1 4 6

1 7

1 1

3 5

3 6

Sample Output

1 0 1 0 0

Hint

题意

给你一棵树,带有边权,然后u被v统治的前提是,u在v的子树里面,且dis(u,v)<=a[u]

现在问你每个点,统治多少个点。

题解:

考虑每个点x,如果他的祖先p,deep[x]-a[x]<=deep[p]的话,那么就会被+1。

我们按照dfs的顺序去更新的话,显然就是每次使得一条链区间加1。

所以你树链剖分,或者用前缀和去维护,就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7; vector<pair<int,int> >E[maxn];
vector<pair<long long,int> >path;
long long a[maxn],deep[maxn];
int ans[maxn],n; void dfs(int x){
ans[x]++;
int p=lower_bound(path.begin(),path.end(),make_pair(deep[x]-a[x],-1))-path.begin()-1;
if(p>=0)ans[path[p].second]--;
path.push_back(make_pair(deep[x],x));
for(auto& v:E[x]){
deep[v.first]=deep[x]+v.second;
dfs(v.first);
ans[x]+=ans[v.first];
}
path.pop_back();
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=2;i<=n;i++){
int p,w;
scanf("%d%d",&p,&w);
E[p].push_back(make_pair(i,w));
}
dfs(1);
for(int i=1;i<=n;i++)
cout<<ans[i]-1<<" ";
cout<<endl;
}

Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和的更多相关文章

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

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

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

  4. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题

    C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...

  6. Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Codeforces Round #358 (Div. 2) C. Alyona and the Tree

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #358 (Div. 2) A B C 水 水 dfs序+dp

    A. Alyona and Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...

随机推荐

  1. JavaScript-插入concat,splice,截取slice

    拼接和截取:concat 拼接: var newArr=arr.concat(值1,值2,值3,值4,.....); 将值1值2,以及arr2中每个元素一次拼接到arr1结尾,返回新数组 强调: 1. ...

  2. 斯坦福第四课:多变量线性回归(Linear Regression with Multiple Variables)

    4.1  多维特征 4.2  多变量梯度下降 4.3  梯度下降法实践 1-特征缩放 4.4  梯度下降法实践 2-学习率 4.5  特征和多项式回归 4.6  正规方程 4.7  正规方程及不可逆性 ...

  3. 2015年8月18日,杨学明老师《技术部门的绩效管理提升(研讨会)》在中国科学院下属机构CNNIC成功举办!

    2015年8月18日,杨学明老师为中国网络新闻办公室直属央企中国互联网络中心(CNNIC)提供了一天的<技术部门的绩效管理提升(研讨会)>培训课程.杨学明老师分别从研发绩效管理概述.研发绩 ...

  4. FIM相关报错汇总

    1.错误1:FIM在修改MA名字的时候报错: The management agent cannot be deleted or renamed because the working directo ...

  5. SQL Develop SSH远程连接

    目前碰到了本机无法直接连接数据库服务器,然后mac上可以选择的oracle客户端又很少,直到知道了可以ssh远程到跳板机上,然后进行连接. 在跳板机上输入命令如下: ssh -L 0.0.0.0:65 ...

  6. 《Linux内核设计与实现》读书笔记(十四)- 块I/O层

    最近太忙,居然过了2个月才更新第十四章.... 主要内容: 块设备简介 内核访问块设备的方法 内核I/O调度程序 1. 块设备简介 I/O设备主要有2类: 字符设备:只能顺序读写设备中的内容,比如 串 ...

  7. Python time datetime常用时间处理方法

    常用时间转换及处理函数: import datetime # 获取当前时间 d1 = datetime.datetime.now() print d1 # 当前时间加上半小时 d2 = d1 + da ...

  8. 对bootstrap modal的简单扩展封装

    对bootstrap modal的简单扩展封装 参考自:http://www.muzilei.com/archives/677   注:原文不支持bootstrap新版本,并且居中等存在问题 此段时间 ...

  9. WPF快速入门系列(1)——WPF布局概览

    一.引言 关于WPF早在一年前就已经看过<深入浅出WPF>这本书,当时看完之后由于没有做笔记,以至于我现在又重新捡起来并记录下学习的过程,本系列将是一个WPF快速入门系列,主要介绍WPF中 ...

  10. Qt 5.3.1 版本应用程序的发布问题

    问题描述:用过Qt的朋友,都知道,完成的Qt程序,只能在QT环境里运行.在debug环境里,没有配置环境路线的情况下,必须包含多个dll库,然而每个dll库的大小确实很大的.但有时候还是会失败的,在一 ...