Submission #226709

# Submission time Handle Problem Language Result Execution time Memory
226709 2020-04-25T01:29:29 Z natsukagami Fireworks (APIO16_fireworks) C++17
100 / 100
231 ms 48856 KB
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<ll, int> ii;
const int MAXN = 3e5 + 5;
struct node {
  int total;
  ll off;
  priority_queue<ii> q;
  node() { total = off = 0; }
  node(node &&v) noexcept : total(v.total), off(v.off), q(std::move(v.q)) {}
  node &operator=(node &&v) {
    total = v.total;
    off = v.off;
    q = std::move(v.q);
    return *this;
  }
} V[MAXN];

void merge(node &a, node &b) {
  // merge a <- b
  if (a.q.size() < b.q.size())
    swap(a, b); // small to large
  a.total += b.total;
  a.off += b.off;
  while (b.q.size()) {
    a.q.push(b.q.top());
    b.q.pop();
  }
}
void opt(node &a, int k) {
  // optimize with k
  a.off += k;
  ll mx, mn;
  while (a.total > 0) {
    mx = mn = a.q.top().x;
    a.total -= a.q.top().y;
    a.q.pop();
  }
  while (a.total == 0) {
    a.total -= a.q.top().y;
    mn = a.q.top().x;
    a.q.pop();
  }
  // cout << "ck " << mn << ' ' << mx << endl;
  while (a.q.size() && a.q.top().x >= mn) {
    a.total -= a.q.top().y;
    a.q.pop();
  }
  // raise total to -1
  a.q.push(ii(mn, -1 - a.total));
  // push 0 in
  a.q.push(ii(mn + k, 1));
  // push 1 in
  a.q.push(ii(mx + k, 1));
  // fix total
  a.total = 1;
}
int N, M;
int par[MAXN], edge[MAXN], deg[MAXN];
// random_device rd;
int main() {
  ios_base::sync_with_stdio(false);
  // N = M = 1e5;
  cin >> N >> M;
  for (int i = 2; i <= N + M; ++i) {
    // par[i] = rd()%(i-1) + 1;
    // edge[i] = rd()%int(1e9) + 1;
    cin >> par[i] >> edge[i];
    ++deg[par[i]];
  }
  for (int i = N + M; i >= 1; --i) {
    // cout << i << endl;
    if (!deg[i]) {
      // i is leaf
      V[i].total = 1;
      V[i].off = edge[i];
      V[i].q.push(ii(edge[i], 2));
    } else {
      opt(V[i], edge[i]);
    }
    // cout << "v = " << i << ":" << endl;
    // cout << V[i].off << ' ' << V[i].total << endl << "qq:" << endl;
    // auto qq = V[i].q;
    // while (qq.size()) {
    // 	cout << qq.top().x << ' ' << qq.top().y << endl;
    // 	qq.pop();
    // }
    if (i > 1)
      merge(V[par[i]], V[i]);
  }
  vector<ii> P;
  while (V[1].q.size()) {
    P.push_back(V[1].q.top());
    V[1].total -= V[1].q.top().y;
    V[1].q.pop();
  }
  reverse(P.begin(), P.end());
  ll ans = V[1].off, last = 0, cur = V[1].off;
  for (auto i : P) {
    // cout << i.x << ' ' << i.y << ' ' << V[1].total << ' ' << cur << endl;
    cur += (i.x - last) * V[1].total;
    ans = min(ans, cur);
    V[1].total += i.y;
    last = i.x;
  }
  cout << ans << endl;
}

Compilation message

