Submission #869842

#TimeUsernameProblemLanguageResultExecution timeMemory
869842DylanSmithFireworks (APIO16_fireworks)C++17
100 / 100
145 ms45208 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x),end(x) #define lb(x,y) lower_bound(all(x),y)-begin(x) mt19937 rng; void solve() { int N, M; cin >> N >> M; vector<vector<int>> adj(N); vector<int> len(N + M, 0); for (int i = 1; i < N + M; i++) { int u; cin >> u; u--; adj[u].pb(i); cin >> len[i]; } if (M == 0) { cout << 0 << "\n"; return; } vector<priority_queue<ll>> x(N + M); vector<ll> y(N + M, 0); vector<ll> d(N + M, 0); for (int u = N + M - 1; u >= 0; u--) { if (u >= N) { x[u].push(len[u]); x[u].push(len[u]); d[u] = 1; } else { for (int v : adj[u]) { if (sz(x[v]) > sz(x[u])) { swap(x[u], x[v]); swap(y[u], y[v]); swap(d[u], d[v]); } } int slope = 1; for (int v : adj[u]) { if (x[v].empty()) continue; if (x[v].top() > x[u].top()) { y[u] += y[v] + d[u] * (x[v].top() - x[u].top()); } else { y[u] += y[v] + d[v] * (x[u].top() - x[v].top()); } while (!x[v].empty()) { x[u].push(x[v].top()); x[v].pop(); } d[u] += d[v]; } while (d[u] > 1) { d[u]--; ll n = x[u].top(); x[u].pop(); y[u] -= d[u] * (n - x[u].top()); } ll n2 = x[u].top(); x[u].pop(); ll n1 = x[u].top(); x[u].pop(); n1 += len[u]; n2 += len[u]; x[u].push(n1); x[u].push(n2); } } cout << y[0] << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); rng = mt19937(chrono::steady_clock::now().time_since_epoch().count()); solve(); return 0; }

Compilation message (stderr)

fireworks.cpp: In function 'void solve()':
fireworks.cpp:43:17: warning: unused variable 'slope' [-Wunused-variable]
   43 |             int slope = 1;
      |                 ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...