Submission #242620

#TimeUsernameProblemLanguageResultExecution timeMemory
242620lycMagic Tree (CEOI19_magictree)C++14
100 / 100
152 ms26232 KiB
#include <bits/stdc++.h> using namespace std; #define TRACE(x) cerr << #x << " :: " << x << endl #define _ << " " << #define SZ(x) (int)(x).size() #define ALL(x) (x).begin(),(x).end() #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define RFOR(i,a,b) for (int i=(a);i>=(b);--i) using ll=long long; using ii=pair<int,int>; const int mxN = 1e5+5; int N, M, K; vector<int> al[mxN]; ii fruit[mxN]; map<int,ll> dfs(int u) { map<int,ll> res; for (int v : al[u]) { auto x = dfs(v); if (SZ(res) < SZ(x)) swap(res,x); for (auto& y : x) { if (res.count(y.first)) res[y.first] += y.second; else res.insert(y); } } if (fruit[u] != ii(-1,-1)) { auto it = res.upper_bound(fruit[u].first); if (it != res.begin() && prev(it)->first == fruit[u].first) { auto x = *prev(it); res.erase(prev(it)); x.second += fruit[u].second; res.insert(x); } else res.insert(fruit[u]); for (ll y = fruit[u].second; y && it != res.end();) { auto x = *it; auto jt = next(it); res.erase(it); ll d = min(y,x.second); y -= d; x.second -= d; if (x.second) res.insert(x); it = jt; } } //cout << u << " :: "; //for (auto& x : res) cout << x.first << " " << x.second << " | "; //cout << endl; return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M >> K; FOR(i,2,N){ int P; cin >> P; al[P].push_back(i); } FOR(i,1,N) fruit[i] = ii(-1,-1); FOR(i,1,M){ int V, D, W; cin >> V >> D >> W; fruit[V] = ii(D,W); } auto res = dfs(1); ll ans = 0; for (auto& x : res) ans += x.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...