This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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])
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |