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 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序 二分 前缀和的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Mysql --分区表(5)Columns分区
COLUMNS分区 COLUMNS分区是RANGE和LIST分区的变种.COLUMNS分区支持多列作为分区键进行分区 RANGE COLUNMS分区和LIST COLUMNS都支持非INT型列作为分区 ...
- berkeley db replica机制 - 主从同步
repmgr/repmgr_net.c, __repmgr_send(): 做send_broadcast, 然后根据policy 对DB_REP_PERMANENT的处理 __repmgr_send ...
- linux tcp协议状态机
截图来自百度文库 TCP状态-有限状态机
- solr与.net系列课程(二)solr的配置文件及其含义
solr与.net系列课程(二)solr的配置文件及其含义 本节内容还是不会涉及到.net与数据库的内容,但是不要着急,这都是学时solr必学要掌握的东西,solr可不是像其他的dll文件一样,只需 ...
- 【Win10】UAP/UWP/通用 开发之 SplitView
[Some information relates to pre-released product which may be substantially modified before it's co ...
- Apache Mina(一)
原文链接:http://www.cnblogs.com/xuekyo/archive/2013/03/06/2945826.html Apache Mina是一个能够帮助用户开发高性能和高伸缩性网络应 ...
- iOS——Command-Line 查看当前SDK版本并修改默认SDK版本
在工作中可能会碰到用命令行编译.打包iOS应用程序的情况(xcodebuild相关命令). 但是由于SDK版本问题,会报错,说某SDK版本不对,可能是因为升级Xcode导致的SDK版本升级,为了避免高 ...
- Windows下使用Redis(一)安装使用
一.Redis 是什么 Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store).它通常被称为数据结构服务器,因为值(value)可以是 字符串(S ...
- EF结合三层:三层中数据层父类和业务层父类的使用
今天我们主要讨论下数据层父类和业务层父类的使用.众所周知,数据层无非就是实现增删改查的方法.无论是哪个实体类,无非就是为了实现增删改查方法,所有我们在三层的DAL层封装了一个BaseDAL类,来做增删 ...
- Linux安装snmp
1.yum安装 yum -y install net-snmp* 2.修改配置文件/etc/snmp/snmpd.conf com2sec notConfigUser default public 默 ...