Submission #262850

# Submission time Handle Problem Language Result Execution time Memory
262850 2020-08-13T10:04:34 Z KoD Fireworks (APIO16_fireworks) C++11
55 / 100
1338 ms 524292 KB
/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#include <iomanip>


template <class T, class U>
bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

template <class T, class U>
bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

/**
 * @title Chmin/Chmax
 */


class range {
public:
  class iterator {
  private:
    int64_t M_position;

  public:
    iterator(int64_t position) noexcept: M_position(position) { }
    void operator ++ () noexcept { ++M_position; }
    bool operator != (iterator other) const noexcept { return M_position != other.M_position; }
    int64_t operator * () const noexcept { return M_position; }

  };

  class reverse_iterator {
  private:
    int64_t M_position;
  
  public:
    reverse_iterator(int64_t position) noexcept: M_position(position) { }
    void operator ++ () noexcept { --M_position; }
    bool operator != (reverse_iterator other) const noexcept { return M_position != other.M_position; }
    int64_t operator * () const noexcept { return M_position; }

  };
  
private:
  const iterator M_first, M_last;

public:
  range(int64_t first, int64_t last) noexcept: M_first(first), M_last(std::max(first, last)) { }
  iterator begin() const noexcept { return M_first; }
  iterator end() const noexcept { return M_last; }
  reverse_iterator rbegin() const noexcept { return reverse_iterator(*M_last - 1); } 
  reverse_iterator rend() const noexcept { return reverse_iterator(*M_first - 1); } 

};

/**
 * @title Range
 */

#include <type_traits>
#include <iterator>

template <class T>
class rev_impl {
public:
  using iterator = decltype(std::declval<T>().rbegin());

private:
  const iterator M_begin;
  const iterator M_end;

public:
  rev_impl(T &&cont) noexcept: M_begin(cont.rbegin()), M_end(cont.rend()) { }
  iterator begin() const noexcept { return M_begin; }
  iterator end() const noexcept { return M_end; }

};

template <class T>
rev_impl<T> rev(T &&cont) {
  return rev_impl<T>(std::forward<T>(cont));
}

/**
 * @title Reverser
 */

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

constexpr size_t MAX = 300000;

i32 N, M, V;
std::array<i32, MAX> P, C;

struct status {
  i64 x, inc;
};

std::array<std::vector<status>, MAX> data;
std::array<std::vector<i32>, MAX> child;

void dfs(const i32 u) {
  if (u >= N) {
    data[u].push_back(status{ 0, -1 });
    data[u].push_back(status{ C[u], 2 });
    return;
  }
  for (auto v: child[u]) {
    dfs(v);
    const auto cur = data[u].size();
    data[u].resize(cur + data[v].size());
    std::copy(data[v].begin(), data[v].end(), data[u].begin() + cur);
    std::inplace_merge(data[u].begin(), data[u].begin() + cur, data[u].end(), [](status s, status t) {
      return s.x < t.x; 
    });
    data[v].clear();
  }
  std::vector<status> next;
  i64 sum = 0;
  for (auto i: range(0, data[u].size())) {
    sum += data[u][i].inc;
    if (i + 1 == data[u].size() || data[u][i + 1].x != data[u][i].x) {
      next.push_back(status{ data[u][i].x, sum });
      sum = 0;
    }
  }
  data[u].swap(next);
  if (C[u] == 0) {
    return;
  }
  next.clear();
  next.push_back(data[u][0]);
  sum = data[u][0].inc;
  for (auto i: range(1, data[u].size())) {
    if (sum + data[u][i].inc == 0) {
      if (-1 - sum != 0) next.push_back(status{ data[u][i].x, -1 - sum });
      next.push_back(status{ data[u][i].x + C[u], 1 });
      next.push_back(status{ data[u][i + 1].x + C[u], 1 });
      break;
    }
    if (sum + data[u][i].inc > 0) {
      if (-1 - sum != 0) next.push_back(status{ data[u][i].x, -1 - sum });
      next.push_back(status{ data[u][i].x + C[u], 2 });
      break;
    }
    sum += data[u][i].inc;
    next.push_back(data[u][i]);
  }
  data[u] = std::move(next);
}

int main() {
  std::cin >> N >> M;
  V = N + M;
  P[0] = -1;
  for (auto i: range(1, V)) {
    std::cin >> P[i] >> C[i];
    --P[i];
    child[P[i]].push_back(i);
  }
  dfs(0);
  i64 sum = 0;
  for (auto i: range(1, V)) {
    sum += C[i];
  }
  i64 slope = data[0][0].inc;
  i64 last = 0;
  for (auto i: range(1, data[0].size())) {
    sum += slope * (data[0][i].x - last);
    slope += data[0][i].inc;
    last = data[0][i].x;
    if (slope >= 0) {
      break;
    }
  }
  std::cout << sum << '\n';
  return 0;
}

