http://codeforces.com/problemset/problem/739/B

dfs,记录距离前缀和,每次找到离最近的不符合的点。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std; int n,cnt = ,a[],b[],sta[],ans[] = {};
long long dis[];
vector<int> v[]; void dfs(int x,long long deep)
{
sta[++cnt] = x;
dis[x] = deep;
for(int i = ;i < v[x].size();i++)
{
int t = v[x][i];
dfs(t,dis[x]+b[t]);
ans[x] += ans[t]+;
}
int l = ,r = cnt-;
while(l < r)
{
int mid = (l+r+)/;
if(dis[x]-dis[sta[mid]] > a[x]) l = mid;
else r = mid-;
}
ans[sta[l]]--;
cnt--;
}
int main()
{
scanf("%d",&n);
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
for(int i = ;i <= n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(i);
b[i] = y;
}
dfs(,);
for(int i = ;i <= n;i++) printf("%d ",ans[i]);
printf("\n");
return ;
}

Codeforces_739_B的更多相关文章

随机推荐

  1. 在Mac/linux上查找(并终止)进程锁定特定端口的几种方法

    前言  无论是做网站还是做产品,经常使用到杀死某个进程的方法.制作脚本并熟悉运用是一个非常节省时间的方法. 基本命令  查找: [sudo] lsof -i :3000  杀戮 kill -9 方法一 ...

  2. floj 2264

    2.公路建设(highway.c/cpp/pas) 在滨海市一共有n 个城市,编号依次为1到n,它们之间计划修建m条双向道路,其中修建第i条道路的费用为ci. 海霸王作为滨海市公路建设项目的总工程师, ...

  3. 1z0-062 题库解析3

    The hr user executes the following query on the employees table but does not issue commit, rollback, ...

  4. Friday the Thirteenth 黑色星期五 USACO 模拟 超级简单做法

    1003: 1.1.3 Friday the Thirteenth 黑色星期五 时间限制: 1 Sec  内存限制: 128 MB提交: 8  解决: 8[提交] [状态] [讨论版] [命题人:外部 ...

  5. CF 558 C

    Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...

  6. 树上点分治 poj 1741

    Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v ...

  7. hdu6621 二分加主席树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 Problem Description You have an array: a1, a2,  ...

  8. Scheme实现数字电路仿真(2)——原语

    版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/12045295.html 作者:窗户 ...

  9. 「 从0到1学习微服务SpringCloud 」10 服务网关Zuul

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config 「 从0到1学习微服务SpringCloud 」07 RabbitM ...

  10. Maven的学习之路1

    对于下列这段Maven的命令行有不懂之处,在zgmzyr的博客上得到了解释.非常感谢这位博友,写在自己的随笔上以便查看和修改. 1. 创建项目 $ mvn archetype:generate -Dg ...