Submission #792718

#TimeUsernameProblemLanguageResultExecution timeMemory
792718IvanJFireworks (APIO16_fireworks)C++17
7 / 100
5 ms7380 KiB
#include<bits/stdc++.h> #define pb push_back #define x first #define y second #define all(a) (a).begin(), (a).end() using namespace std; typedef long long ll; typedef pair<int, int> ii; const int maxn = 3e5 + 5; struct Data { int idx; ll lo, hi, ans; }; int n, m; ll depth[maxn]; Data dp[maxn]; vector<pair<int, ll>> adj[maxn]; void dfs(int x) { for(auto p : adj[x]) depth[p.x] = p.y + depth[x], dfs(p.x); } Data solve(int x) { if(adj[x].size() == 0) return dp[x] = {x, depth[x], depth[x], 0}; vector<Data> ch; for(auto p : adj[x]) ch.pb(solve(p.x)); int sz = 0; ll sum_l = 0, sum_r = 0; int cnt_l = 0, cnt_r = 0; for(Data D : ch) sum_r += D.lo, sz++, cnt_r++; map<ll, vector<int>> L, R; set<ll> vals; ll sum = 0; for(Data D : ch) { sum += D.ans; L[D.lo].pb(D.idx); R[D.hi].pb(D.idx); vals.insert(D.lo), vals.insert(D.hi); } vector<ll> v; ll ans = 1e18; for(ll val : vals) { for(int y : L[val]) sum_r -= dp[y].lo, cnt_r--; ll ansi = sum_r - sum_l + (ll)(cnt_l - cnt_r) * val; if(abs(cnt_r - cnt_l) <= sz - cnt_r - cnt_l) { if(ansi < ans) v.clear(), ans = ansi; if(ansi == ans) v.pb(val); } for(int y : R[val]) sum_l += dp[y].hi, cnt_l++; } Data ret; ret.idx = x; ret.ans = ans + sum; ret.lo = v[0]; ret.hi = v.back(); //cout << x + 1 << " -> " << ret.lo << " " << ret.hi << " " << ret.ans << "\n"; return dp[x] = ret; } int main() { scanf("%d%d", &n, &m); for(int i = 1;i < n + m;i++) { int p, c; scanf("%d%d", &p, &c); p--; adj[p].pb({i, c}); } dfs(0); Data ans = solve(0); printf("%lld\n", ans.ans); return 0; }

Compilation message (stderr)

fireworks.cpp: In function 'int main()':
fireworks.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |   scanf("%d%d", &p, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...