Compilation message

fireworks.cpp: In function 'void dfs(i32)':
fireworks.cpp:149:15: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<status>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |     if (i + 1 == data[u].size() || data[u][i + 1].x != data[u][i].x) {
      |         ~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 10 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 10 ms 14464 KB Output is correct
10 Correct 11 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 10 ms 14464 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 10 ms 14464 KB Output is correct
8 Correct 10 ms 14464 KB Output is correct
9 Correct 10 ms 14464 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 10 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 10 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 10 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 10 ms 14464 KB Output is correct
10 Correct 11 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 11 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 10 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 10 ms 14464 KB Output is correct
22 Correct 10 ms 14464 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14464 KB Output is correct
25 Correct 10 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 11 ms 14464 KB Output is correct
31 Correct 12 ms 14848 KB Output is correct
32 Correct 13 ms 14860 KB Output is correct
33 Correct 15 ms 15104 KB Output is correct
34 Correct 16 ms 15488 KB Output is correct
35 Correct 19 ms 16384 KB Output is correct
36 Correct 19 ms 16256 KB Output is correct
37 Correct 21 ms 16128 KB Output is correct
38 Correct 24 ms 18432 KB Output is correct
39 Correct 22 ms 16640 KB Output is correct
40 Correct 18 ms 15744 KB Output is correct
41 Correct 18 ms 15744 KB Output is correct
42 Correct 18 ms 14848 KB Output is correct
43 Correct 76 ms 62968 KB Output is correct
44 Correct 71 ms 53496 KB Output is correct
45 Correct 62 ms 52088 KB Output is correct
46 Correct 19 ms 15104 KB Output is correct
47 Correct 18 ms 15104 KB Output is correct
48 Correct 20 ms 14976 KB Output is correct
49 Correct 21 ms 14976 KB Output is correct
50 Correct 24 ms 14976 KB Output is correct
51 Correct 27 ms 14968 KB Output is correct
52 Correct 23 ms 15608 KB Output is correct
53 Correct 23 ms 15100 KB Output is correct
54 Correct 29 ms 15104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 10 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 10 ms 14464 KB Output is correct
10 Correct 11 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 11 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 10 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 10 ms 14464 KB Output is correct
22 Correct 10 ms 14464 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14464 KB Output is correct
25 Correct 10 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 11 ms 14464 KB Output is correct
31 Correct 12 ms 14848 KB Output is correct
32 Correct 13 ms 14860 KB Output is correct
33 Correct 15 ms 15104 KB Output is correct
34 Correct 16 ms 15488 KB Output is correct
35 Correct 19 ms 16384 KB Output is correct
36 Correct 19 ms 16256 KB Output is correct
37 Correct 21 ms 16128 KB Output is correct
38 Correct 24 ms 18432 KB Output is correct
39 Correct 22 ms 16640 KB Output is correct
40 Correct 18 ms 15744 KB Output is correct
41 Correct 18 ms 15744 KB Output is correct
42 Correct 18 ms 14848 KB Output is correct
43 Correct 76 ms 62968 KB Output is correct
44 Correct 71 ms 53496 KB Output is correct
45 Correct 62 ms 52088 KB Output is correct
46 Correct 19 ms 15104 KB Output is correct
47 Correct 18 ms 15104 KB Output is correct
48 Correct 20 ms 14976 KB Output is correct
49 Correct 21 ms 14976 KB Output is correct
50 Correct 24 ms 14976 KB Output is correct
51 Correct 27 ms 14968 KB Output is correct
52 Correct 23 ms 15608 KB Output is correct
53 Correct 23 ms 15100 KB Output is correct
54 Correct 29 ms 15104 KB Output is correct
55 Correct 47 ms 23288 KB Output is correct
56 Correct 217 ms 83260 KB Output is correct
57 Correct 418 ms 152852 KB Output is correct
58 Correct 579 ms 177236 KB Output is correct
59 Correct 888 ms 374060 KB Output is correct
60 Correct 956 ms 374720 KB Output is correct
61 Correct 1195 ms 482964 KB Output is correct
62 Correct 1292 ms 508032 KB Output is correct
63 Runtime error 1338 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
64 Halted 0 ms 0 KB -