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 ...
随机推荐
- JSON和js对象之间的相互转化
jQuery插件支持的转换方式 $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 http://www. ...
- centos(Linux)系统阿里云ECS搭建 jdk,tomcat和MySQL环境,并部署web程序
之前我对这个东西一无所知,攻击力为0,谢谢各个论坛上面的兄弟们的帮助. 过程: 首先ssh远程登陆: ssh root@你的公网ip ,输入密码 1,jdk我用的版本是jdk-7u80-linux-x ...
- C编程风格的人机交互 -- CSHELL (提供源码下载)
记得上大学时,做C语言的程序都是用sdb来调试的:再后来有了gdb,同sdb差不多,不过就好用了很多.但终究还是有点遗憾.比如,程序里设计了几个函数,如果想测试下它们,就不得不再编写个测试函数,用各种 ...
- JQuery学习(选择器-基本-*)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 分享一段视频关于SQL2014 Hekaton数据库的
分享一段视频关于SQL2014 Hekaton数据库的 Microsoft SQL Server In-Memory OLTP Project "Hekaton": App Dev ...
- 需要弥补的那部分SQL
一.前言 虽然我们大多数人都学习过SQL,但是经常忽略它.总是会自以为学到的已经足够用了,从而导致我们在实际开发的过程中遇到复杂的问题后只能在检索数据后通过传统的代码来完成,但是其中很多的功能利用SQ ...
- WinServer2008 R2搭建TFS2013小结(无法连接Internet手动安装)
不定时更新参考文档: TFS安装与管理 为本地管理配置本机模式报表服务器 (SSRS) 手里有文档还是掉进各种坑,这里把坑总结一下,方面以后填坑. 安装指导文档中搭建TFS2013用了两台服务器,把S ...
- Kali Linux系列教程之OpenVas安装
Kali Linux系列教程之OpenVas安装 文 /玄魂 目录 Kali Linux系列教程之OpenVas安装 前言 1. 服务器层组件 2.客户层组件 安装过程 Initial setup ...
- Ubuntu配置git
安装ssh sudo apt-get install openssh-server sudo apt-get install openssh-client 启动SSH服务 sudo /etc/init ...
- 自己动手写UI库——引入ExtJs(布局)
第一: 来看一下最终的效果 第二: 来看一下使用方法: 第三: Component类代码如下所示: public class Component { pub ...