codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree
2 seconds
256 megabytes
standard input
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 v controls u.
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.
Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.
5
2 5 1 4 6
1 7
1 1
3 5
3 6
1 0 1 0 0
5
9 7 8 6 5
1 1
2 1
3 1
4 1
4 3 2 1 0
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 1 controls the vertex 5).
【分析】题意就不说了,说下怎么做。很显然暴力肯定超时。对于每一个节点,用倍增的思想,fa[i][x]、cost[i][x]数组分别记录x节点往上第2的i次方个祖先编号和与那个祖先的距离。如果节点x对节点f有一个贡献,那么x对处于x和f之间的节点也应该有一个贡献,所以用前缀数组的思想,ans[x]++,ans[f]--,然后只需要在dfs的时候一个一个累加就行了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e5+;
int n,m,k,u;
int fa[][N];
ll cost[][N];
ll a[N],ans[N];
struct man{
int son;
ll co;
};
vector<man>edg[N];
void dfs(int x){
for(int i=;fa[i-][fa[i-][x]];i++){
fa[i][x]=fa[i-][fa[i-][x]];
cost[i][x]=cost[i-][x]+cost[i-][fa[i-][x]];
}
for(int i=;i<edg[x].size();i++){
man e=edg[x][i];
int v=e.son;ll c=e.co;
fa[][v]=x;cost[][v]=c;
dfs(v);
}
}
int Find(int x){
ll c=a[x];
for(int i=;i>=;i--){
if(cost[i][x]<=c&&fa[i][x]){
c-=cost[i][x];
x=fa[i][x];
}
}
return x;
}
void sum_dfs(int x){
for(int i=;i<edg[x].size();i++){
man e=edg[x][i];
int v=e.son;ll c=e.co;
sum_dfs(v);
ans[x]+=ans[v];
}
}
int main (){
ll c;
met(ans,);
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%lld",&a[i]);
for(int i=;i<=n;i++){
scanf("%d%lld",&u,&c);
man s;s.co=c;s.son=i;
edg[u].push_back(s);
}
dfs();
for(int i=;i<=n;i++){
int f=Find(i);
ans[fa[][f]]--;
ans[fa[][i]]++;
}
sum_dfs();
for(int i=;i<=n;i++)printf("%lld ",ans[i]);printf("\n");
return ;
}
codeforces 381 D Alyona and a tree(倍增)(前缀数组)的更多相关文章
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 682C C. Alyona and the Tree(dfs)
题目链接: C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input ...
- 【CodeForces - 682C】Alyona and the Tree(dfs)
Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...
- 【Codeforces 682C】Alyona and the Tree
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设 ...
- 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. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 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 #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 ...
随机推荐
- (原创)详解Quartus导出网表文件:.qxp和.vqm
当项目过程中,不想给甲方源码时,该如何?我们可以用网表文件qxp或者vqm对资源进行保护. 下面讲解这两个文件的具体生成步骤: 一.基本概念 QuartusII的qxp文件为QuartusII Exp ...
- C++ do{...}while(0)的好处
在开源软件里面经常可以看到这样的写法. #define X(a) do { f1(a); f2(a); } while(0) 1. 主要作用是放在宏定义里面,避免宏带来的语法问题. 比如 #defin ...
- 使用 InstallShield 制作 Delphi 软件安装包
软件版本: InstallShield 12 Delphi 5/7 SQL Server 2005 一.配置软件信息 二.软件安装的需求配置 三.安装架构 四.安装需要的文件 软件的安装路径.可执行文 ...
- Google Volley框架源码走读
PS一句:最终还是选择CSDN来整理发表这几年的知识点,该文章平行迁移到CSDN.因为CSDN也支持MarkDown语法了,牛逼啊! [工匠若水 http://blog.csdn.net/yanb ...
- DEELX 正则表达式引擎(v1.2)
DEELX 正则表达式引擎(v1.2) 简介见文末. 选择使用deelx的理由:全部代码位于一个头文件(.h)中, 比任何引擎都使用简单和方便. 利用分组从字符串当中提取出化学元素英文名.比如 Ag, ...
- dll文件是什么
dll实际上是动态链接库的缩写,从windows1.0开始,动态链接库就是整个操作系统的基础,那么这有什么作用呢?在dos时代,程序员是通过编写程序来达到预期的目的的,每实现一个目的就需要编写一个程序 ...
- 常用JavaBean:JdbcBean codes:Java通过JDBC 连接 Mysql 数据库
package bean;import java.sql.*;import com.mysql.jdbc.PreparedStatement;public class JdbcBean { publi ...
- 20145224&20145238 《信息安全系统设计基础》 第一次实验
20145224&20145238 <信息安全系统设计基础>第一次实验 课程:信息安全系统设计基础 班级:1452 姓名:陈颢文 荆玉茗 学号:20145224 20145238 ...
- sans-serif
sans-serif无衬线字体,是一类字体,它在操作系统或者浏览器里是可以设置的,你可以把它设置成宋体,也可以设置成微软雅黑,而设置的这种字体肯定是当前系统里存在的字体,所以使用这个字体就一肯能显示出 ...
- const char * char * const
2 区分 const * 与 * const 的差别 ( 1 ) 指针本身是常量不可变 ( char * ) const pCount; const (char *) pCount; ( 2 ) ...