Submission #367890

#TimeUsernameProblemLanguageResultExecution timeMemory
367890AriaHFireworks (APIO16_fireworks)C++11
7 / 100
6 ms7424 KiB
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define SZ(x) (int) x.size() #define F first #define S second #define all(x) x.begin(), x.end() #define Mp make_pair const int N = 3e5 + 10; const ll inf = 1e18; ll res; int n, m; vector<pll> adj[N]; ll psL[N], psR[N], cntl[N], cntr[N]; pair<ll, ll> DFS(int v) { if (!SZ(adj[v])) return {0, 0}; vector < pll > seg; int cnt = 0; for (pll u : adj[v]) { psL[cnt] = 0; psR[cnt] = 0; cntl[cnt] = 0; cntr[cnt] = 0; cnt ++; pll x = DFS(u.F); x.F += u.S, x.S += u.S; seg.push_back(Mp(x.F, 0)); seg.push_back(Mp(x.S, 1)); } psL[cnt] = psR[cnt] = cntl[cnt] = cntr[cnt] = 0; sort(all(seg)); for(int i = 0; i < (int)seg.size(); i ++) { if(i) cntl[i] = cntl[i - 1], psL[i] = psL[i - 1] + cntl[i - 1] * (seg[i].F - seg[i - 1].F); if(seg[i].S == 1) { cntl[i] ++; } } for(int i = (int)seg.size() - 1; ~i; i --) { if(i + 1 < (int)seg.size()) { cntr[i] = cntr[i + 1]; psR[i] = psR[i + 1] + cntr[i + 1] * (seg[i + 1].F - seg[i].F); } if(seg[i].S == 0) cntr[i] ++; } ll mn = inf, l = inf, r = -inf; for(int i = 0; i < (int)seg.size(); i ++) { ll cu = psL[i] + psR[i]; if(cu < mn) { mn = cu; l = min(l, seg[i].F); r = max(r, seg[i].F); } } res += mn; return Mp(l, r); } int main() { scanf("%d%d", &n, &m); for (int i = 2; i <= n + m; i++) { int p, c; scanf("%d%d", &p, &c); adj[p].push_back({i, c}); } DFS(1); printf("%lld\n", res); return 0; }

Compilation message (stderr)

fireworks.cpp: In function 'int main()':
fireworks.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   71 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:73:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |         int p, c; 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...