File tree Expand file tree Collapse file tree 2 files changed +41
-9
lines changed
tests/library_checker_aizu_tests/trees Expand file tree Collapse file tree 2 files changed +41
-9
lines changed Original file line number Diff line number Diff line change @@ -16,27 +16,23 @@ int main() {
16
16
adj[par].push_back (i);
17
17
adj[i].push_back (par);
18
18
}
19
- HLD<0 > hld_nodes (adj);
20
- HLD<1 > hld_edges (adj);
19
+ HLD<1 > hld (adj);
21
20
BIT bit (n);
22
- for (int i = 0 ; i < n; i++)
23
- bit.update (hld_nodes.tin [i], a[i]);
21
+ for (int i = 0 ; i < n; i++) bit.update (hld.tin [i], a[i]);
24
22
while (q--) {
25
23
int type;
26
24
cin >> type;
27
25
if (type == 0 ) {
28
26
int v, x;
29
27
cin >> v >> x;
30
- bit.update (hld_nodes .tin [v], x);
28
+ bit.update (hld .tin [v], x);
31
29
a[v] += x;
32
30
} else {
33
31
int v;
34
32
cin >> v;
35
- auto [l, r] = hld_nodes .subtree (v);
33
+ auto [l, r] = hld .subtree (v);
36
34
ll res = bit.query (l, r);
37
- auto [l_edges, r_edges] = hld_edges.subtree (v);
38
- assert (res == bit.query (l_edges, r_edges) + a[v]);
39
- cout << res << ' \n ' ;
35
+ cout << bit.query (l, r) + a[v] << ' \n ' ;
40
36
}
41
37
}
42
38
}
Original file line number Diff line number Diff line change
1
+ #define PROBLEM \
2
+ " https://judge.yosupo.jp/problem/vertex_add_subtree_sum"
3
+ #include " ../template.hpp"
4
+ #include " ../../../library/trees/hld.hpp"
5
+ #include " ../../../library/data_structures/bit.hpp"
6
+ int main () {
7
+ cin.tie (0 )->sync_with_stdio (0 );
8
+ int n, q;
9
+ cin >> n >> q;
10
+ vector<ll> a (n);
11
+ for (int i = 0 ; i < n; i++) cin >> a[i];
12
+ vector<vector<int >> adj (n);
13
+ for (int i = 1 ; i < n; i++) {
14
+ int par;
15
+ cin >> par;
16
+ adj[par].push_back (i);
17
+ adj[i].push_back (par);
18
+ }
19
+ HLD<0 > hld (adj);
20
+ BIT bit (n);
21
+ for (int i = 0 ; i < n; i++) bit.update (hld.tin [i], a[i]);
22
+ while (q--) {
23
+ int type;
24
+ cin >> type;
25
+ if (type == 0 ) {
26
+ int v, x;
27
+ cin >> v >> x;
28
+ bit.update (hld.tin [v], x);
29
+ } else {
30
+ int v;
31
+ cin >> v;
32
+ auto [l, r] = hld.subtree (v);
33
+ cout << bit.query (l, r) << ' \n ' ;
34
+ }
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments