답안 #1086474

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1086474 2024-09-10T17:51:15 Z vjudge1 Fireworks (APIO16_fireworks) C++17
100 / 100
207 ms 102740 KB
#include <iostream>
#include <map>
#include <tuple>
#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;
    }
  }

  std::tuple<long long, long long, int> pop_until_negative_gradient() {
    long long x, x_prev;
    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;
    }

    return {x_prev, x, dm};
  }

  long long query_min() {
    long long x, x_prev;
    int dm;
    std::tie(x_prev, x, dm) = pop_until_negative_gradient();
    return x * m + c;
  }

  void apply_w(int w) {
    long long x, x_prev;
    int dm;
    std::tie(x_prev, x, dm) = pop_until_negative_gradient();

    // x = leftmost xpos that make the minimum
    long long minval = x * m + c;

    if (m != -1) {
      m_changes[x] -= m + 1; // make gradient = -1 at x
    }
    m_changes[x + w]++; // make next gradient = 0 at x + w

    // we are at m = 0 now, jump to the end of m = 0
    x = m == -dm ? x_prev : x;
    x += w;

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

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;
    }
  }

  // idea: dp[u][x] = apply_w(sum(dp[v][x]), w[u])
  //      dp[leaf][x] = abs(x - w[leaf])
  // apply_w with convex linear func -> we know these two greedy logic:
  //   1. for gradient > 1, we can instead increase from w => we can have gradient=1 instead
  //   2. for gradient < 0, we can use up all w first => x..x+w will have gradient=-1
  // so at minimum point (mx, my), we can have line (mx, my + w) to (mx + c, my) and then (mx + c, my) to (last_mx + c, my), then gradient=1 afterwards
  // but why shift right? because we can think of all the base constant are initially moved by w, and later on we cut at minimum points and apply (2) to the left direction.

  auto dfs = [&](auto &&dfs_fn, int u) -> void {
    if (adj[u].empty()) {
      return;
    }

    // smaller to larger trick here
    // each leaf element will only be merged at most logN times
    for (int v: adj[u]) {
      dfs_fn(dfs_fn, v);
      if (sets[u].size() < sets[v].size()) {
        // find largest set
        std::swap(sets[u], sets[v]);
      }
    }

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

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

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

  return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 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 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 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 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 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 600 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 860 KB Output is correct
35 Correct 2 ms 1116 KB Output is correct
36 Correct 2 ms 1188 KB Output is correct
37 Correct 2 ms 1372 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 1120 KB Output is correct
43 Correct 3 ms 1624 KB Output is correct
44 Correct 3 ms 1624 KB Output is correct
45 Correct 2 ms 1628 KB Output is correct
46 Correct 2 ms 1624 KB Output is correct
47 Correct 3 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 1372 KB Output is correct
53 Correct 2 ms 1372 KB Output is correct
54 Correct 2 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 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 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 600 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 860 KB Output is correct
35 Correct 2 ms 1116 KB Output is correct
36 Correct 2 ms 1188 KB Output is correct
37 Correct 2 ms 1372 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 1120 KB Output is correct
43 Correct 3 ms 1624 KB Output is correct
44 Correct 3 ms 1624 KB Output is correct
45 Correct 2 ms 1628 KB Output is correct
46 Correct 2 ms 1624 KB Output is correct
47 Correct 3 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 1372 KB Output is correct
53 Correct 2 ms 1372 KB Output is correct
54 Correct 2 ms 1628 KB Output is correct
55 Correct 7 ms 2908 KB Output is correct
56 Correct 25 ms 10588 KB Output is correct
57 Correct 44 ms 18000 KB Output is correct
58 Correct 62 ms 23120 KB Output is correct
59 Correct 98 ms 30632 KB Output is correct
60 Correct 107 ms 38480 KB Output is correct
61 Correct 138 ms 43604 KB Output is correct
62 Correct 149 ms 48468 KB Output is correct
63 Correct 185 ms 58448 KB Output is correct
64 Correct 207 ms 60316 KB Output is correct
65 Correct 103 ms 102740 KB Output is correct
66 Correct 108 ms 102736 KB Output is correct
67 Correct 101 ms 41812 KB Output is correct
68 Correct 142 ms 81360 KB Output is correct
69 Correct 161 ms 76880 KB Output is correct
70 Correct 166 ms 76632 KB Output is correct
71 Correct 159 ms 79184 KB Output is correct
72 Correct 166 ms 79184 KB Output is correct
73 Correct 157 ms 70392 KB Output is correct
74 Correct 150 ms 70480 KB Output is correct
75 Correct 154 ms 69716 KB Output is correct
76 Correct 156 ms 69968 KB Output is correct
77 Correct 154 ms 68120 KB Output is correct
78 Correct 169 ms 66232 KB Output is correct
79 Correct 172 ms 69524 KB Output is correct