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. JS初级-作用域

    作用域:域:空间.范围.区域--作用:读.写    script        全局变量.全局函数        自上而下        函数        由里到外        {}    浏览器 ...

  2. NY 269 VF

    题目 求1—1000000000之间的数,它的各位数字之和为 s. dp[i][j]表示 i 位数,它的各位数之和为 j 的总个数. 这里假设第 i 位为 k,则前 i - 1 位的和应为 j - k ...

  3. Python字符转换

    Python提供了ord和chr两个内置的函数,用于字符与ASCII码之间的转换. 如:>>> print ord('a') 97 >>> print chr(97 ...

  4. Robberies(HDU2955):01背包+概率转换问题(思维转换)

    Robberies  HDU2955 因为题目涉及求浮点数的计算:则不能从正面使用01背包求解... 为了能够使用01背包!从唯一的整数(抢到的钱下手)... 之后就是概率的问题: 题目只是给出被抓的 ...

  5. 解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assemb

    解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceMode ...

  6. 盘点国内网站常用的一些 CDN 公共库加速服务

    CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用.与将JS库存放在服务器单机上相比,CDN公共库更加稳定.高速.一 般的CDN公共库都会包含全球所有最流行的开源JavaScri ...

  7. 深入浅出话VC++(3)——VC++实现绘图操作

    VC++实现绘图操作,说白了也就是对API熟练操作了,下面介绍几种绘图 1. 绘制线条 具体实现代码如下: // 鼠标左键按下时的处理函数 void CDrawView::OnLButtonDown( ...

  8. Asp.Net Web API 2第七课——Web API异常处理

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文主要来讲解Asp.Ne ...

  9. 轻量级实用JQuery表单验证插件:validateForm5

    表单验证是Web项目一个必不可少的环节,而且是一项重复的劳动,于是小菜封装了一款简单的表单验证插件,名字叫:validateForm5. validateForm5插件基于Jquery,并向HTML5 ...

  10. C++ Primer 快速入门

    <C++ Primer 4th> 读书摘要 必须有一个命名为 main.操作系统通过 main 函数返回的值来确定程序是否成功执行完毕.返回 0 值表明程序程序成功执行完毕.任何其他非零的 ...