#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define len(a) (int)a.size()
#define endl '\n'
#define int long long
using namespace std;
const int INF = 1e18;
const int MAXN = 2e5 + 100;
vector<int> g[MAXN];
map<int, int> stored_map[MAXN];
map<int, int> global_map;
int subtree[MAXN], fruit_time[MAXN], fruit_weight[MAXN], has_fruit[MAXN];
void dfs1(int u) {
subtree[u] = 1;
for (int v : g[u]) {
dfs1(v);
subtree[u] += subtree[v];
}
}
void dfs2(int u, bool store_info = true) {
int max_subtree_v = -1;
for (int v : g[u]) {
if (max_subtree_v == -1 || subtree[v] > subtree[max_subtree_v])
max_subtree_v = v;
}
for (int v : g[u]) {
if (v != max_subtree_v)
dfs2(v, true);
}
global_map.clear();
if (max_subtree_v != -1)
dfs2(max_subtree_v, false);
for (int v : g[u]) {
if (v != max_subtree_v) {
for (auto [x, dif] : stored_map[v])
global_map[x] += dif;
}
}
if (has_fruit[u]) {
global_map[fruit_time[u]] += fruit_weight[u];
auto it = global_map.upper_bound(fruit_time[u]);
int acc = 0;
while (it != global_map.end()) {
acc += it->second;
if (acc <= fruit_weight[u]) {
global_map.erase(it++);
} else {
it->second = acc - fruit_weight[u];
break;
}
}
}
if (store_info)
stored_map[u] = global_map;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef MY_DEBUG
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#endif
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p - 1].push_back(i);
}
for (int i = 0; i < m; i++) {
int u;
cin >> u;
has_fruit[u - 1] = true;
cin >> fruit_time[u - 1] >> fruit_weight[u - 1];
}
dfs1(0);
dfs2(0);
int ans = 0;
for (auto [x, dif] : stored_map[0])
ans += dif;
cout << ans;
}
Compilation message
magictree.cpp: In function 'void dfs2(long long int, bool)':
magictree.cpp:41:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for (auto [x, dif] : stored_map[v])
| ^
magictree.cpp: In function 'int32_t main()':
magictree.cpp:86:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for (auto [x, dif] : stored_map[0])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20568 KB |
Output is correct |
2 |
Correct |
4 ms |
20568 KB |
Output is correct |
3 |
Correct |
5 ms |
20644 KB |
Output is correct |
4 |
Correct |
5 ms |
20572 KB |
Output is correct |
5 |
Correct |
5 ms |
20572 KB |
Output is correct |
6 |
Correct |
4 ms |
20572 KB |
Output is correct |
7 |
Correct |
5 ms |
20572 KB |
Output is correct |
8 |
Correct |
4 ms |
20572 KB |
Output is correct |
9 |
Correct |
4 ms |
20572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
32964 KB |
Output is correct |
2 |
Correct |
47 ms |
33976 KB |
Output is correct |
3 |
Correct |
122 ms |
52356 KB |
Output is correct |
4 |
Correct |
64 ms |
36708 KB |
Output is correct |
5 |
Correct |
96 ms |
38744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20824 KB |
Output is correct |
2 |
Correct |
5 ms |
20824 KB |
Output is correct |
3 |
Correct |
5 ms |
20828 KB |
Output is correct |
4 |
Correct |
45 ms |
39860 KB |
Output is correct |
5 |
Correct |
65 ms |
47696 KB |
Output is correct |
6 |
Correct |
62 ms |
39808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
28492 KB |
Output is correct |
2 |
Correct |
53 ms |
27720 KB |
Output is correct |
3 |
Correct |
47 ms |
32088 KB |
Output is correct |
4 |
Correct |
37 ms |
28884 KB |
Output is correct |
5 |
Correct |
42 ms |
40020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20568 KB |
Output is correct |
2 |
Correct |
4 ms |
20568 KB |
Output is correct |
3 |
Correct |
5 ms |
20644 KB |
Output is correct |
4 |
Correct |
5 ms |
20572 KB |
Output is correct |
5 |
Correct |
5 ms |
20572 KB |
Output is correct |
6 |
Correct |
4 ms |
20572 KB |
Output is correct |
7 |
Correct |
5 ms |
20572 KB |
Output is correct |
8 |
Correct |
4 ms |
20572 KB |
Output is correct |
9 |
Correct |
4 ms |
20572 KB |
Output is correct |
10 |
Correct |
65 ms |
30424 KB |
Output is correct |
11 |
Correct |
64 ms |
29776 KB |
Output is correct |
12 |
Correct |
48 ms |
31568 KB |
Output is correct |
13 |
Correct |
34 ms |
28112 KB |
Output is correct |
14 |
Correct |
40 ms |
39504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
21340 KB |
Output is correct |
2 |
Correct |
28 ms |
23520 KB |
Output is correct |
3 |
Correct |
33 ms |
23380 KB |
Output is correct |
4 |
Correct |
28 ms |
23464 KB |
Output is correct |
5 |
Correct |
11 ms |
21976 KB |
Output is correct |
6 |
Correct |
26 ms |
25948 KB |
Output is correct |
7 |
Correct |
25 ms |
29604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20568 KB |
Output is correct |
2 |
Correct |
4 ms |
20568 KB |
Output is correct |
3 |
Correct |
5 ms |
20644 KB |
Output is correct |
4 |
Correct |
5 ms |
20572 KB |
Output is correct |
5 |
Correct |
5 ms |
20572 KB |
Output is correct |
6 |
Correct |
4 ms |
20572 KB |
Output is correct |
7 |
Correct |
5 ms |
20572 KB |
Output is correct |
8 |
Correct |
4 ms |
20572 KB |
Output is correct |
9 |
Correct |
4 ms |
20572 KB |
Output is correct |
10 |
Correct |
5 ms |
20824 KB |
Output is correct |
11 |
Correct |
5 ms |
20824 KB |
Output is correct |
12 |
Correct |
5 ms |
20828 KB |
Output is correct |
13 |
Correct |
45 ms |
39860 KB |
Output is correct |
14 |
Correct |
65 ms |
47696 KB |
Output is correct |
15 |
Correct |
62 ms |
39808 KB |
Output is correct |
16 |
Correct |
65 ms |
30424 KB |
Output is correct |
17 |
Correct |
64 ms |
29776 KB |
Output is correct |
18 |
Correct |
48 ms |
31568 KB |
Output is correct |
19 |
Correct |
34 ms |
28112 KB |
Output is correct |
20 |
Correct |
40 ms |
39504 KB |
Output is correct |
21 |
Correct |
18 ms |
24152 KB |
Output is correct |
22 |
Correct |
60 ms |
31732 KB |
Output is correct |
23 |
Correct |
74 ms |
36692 KB |
Output is correct |
24 |
Correct |
120 ms |
45648 KB |
Output is correct |
25 |
Correct |
57 ms |
35800 KB |
Output is correct |
26 |
Correct |
88 ms |
36944 KB |
Output is correct |
27 |
Correct |
64 ms |
34384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20568 KB |
Output is correct |
2 |
Correct |
4 ms |
20568 KB |
Output is correct |
3 |
Correct |
5 ms |
20644 KB |
Output is correct |
4 |
Correct |
5 ms |
20572 KB |
Output is correct |
5 |
Correct |
5 ms |
20572 KB |
Output is correct |
6 |
Correct |
4 ms |
20572 KB |
Output is correct |
7 |
Correct |
5 ms |
20572 KB |
Output is correct |
8 |
Correct |
4 ms |
20572 KB |
Output is correct |
9 |
Correct |
4 ms |
20572 KB |
Output is correct |
10 |
Correct |
60 ms |
32964 KB |
Output is correct |
11 |
Correct |
47 ms |
33976 KB |
Output is correct |
12 |
Correct |
122 ms |
52356 KB |
Output is correct |
13 |
Correct |
64 ms |
36708 KB |
Output is correct |
14 |
Correct |
96 ms |
38744 KB |
Output is correct |
15 |
Correct |
5 ms |
20824 KB |
Output is correct |
16 |
Correct |
5 ms |
20824 KB |
Output is correct |
17 |
Correct |
5 ms |
20828 KB |
Output is correct |
18 |
Correct |
45 ms |
39860 KB |
Output is correct |
19 |
Correct |
65 ms |
47696 KB |
Output is correct |
20 |
Correct |
62 ms |
39808 KB |
Output is correct |
21 |
Correct |
56 ms |
28492 KB |
Output is correct |
22 |
Correct |
53 ms |
27720 KB |
Output is correct |
23 |
Correct |
47 ms |
32088 KB |
Output is correct |
24 |
Correct |
37 ms |
28884 KB |
Output is correct |
25 |
Correct |
42 ms |
40020 KB |
Output is correct |
26 |
Correct |
65 ms |
30424 KB |
Output is correct |
27 |
Correct |
64 ms |
29776 KB |
Output is correct |
28 |
Correct |
48 ms |
31568 KB |
Output is correct |
29 |
Correct |
34 ms |
28112 KB |
Output is correct |
30 |
Correct |
40 ms |
39504 KB |
Output is correct |
31 |
Correct |
9 ms |
21340 KB |
Output is correct |
32 |
Correct |
28 ms |
23520 KB |
Output is correct |
33 |
Correct |
33 ms |
23380 KB |
Output is correct |
34 |
Correct |
28 ms |
23464 KB |
Output is correct |
35 |
Correct |
11 ms |
21976 KB |
Output is correct |
36 |
Correct |
26 ms |
25948 KB |
Output is correct |
37 |
Correct |
25 ms |
29604 KB |
Output is correct |
38 |
Correct |
18 ms |
24152 KB |
Output is correct |
39 |
Correct |
60 ms |
31732 KB |
Output is correct |
40 |
Correct |
74 ms |
36692 KB |
Output is correct |
41 |
Correct |
120 ms |
45648 KB |
Output is correct |
42 |
Correct |
57 ms |
35800 KB |
Output is correct |
43 |
Correct |
88 ms |
36944 KB |
Output is correct |
44 |
Correct |
64 ms |
34384 KB |
Output is correct |
45 |
Correct |
24 ms |
24372 KB |
Output is correct |
46 |
Correct |
69 ms |
32732 KB |
Output is correct |
47 |
Correct |
89 ms |
38120 KB |
Output is correct |
48 |
Correct |
125 ms |
48892 KB |
Output is correct |
49 |
Correct |
67 ms |
36560 KB |
Output is correct |
50 |
Correct |
107 ms |
38080 KB |
Output is correct |
51 |
Correct |
71 ms |
35396 KB |
Output is correct |