Submission #939353

#TimeUsernameProblemLanguageResultExecution timeMemory
939353AlcabelFireworks (APIO16_fireworks)C++17
7 / 100
10 ms32348 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5; priority_queue<long long> down[maxn]; priority_queue<long long, vector<long long>, greater<long long>> up[maxn]; long long val[maxn], modUp[maxn]; int slope[maxn]; vector<int> g[maxn]; int par[maxn], c[maxn]; /*void print(int v) { vector<long long> res; { auto tmp = down[v]; while (!tmp.empty()) { res.emplace_back(tmp.top()); tmp.pop(); } reverse(res.begin(), res.end()); } { auto tmp = up[v]; while (!tmp.empty()) { res.emplace_back(tmp.top() + modUp[v]); tmp.pop(); } for (const int& x : res) { cerr << x << ' '; } cerr << '\n'; } }*/ void dfs(int v) { if (g[v].empty()) { val[v] = c[v]; slope[v] = -1; up[v].emplace(c[v]); up[v].emplace(c[v]); return; } for (const int& u : g[v]) { dfs(u); if (down[v].size() + up[v].size() < down[u].size() + up[u].size()) { swap(down[v], down[u]); swap(up[v], up[u]); swap(val[v], val[u]); swap(modUp[v], modUp[u]); swap(slope[v], slope[u]); } slope[v] += slope[u]; val[v] += val[u]; while (!down[u].empty()) { down[v].emplace(down[u].top()); down[u].pop(); } while (!up[u].empty()) { up[v].emplace(up[u].top() + modUp[u] - modUp[v]); up[u].pop(); } } if (!up[v].empty()) { while (!down[v].empty() && down[v].top() > up[v].top() + modUp[v]) { up[v].emplace(down[v].top() - modUp[v]); down[v].pop(); } } while (slope[v] + (int)down[v].size() > -1) { up[v].emplace(down[v].top() - modUp[v]); down[v].pop(); } while (slope[v] + (int)down[v].size() < -1) { down[v].emplace(up[v].top() + modUp[v]); up[v].pop(); } // cerr << "v: " << v + 1 << " before:\n"; // print(v); if (par[v] != -1) { val[v] += c[v]; modUp[v] += c[v]; down[v].emplace(up[v].top() + modUp[v]); up[v].pop(); down[v].emplace(up[v].top() + modUp[v]); up[v].pop(); modUp[v] += c[v]; } // cerr << "v: " << v + 1 << ", val: " << val[v] << ", slope: " << slope[v] << '\n'; // print(v); } void solve() { int n, m; cin >> n >> m; par[0] = -1, c[0] = 0; for (int i = 1; i < n + m; ++i) { cin >> par[i] >> c[i]; --par[i]; g[par[i]].emplace_back(i); } dfs(0); while (slope[0] + (int)down[0].size() < 0) { down[0].emplace(up[0].top() + modUp[0]); up[0].pop(); } while (slope[0] + (int)down[0].size() > 0) { up[0].emplace(down[0].top() - modUp[0]); down[0].pop(); } long long ans = val[0]; vector<long long> leftPart; while (!down[0].empty()) { leftPart.emplace_back(down[0].top()); down[0].pop(); } leftPart.emplace_back(0); reverse(leftPart.begin(), leftPart.end()); for (int i = 0; i < (int)leftPart.size() - 1; ++i) { ans += (slope[0] + i) * (leftPart[i + 1] - leftPart[i]); } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCALa freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); int T = 1; cin >> T; while (T--) { solve(); cerr << "-----------\n"; cout << "-----------\n"; } #else int T = 1; // cin >> T; while (T--) { solve(); } #endif 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...