我是靠谱客的博主 闪闪早晨,这篇文章主要介绍B. Alyona and a tree,现在分享给大家,希望可以做个参考。

二分+dfs序+前缀和+瞎几把yy

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "a.h" int n; aLL a; struct edge { int to, nx, w; } e[N << 1]; arr fi; int ce = 1; aLL sum, ans; aLL vec, up, down, res; ll tot = 1; ll rs = 0; inline void add(int u, int v, int w) { e[++ce] = edge{v, fi[u], w}; fi[u] = ce; } void dfs(int x) { if (fi[x] == -1) { rs = res[x]; rs += up[x], rs -= down[x]; ans[x] = rs; return; } for (int i = fi[x]; i != -1; i = e[i].nx) { int v = e[i].to, w = e[i].w; sum[++tot] = sum[tot - 1] + w; int p = lower_bound(sum + 1, sum + 1 + tot, sum[tot] - a[v]) - sum; if (tot - 1 >= p) up[vec[tot - 1]]++, down[vec[p - 1]]++; vec[tot] = v; dfs(v); tot--; res[x] += rs; rs = 0; } rs = res[x]; rs += up[x], rs -= down[x]; ans[x] = rs; } int main() { // file("test"); while (~scanf("%d", &n)) { me(fi, -1); For(i, 1, n) sdf(a[i]); For(i, 2, n) { int x, y; sdf(x), sdf(y); add(x, i, y); } vec[1] = 1; sum[1] = 0; dfs(1); For(i, 1, n) printf("%d ", ans[i]); } return 0; }

 

转载于:https://www.cnblogs.com/planche/p/9503352.html

最后

以上就是闪闪早晨最近收集整理的关于B. Alyona and a tree的全部内容,更多相关B.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部