Submission #1086464

# Submission time Handle Problem Language Result Execution time Memory
1086464 2024-09-10T17:33:03 Z vjudge1 Fireworks (APIO16_fireworks) C++17
100 / 100
203 ms 102724 KB
#include <iostream>
#include <map>
#include <utility>
#include <vector>

struct ConvexLinearSet {
  int m = 0;
  long long c = 0;
  std::map<long long, int> m_changes; // key: xpos, val: dm

  int size() {
    return m_changes.size();
  }
  
  void merge_with(const ConvexLinearSet& o) {
    m += o.m;
    c += o.c;
    for (auto [x, dm]: o.m_changes) {
      m_changes[x] += dm;
    }
  }

  long long query_min() {
    long long x;
    int dm;
    auto it = m_changes.rbegin();
    while (it != m_changes.rend() && m >= 0) {
      std::tie(x, dm) = *it;

      // update current m & c
      // m*x + c = (m - dm)*x + new_c -> new_c = x*dm + c
      m -= dm;
      c += x * dm;
      it++;
    }

    return x * m + c;
  }

  void apply_w(int w) {
    long long x, x_prev, xprev2;
    int dm;
    while (!m_changes.empty() && m >= 0) {
      // pop largest
      x_prev = x;
      std::tie(x, dm) = *m_changes.rbegin();
      m_changes.erase(x);

      // update current m & c
      // m*x + c = (m - dm)*x + new_c -> new_c = x*dm + c
      m -= dm;
      c += x * dm;
    }

    bool has_zero_grad = (m == -dm);
    long long minval = x * m + c;
    // std::cerr << "minimum at m=" << m << " x=" << x << " val=" << minval << std::endl;

    if (m != -1) {
      m_changes[x] -= m + 1;
    }

    m_changes[x + w]++; // make next grad = 0 here

    // we are at m = 0 now, let's jump to the end of m = 0
    x = has_zero_grad ? x_prev : x;
    x += w;

    // make next_m = 1
    // const = x + new_c -> new_c = const - x
    m_changes[x]++;
    m = 1;
    c = minval - x;
  }

  void debug() {
    std::cerr << "{\n";
    int cur_m = m;
    long long cur_c = c;
    for (auto it = m_changes.rbegin(); it != m_changes.rend(); it++) {
      long long x;
      int dm;
      std::tie(x, dm) = *it;
      std::cerr << "  y = (" << cur_m << ")x + (" << cur_c << ") from x >= " << x << '\n';
      cur_m -= dm;
      cur_c += x * dm;
    }
    std::cerr << "  y = (" << cur_m << ")x + (" << cur_c << ") otherwise\n";
    std::cerr << "}\n";

    std::cerr << "changes:"; for (auto [x, dm]: m_changes) std::cerr << " (x=" << x << ",d=" << dm << ")"; std::cerr << std::endl;
  }
};

int main() {
  std::cin.tie(NULL)->sync_with_stdio(false);

  int n, m;
  std::cin >> n >> m;

  std::vector<ConvexLinearSet> sets(n + m + 1);
  std::vector<std::vector<int>> adj(n + m + 1);
  std::vector<int> w(n + m + 1);
  w[1] = 0;
  for (int i = 2; i <= n + m; i++) {
    int par;
    std::cin >> par >> w[i];

    adj[par].push_back(i);
    if (i > n) {
      // leaf nodes
      sets[i].m = 1;
      sets[i].c = -w[i];
      sets[i].m_changes[w[i]] = 2;
    }
  }

  auto dfs = [&](auto &&dfs_fn, int u) -> void {
    if (adj[u].empty()) {
      // base case
      // std::cerr << "node=" << u << " w=" << w[u] << " ";
      // sets[u].debug();
      // std::cerr << std::endl;
      return;
    }

    // recursive + find largest set
    for (int v: adj[u]) {
      dfs_fn(dfs_fn, v);
      if (sets[u].size() < sets[v].size()) {
        std::swap(sets[u], sets[v]);
      }
    }

    // merge sets
    for (int v: adj[u]) {
      sets[u].merge_with(sets[v]);
    }

    sets[u].apply_w(w[u]);

    // std::cerr << "node=" << u << " w=" << w[u] << " ";
    // sets[u].debug();
    // std::cerr << std::endl;
  };

  dfs(dfs, 1);

  std::cout << sets[1].query_min() << std::endl;

  return 0;
}

Compilation message

