/**
* @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 = 5000;
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;
});
}
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:148:15: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<status>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | if (i + 1 == data[u].size() || data[u][i + 1].x != data[u][i].x) {
| ~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
544 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
544 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
640 KB |
Output is correct |
31 |
Correct |
3 ms |
1024 KB |
Output is correct |
32 |
Correct |
4 ms |
1024 KB |
Output is correct |
33 |
Correct |
5 ms |
1280 KB |
Output is correct |
34 |
Correct |
9 ms |
1664 KB |
Output is correct |
35 |
Correct |
10 ms |
2560 KB |
Output is correct |
36 |
Correct |
10 ms |
2432 KB |
Output is correct |
37 |
Correct |
14 ms |
2304 KB |
Output is correct |
38 |
Correct |
15 ms |
4736 KB |
Output is correct |
39 |
Correct |
13 ms |
2816 KB |
Output is correct |
40 |
Correct |
9 ms |
1792 KB |
Output is correct |
41 |
Correct |
9 ms |
1792 KB |
Output is correct |
42 |
Correct |
9 ms |
1024 KB |
Output is correct |
43 |
Correct |
67 ms |
49144 KB |
Output is correct |
44 |
Correct |
62 ms |
39800 KB |
Output is correct |
45 |
Correct |
55 ms |
38392 KB |
Output is correct |
46 |
Correct |
10 ms |
1280 KB |
Output is correct |
47 |
Correct |
9 ms |
1280 KB |
Output is correct |
48 |
Correct |
11 ms |
1152 KB |
Output is correct |
49 |
Correct |
13 ms |
1152 KB |
Output is correct |
50 |
Correct |
13 ms |
1152 KB |
Output is correct |
51 |
Correct |
13 ms |
1152 KB |
Output is correct |
52 |
Correct |
13 ms |
1920 KB |
Output is correct |
53 |
Correct |
11 ms |
1280 KB |
Output is correct |
54 |
Correct |
21 ms |
1396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
544 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
640 KB |
Output is correct |
31 |
Correct |
3 ms |
1024 KB |
Output is correct |
32 |
Correct |
4 ms |
1024 KB |
Output is correct |
33 |
Correct |
5 ms |
1280 KB |
Output is correct |
34 |
Correct |
9 ms |
1664 KB |
Output is correct |
35 |
Correct |
10 ms |
2560 KB |
Output is correct |
36 |
Correct |
10 ms |
2432 KB |
Output is correct |
37 |
Correct |
14 ms |
2304 KB |
Output is correct |
38 |
Correct |
15 ms |
4736 KB |
Output is correct |
39 |
Correct |
13 ms |
2816 KB |
Output is correct |
40 |
Correct |
9 ms |
1792 KB |
Output is correct |
41 |
Correct |
9 ms |
1792 KB |
Output is correct |
42 |
Correct |
9 ms |
1024 KB |
Output is correct |
43 |
Correct |
67 ms |
49144 KB |
Output is correct |
44 |
Correct |
62 ms |
39800 KB |
Output is correct |
45 |
Correct |
55 ms |
38392 KB |
Output is correct |
46 |
Correct |
10 ms |
1280 KB |
Output is correct |
47 |
Correct |
9 ms |
1280 KB |
Output is correct |
48 |
Correct |
11 ms |
1152 KB |
Output is correct |
49 |
Correct |
13 ms |
1152 KB |
Output is correct |
50 |
Correct |
13 ms |
1152 KB |
Output is correct |
51 |
Correct |
13 ms |
1152 KB |
Output is correct |
52 |
Correct |
13 ms |
1920 KB |
Output is correct |
53 |
Correct |
11 ms |
1280 KB |
Output is correct |
54 |
Correct |
21 ms |
1396 KB |
Output is correct |
55 |
Runtime error |
11 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
56 |
Halted |
0 ms |
0 KB |
- |