Submission #594822

#TimeUsernameProblemLanguageResultExecution timeMemory
594822czhang2718Magic Tree (CEOI19_magictree)C++17
100 / 100
140 ms37868 KiB
#include "bits/stdc++.h" using namespace std; typedef long long ll; const int N=1e5; int n, m, k; int par[N]; int d[N], w[N]; map<int,ll> mp[N]; int main(){ cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> k; for(int i=1; i<n; i++){ cin >> par[i]; --par[i]; } for(int i=0; i<m; i++){ int v; cin >> v; v--; cin >> d[v] >> w[v]; } auto join=[&](int i, int j){ if(mp[i].size()>mp[j].size()) swap(mp[i], mp[j]); for(auto p:mp[i]){ mp[j][p.first]+=p.second; } }; for(int i=n-1; i>=0; i--){ mp[i][d[i]]+=w[i]; auto it=mp[i].upper_bound(d[i]); while(it!=mp[i].end()){ if(it->second>w[i]){ it->second-=w[i]; break; } w[i]-=it->second; it++; mp[i].erase(prev(it)); } // cout << "mp[" << i << "]\n"; // for(auto p:mp[i]) cout << p.first << " " << p.second << "\n"; if(i) join(i, par[i]); } ll ans=0; for(auto p:mp[0]) ans+=p.second; cout << ans; }
#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...