fireworks.cpp: In member function 'void ConvexLinearSet::apply_w(int)':
fireworks.cpp:41:26: warning: unused variable 'xprev2' [-Wunused-variable]
   41 |     long long x, x_prev, xprev2;
      |                          ^~~~~~
fireworks.cpp:45:14: warning: 'x' is used uninitialized in this function [-Wuninitialized]
   45 |       x_prev = x;
      |       ~~~~~~~^~~
fireworks.cpp:55:32: warning: 'dm' may be used uninitialized in this function [-Wmaybe-uninitialized]
   55 |     bool has_zero_grad = (m == -dm);
      |                                ^~~
fireworks.cpp:67:7: warning: 'x_prev' may be used uninitialized in this function [-Wmaybe-uninitialized]
   67 |     x += w;
      |     ~~^~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:37:14: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   37 |     return x * m + c;
      |            ~~^~~
fireworks.cpp:24:15: note: 'x' was declared here
   24 |     long long x;
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 2 ms 856 KB Output is correct
35 Correct 2 ms 1116 KB Output is correct
36 Correct 2 ms 1116 KB Output is correct
37 Correct 3 ms 1188 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 2 ms 1372 KB Output is correct
40 Correct 2 ms 2140 KB Output is correct
41 Correct 2 ms 2140 KB Output is correct
42 Correct 2 ms 1116 KB Output is correct
43 Correct 2 ms 1628 KB Output is correct
44 Correct 2 ms 1628 KB Output is correct
45 Correct 2 ms 1628 KB Output is correct
46 Correct 3 ms 1628 KB Output is correct
47 Correct 2 ms 1372 KB Output is correct
48 Correct 2 ms 1372 KB Output is correct
49 Correct 2 ms 1372 KB Output is correct
50 Correct 2 ms 1372 KB Output is correct
51 Correct 2 ms 1372 KB Output is correct
52 Correct 2 ms 1368 KB Output is correct
53 Correct 2 ms 1368 KB Output is correct
54 Correct 3 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 2 ms 856 KB Output is correct
35 Correct 2 ms 1116 KB Output is correct
36 Correct 2 ms 1116 KB Output is correct
37 Correct 3 ms 1188 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 2 ms 1372 KB Output is correct
40 Correct 2 ms 2140 KB Output is correct
41 Correct 2 ms 2140 KB Output is correct
42 Correct 2 ms 1116 KB Output is correct
43 Correct 2 ms 1628 KB Output is correct
44 Correct 2 ms 1628 KB Output is correct
45 Correct 2 ms 1628 KB Output is correct
46 Correct 3 ms 1628 KB Output is correct
47 Correct 2 ms 1372 KB Output is correct
48 Correct 2 ms 1372 KB Output is correct
49 Correct 2 ms 1372 KB Output is correct
50 Correct 2 ms 1372 KB Output is correct
51 Correct 2 ms 1372 KB Output is correct
52 Correct 2 ms 1368 KB Output is correct
53 Correct 2 ms 1368 KB Output is correct
54 Correct 3 ms 1628 KB Output is correct
55 Correct 8 ms 2904 KB Output is correct
56 Correct 29 ms 10576 KB Output is correct
57 Correct 45 ms 18000 KB Output is correct
58 Correct 60 ms 23124 KB Output is correct
59 Correct 109 ms 30620 KB Output is correct
60 Correct 114 ms 38480 KB Output is correct
61 Correct 123 ms 43584 KB Output is correct
62 Correct 155 ms 48468 KB Output is correct
63 Correct 180 ms 58448 KB Output is correct
64 Correct 203 ms 60480 KB Output is correct
65 Correct 105 ms 102724 KB Output is correct
66 Correct 104 ms 102708 KB Output is correct
67 Correct 104 ms 41808 KB Output is correct
68 Correct 140 ms 81496 KB Output is correct
69 Correct 175 ms 76728 KB Output is correct
70 Correct 158 ms 76884 KB Output is correct
71 Correct 178 ms 79200 KB Output is correct
72 Correct 172 ms 79084 KB Output is correct
73 Correct 160 ms 70480 KB Output is correct
74 Correct 158 ms 70404 KB Output is correct
75 Correct 159 ms 69744 KB Output is correct
76 Correct 153 ms 70084 KB Output is correct
77 Correct 185 ms 68172 KB Output is correct
78 Correct 162 ms 66072 KB Output is correct
79 Correct 164 ms 69520 KB Output is correct