fireworks.cpp: In function 'void opt(node&, int)':
fireworks.cpp:48:21: warning: 'mn' may be used uninitialized in this function [-Wmaybe-uninitialized]
   while (a.q.size() && a.q.top().x >= mn) {
          ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
fireworks.cpp:57:18: warning: 'mx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   a.q.push(ii(mx + k, 1));
               ~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 12 ms 14464 KB Output is correct
5 Correct 12 ms 14336 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 12 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 12 ms 14464 KB Output is correct
5 Correct 12 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 12 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 12 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 13 ms 14464 KB Output is correct
13 Correct 12 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 12 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 12 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 12 ms 14464 KB Output is correct
5 Correct 12 ms 14336 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 12 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 12 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 12 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 12 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 13 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 12 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 12 ms 14464 KB Output is correct
27 Correct 12 ms 14464 KB Output is correct
28 Correct 12 ms 14464 KB Output is correct
29 Correct 12 ms 14464 KB Output is correct
30 Correct 12 ms 14464 KB Output is correct
31 Correct 13 ms 14592 KB Output is correct
32 Correct 13 ms 14592 KB Output is correct
33 Correct 14 ms 14720 KB Output is correct
34 Correct 14 ms 14720 KB Output is correct
35 Correct 15 ms 14848 KB Output is correct
36 Correct 15 ms 14868 KB Output is correct
37 Correct 15 ms 14976 KB Output is correct
38 Correct 16 ms 15104 KB Output is correct
39 Correct 15 ms 14976 KB Output is correct
40 Correct 15 ms 14848 KB Output is correct
41 Correct 15 ms 14720 KB Output is correct
42 Correct 15 ms 14848 KB Output is correct
43 Correct 14 ms 14720 KB Output is correct
44 Correct 15 ms 14848 KB Output is correct
45 Correct 17 ms 14848 KB Output is correct
46 Correct 16 ms 14976 KB Output is correct
47 Correct 15 ms 14976 KB Output is correct
48 Correct 14 ms 14848 KB Output is correct
49 Correct 15 ms 14976 KB Output is correct
50 Correct 15 ms 14848 KB Output is correct
51 Correct 14 ms 14848 KB Output is correct
52 Correct 15 ms 14976 KB Output is correct
53 Correct 14 ms 14848 KB Output is correct
54 Correct 14 ms 14848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 12 ms 14464 KB Output is correct
5 Correct 12 ms 14336 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 12 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 12 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 12 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 12 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 13 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 12 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 12 ms 14464 KB Output is correct
27 Correct 12 ms 14464 KB Output is correct
28 Correct 12 ms 14464 KB Output is correct
29 Correct 12 ms 14464 KB Output is correct
30 Correct 12 ms 14464 KB Output is correct
31 Correct 13 ms 14592 KB Output is correct
32 Correct 13 ms 14592 KB Output is correct
33 Correct 14 ms 14720 KB Output is correct
34 Correct 14 ms 14720 KB Output is correct
35 Correct 15 ms 14848 KB Output is correct
36 Correct 15 ms 14868 KB Output is correct
37 Correct 15 ms 14976 KB Output is correct
38 Correct 16 ms 15104 KB Output is correct
39 Correct 15 ms 14976 KB Output is correct
40 Correct 15 ms 14848 KB Output is correct
41 Correct 15 ms 14720 KB Output is correct
42 Correct 15 ms 14848 KB Output is correct
43 Correct 14 ms 14720 KB Output is correct
44 Correct 15 ms 14848 KB Output is correct
45 Correct 17 ms 14848 KB Output is correct
46 Correct 16 ms 14976 KB Output is correct
47 Correct 15 ms 14976 KB Output is correct
48 Correct 14 ms 14848 KB Output is correct
49 Correct 15 ms 14976 KB Output is correct
50 Correct 15 ms 14848 KB Output is correct
51 Correct 14 ms 14848 KB Output is correct
52 Correct 15 ms 14976 KB Output is correct
53 Correct 14 ms 14848 KB Output is correct
54 Correct 14 ms 14848 KB Output is correct
55 Correct 21 ms 15872 KB Output is correct
56 Correct 46 ms 20080 KB Output is correct
57 Correct 71 ms 24552 KB Output is correct
58 Correct 91 ms 27180 KB Output is correct
59 Correct 116 ms 31972 KB Output is correct
60 Correct 153 ms 36512 KB Output is correct
61 Correct 168 ms 38756 KB Output is correct
62 Correct 186 ms 41308 KB Output is correct
63 Correct 227 ms 46736 KB Output is correct
64 Correct 231 ms 47588 KB Output is correct
65 Correct 203 ms 39892 KB Output is correct
66 Correct 207 ms 31200 KB Output is correct
67 Correct 211 ms 41936 KB Output is correct
68 Correct 176 ms 31080 KB Output is correct
69 Correct 197 ms 33380 KB Output is correct
70 Correct 191 ms 33356 KB Output is correct
71 Correct 186 ms 48448 KB Output is correct
72 Correct 187 ms 48856 KB Output is correct
73 Correct 166 ms 44800 KB Output is correct
74 Correct 176 ms 45184 KB Output is correct
75 Correct 169 ms 43620 KB Output is correct
76 Correct 176 ms 43912 KB Output is correct
77 Correct 178 ms 43484 KB Output is correct
78 Correct 174 ms 42720 KB Output is correct
79 Correct 159 ms 40784 KB Output is correct