Submission #703491

#TimeUsernameProblemLanguageResultExecution timeMemory
703491CyanmondMagic Tree (CEOI19_magictree)C++17
100 / 100
188 ms33964 KiB
#include <bits/stdc++.h> using i64 = long long; struct Vertice { bool exFruit; int day; i64 weight; }; int main() { int N, M, K; std::cin >> N >> M >> K; std::vector<int> P(N); std::vector<std::vector<int>> tree(N); for (int i = 1; i < N; ++i) { std::cin >> P[i]; --P[i]; tree[P[i]].push_back(i); } std::vector<int> U(M), D(M); std::vector<i64> W(M); std::vector<Vertice> vData(N); for (int i = 0; i < M; ++i) { std::cin >> U[i] >> D[i] >> W[i]; --U[i]; } { // press auto cD = D; std::sort(cD.begin(), cD.end()); cD.erase(std::unique(cD.begin(), cD.end()), cD.end()); for (auto &e : D) { e = (int)(std::lower_bound(cD.begin(), cD.end(), e) - cD.begin()); } K = (int)cD.size(); } for (int i = 0; i < M; ++i) { vData[U[i]] = {true, D[i], W[i]}; } auto merge = [&] (std::map<int, i64> &a, std::map<int, i64> &b) { if (a.size() < b.size()) { std::swap(a, b); } for (const auto &[k, v] : b) { a[k] += v; } }; auto dfs = [&](auto &&self, const int v) -> std::map<int, i64> { // calc DP std::map<int, i64> ret; for (const int t : tree[v]) { auto res = self(self, t); merge(ret, res); } // cut if (vData[v].exFruit) { ret[vData[v].day] += vData[v].weight; auto itr = ret.upper_bound(vData[v].day); i64 sum = vData[v].weight; while (itr != ret.end() and sum != 0) { if (itr->second <= sum) { sum -= itr->second; itr = ret.erase(itr); } else { itr->second -= sum; sum = 0; } } } return ret; }; const auto res = dfs(dfs, 0); i64 answer = 0; for (const auto &[k, v] : res) { answer += v; } std::cout << answer << std::endl; }
#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...