Submission #871879

#TimeUsernameProblemLanguageResultExecution timeMemory
871879DylanSmithMagic Tree (CEOI19_magictree)C++17
100 / 100
139 ms42832 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x),end(x) #define lb(x,y) lower_bound(all(x),y)-begin(x) mt19937 rng; void solve() { int N, M, K; cin >> N >> M >> K; vector<vector<int>> adj(N); for (int i = 1; i < N; i++) { int p; cin >> p; p--; adj[p].pb(i); } vector<vector<vector<int>>> fruits(N); for (int i = 0; i < M; i++) { int u, t, k; cin >> u >> t >> k; u--; fruits[u].pb({t, k}); } vector<map<int, ll>> mp(N); for (int u = N - 1; u >= 0; u--) { for (int v : adj[u]) { if (sz(mp[v]) > sz(mp[u])) { swap(mp[v], mp[u]); } } for (int v : adj[u]) { for (auto &p : mp[v]) { mp[u][p.first] += p.second; } } for (auto &fruit : fruits[u]) { int t = fruit[0], k = fruit[1]; mp[u][t] += k; ll m = k; while (m && prev(mp[u].end())->first >= t + 1) { int f = mp[u].lower_bound(t + 1)->first; ll r = min(mp[u][f], m); mp[u][f] -= r; m -= r; if (!mp[u][f]) mp[u].erase(f); } } } ll res = 0; for (auto &p : mp[0]) { res += p.second; } cout << res << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); rng = mt19937(chrono::steady_clock::now().time_since_epoch().count()); solve(); return 0; }
#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...