Submission #725220

#TimeUsernameProblemLanguageResultExecution timeMemory
725220pavementMagic Tree (CEOI19_magictree)C++17
50 / 100
2087 ms53312 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long #define mp make_pair #define mt make_tuple #define pb push_back #define ppb pop_back #define eb emplace_back #define g0(a) get<0>(a) #define g1(a) get<1>(a) #define g2(a) get<2>(a) #define g3(a) get<3>(a) #define g4(a) get<4>(a) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); using db = double; using ll = long long; using ld = long double; using ii = pair<int, int>; using iii = tuple<int, int, int>; using iiii = tuple<int, int, int, int>; using iiiii = tuple<int, int, int, int, int>; template<class key, class value = null_type, class cmp = less<key> > using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>; int n, m, k, ans; vector<int> adj[100005]; vector<ii> vec[100005]; map<int, int> M[100005], mpp[100005]; void dp(int u) { for (auto v : adj[u]) { dp(v); if (M[u].size() < M[v].size()) swap(M[u], M[v]); for (auto i : M[v]) M[u][i.first] += i.second; } int cur = 0; vector<int> eval; auto it = M[u].begin(); for (auto [t, val] : vec[u]) { while (it != M[u].end() && it->first <= t) { cur += val; ++it; } eval.pb(cur); } reverse(eval.begin(), eval.end()); reverse(vec[u].begin(), vec[u].end()); for (int i = 0; i < (int)vec[u].size(); i++) { vector<map<int, int>::iterator> to_del; vector<ii> to_add; auto [t, val] = vec[u][i]; M[u][t] += val; auto it = M[u].find(t); ++it; int sf = 0; while (it != M[u].end()) { if (sf + it->second <= val) { to_del.pb(it); sf += it->second; } else { to_del.pb(it); to_add.eb(it->first, sf + it->second - val); break; } ++it; } for (auto it : to_del) M[u].erase(it); for (auto j : to_add) M[u][j.first] += j.second; } //~ cout << "NODE " << u << '\n'; //~ for (auto [a, b] : M[u]) cout << a << ' ' << b << '\n'; } main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 2, p; i <= n; i++) { cin >> p; adj[p].pb(i); } for (int i = 1, v, d, w; i <= m; i++) { cin >> v >> d >> w; mpp[v][d] += w; } for (int i = 1; i <= n; i++) { for (auto j : mpp[i]) vec[i].pb(j); } dp(1); for (auto i : M[1]) ans += i.second; cout << ans << '\n'; }

Compilation message (stderr)

magictree.cpp:77:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   77 | main() {
      | ^~~~
#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...