Submission #574154

#TimeUsernameProblemLanguageResultExecution timeMemory
574154AQTMagic Tree (CEOI19_magictree)C++14
100 / 100
106 ms31588 KiB
#include <bits/stdc++.h> using namespace std; int N, M, K; vector<int> graph[100005]; int day[100005]; long long val[100005]; map<int, long long> dfs(int n) { map<int, long long> ret; for(int e : graph[n]) { auto m = dfs(e); if(ret.size() < m.size()) { swap(ret, m); } for(auto p : m) { ret[p.first] += p.second; } } if(val[n]) { ret[day[n]] += val[n]; long long pre = 0; while(1) { auto ptr = ret.upper_bound(day[n]); if(ptr != ret.end()) { pre += ptr->second; if(pre > val[n]) { ptr->second = pre - val[n]; break; } else { ret.erase(ptr); } } else { break; } } } return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> M >> K; for(int i = 2; i <= N; i++) { int p; cin >> p; graph[p].push_back(i); } for(int i = 1; i <= M; i++) { int n; cin >> n; cin >> day[n] >> val[n]; } auto mp = dfs(1); long long ans = 0; for(auto p : mp) { ans += p.second; } cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...