http://codeforces.com/problemset/problem/740/D

对于每一对<u, v>。设dis[u]表示root到点u的距离,那么dis<u去v>就是dis[v] - dis[u],

就是在它的儿子中找出有多少个v使得dis[v] - dis[u] <= a[v]。然后,因为如果v确定了,那么dis[v]和a[v]就确定了。

所以把公式转换为dis[v] - a[v] <= dis[u]。

那么可以暴力枚举每一个u,然后在其儿子中找有多少个数小于等于它,这个可以直接暴力分块。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <assert.h>
  7. #define IOS ios::sync_with_stdio(false)
  8. using namespace std;
  9. #define inf (0x3f3f3f3f)
  10. typedef long long int LL;
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14. #include <vector>
  15. #include <set>
  16. #include <map>
  17. #include <queue>
  18. #include <string>
  19. const int maxn = 2e5 + ;
  20. struct node {
  21. int u, v, w;
  22. int tonext;
  23. }e[maxn];
  24. int first[maxn];
  25. int num;
  26. void add(int u, int v, int w) {
  27. ++num;
  28. e[num].u = u;
  29. e[num].v = v;
  30. e[num].w = w;
  31. e[num].tonext = first[u];
  32. first[u] = num;
  33. }
  34. int a[maxn];
  35. int ans[maxn];
  36. LL dp[maxn];
  37. struct LIST {
  38. int id;
  39. LL val;
  40. }List[maxn];
  41. int lenList;
  42. int DFN;
  43. int L[maxn];
  44. int R[maxn];
  45. void dfs(int cur) {
  46. List[++lenList].id = cur;
  47. // List[lenList].val = a[cur];
  48. ++DFN;
  49. L[cur] = DFN;
  50. for (int i = first[cur]; i; i = e[i].tonext) {
  51. int v = e[i].v;
  52. dp[v] = dp[e[i].u] + e[i].w;
  53. dfs(v);
  54. }
  55. R[cur] = DFN;
  56. }
  57. LL tosort[maxn];
  58. void work() {
  59. int n;
  60. scanf("%d", &n);
  61. for (int i = ; i <= n; ++i) {
  62. scanf("%d", &a[i]);
  63. }
  64. for (int i = ; i <= n - ; ++i) {
  65. int fa, w;
  66. scanf("%d%d", &fa, &w);
  67. add(fa, i + , w);
  68. }
  69. dfs();
  70. for (int i = ; i <= lenList; ++i) {
  71. List[i].val = dp[List[i].id] - a[List[i].id];
  72. tosort[i] = List[i].val;
  73. // printf("%d ", List[i].id);
  74. // printf("%d %d\n", L[List[i].id], R[List[i].id]);
  75. }
  76. int magic = (int)sqrt(lenList);
  77. for (int i = ; i <= lenList;) {
  78. if (i + magic - <= lenList) {
  79. sort(tosort + i, tosort + i + magic);
  80. } else break;
  81. i += magic;
  82. }
  83. for (int i = ; i <= n; ++i) {
  84. for (int j = L[i] + ; j <= R[i];) {
  85. if (j % magic == && j + magic - <= R[i]) {
  86. int pos = upper_bound(tosort + j, tosort + j + magic, dp[i]) - (tosort + j - );
  87. ans[i] += pos - ;
  88. j += magic;
  89. } else {
  90. if (dp[i] >= List[j].val) {
  91. ans[i]++;
  92. }
  93. j++;
  94. }
  95. }
  96. }
  97. for (int i = ; i <= n; ++i) {
  98. printf("%d ", ans[i]);
  99. }
  100. }
  101. int main() {
  102. #ifdef local
  103. freopen("data.txt", "r", stdin);
  104. // freopen("data.txt", "w", stdout);
  105. #endif
  106. work();
  107. return ;
  108. }

D. Alyona and a tree 公式转换 + 分块暴力的更多相关文章

  1. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. 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 ...

  3. codeforces 381 D Alyona and a tree(倍增)(前缀数组)

    Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. 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 ...

  5. 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 ...

  6. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  7. Alyona and a tree

    Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 纯C语言实现简单封装继承机制

    0 继承是OO设计的基础 继承是OO设计中的基本部分,也是实现多态的基础,C++,C#,Objective-C.Java.PHP.JavaScript等为OO而设计的语言,其语言本身对实现继承提供了直 ...

  2. httpclient发送get请求

    /** * 获取httpclient的请求url地址 */ public static String getUrl(){ String url = "http://"+map.ge ...

  3. hadoop生态系统学习之路(十)MR将结果输出到hbase

    之前讲了MR将结果输出到hdfs.hive.db,今天再给大家分享一下,怎样将结果输出到hbase. 首先,提一句,笔者在hadoop集群运行此MR的时候报了一个错误.是一个jar包的缘故,这个错误是 ...

  4. 使用JS对select标签进行联动选择

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. centos 安装mysql时错误unknown variable &#39;defaults-file=/opt/redmine-2.6.0-2/mysql/my.cnf&#39;

    找到my.cnf所在目录.运行 chmod 664 my.cnf,再启动mysql成功

  6. 【iOS系列】-UIImageView帧动画相关属性介绍

    UIImageView帧动画相关属性介绍 1:相关属性: //An array of UIImage objects to use for an animation.存放UIImage对象,会按顺序显 ...

  7. 在webkit中如何避免触发layout(重排)

    很多web开发者都已经意识到,在脚本执行中,DOM操作的用时可能比js本身执行时间要长很多,其中潜在的消耗基本上是由于触发了layout(即重排reflow:由DOM树构建为Render渲染树的过程) ...

  8. 查看android-support-v4.jar引出的问题

    1.前面博文里也写过如何关联android-support-v4.jar的源码 今天新项目用上述方法的时候,竟然不成功..来回反复试了很长时间,最后发现 新建项目,会自动引用一个类库(自动新建的..) ...

  9. Django模板语言(一)

    1,Django模板语言 1.1>基础语法:1.1.1,变量相关:{{ 变量名 }},1.1.2,逻辑相关{% ... %} 在Django模板语言中按此语法使用:{{ 变量名 }},当模板引擎 ...

  10. bean的scope属性

    1.singleton  (默认属性)  Spring将Bean放入Spring IOC容器的缓存池中,并将Bean引用返回给调用者,spring IOC继续对这些Bean进行后续的生命管理.Bean ...