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 ...
随机推荐
-
study topics
永远不变的东西,原理 study roadmap: 1.user space: tizen power manager => suspend/resume or runtime? android ...
-
更新页面缓存OutputCache
为什么要使用OutputCache 我认为OutputCache是最简单的缓存技术了,它针对的是页面级别的,简单的一条指令就可以达到缓存的效果,有效的减轻服务器的压力和减少带宽,对于网站一些不会频繁更 ...
-
PHP面试题三
1.nginx使用哪种网络协议? nginx是应用层 我觉得从下往上的话 传输层用的是tcp/ip 应用层用的是http fastcgi负责调度进程 2. <? echo 'hello tush ...
-
php开启ssl支持
1.首先在php的安装文件下找到三个文件 并copy到系统目标下的 system32文件夹下: ssleay32.dll.libeay32.dll,php_openssl.dll. 2.打开php.i ...
-
201521123067 《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 Q1:clone方法 1.1 Object ...
-
javascript中所有函数参数都是按值传递
在看<JavaScript高级程序设计>(第三版)的时候,传递参数这一节,里面提到 ECMAScript中所有函数的参数都是按值传递的 它自己的解释是, 把函数外部的值复制给函数内部的参数 ...
-
ORACLE——获取随机数
在oracle中获取一个指定的随机数: --(DBMS_RANDOM.VALUE(INT NUM1,INT NUM2),比如: ,) FROM DUAL; --结果:8.23602331029803 ...
-
【HAOI2008】硬币购物
既然没人写扩欧,那我就来一发吧. 扩欧也还好,就是跑的有点慢,然后写的时候还有点烦,不过还是卡过去了. 考场上看到这道题又蒙了...怎么回事第一题又要爆零了? 然后我打了个暴力测了一下极限数据根本过不 ...
-
JavaScript之图片操作6
上一篇写的关于放大镜的,可能在实际开发中用的不是很多,接下来将的图片无缝滚动在实际工作中就是用的比较多的了. 如上图,通过定时器控制图片无缝滚动,当鼠标悬浮时停止滚动,鼠标离开,滚动继续. 主要原理是 ...
-
matlab2016b和c# .net4.0混合编程
参考:https://www.cnblogs.com/eniac12/p/4390845.html 主要想用c#写软件界面,利用matlab绘图,或者用里面的遗传算法. 我的环境是:Win10 64位 ...