/*input
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct Curve : priority_queue<long long>
{
long long end_slope, offset;
void swap(Curve &rhs)
{
priority_queue::swap(rhs);
std::swap(end_slope, rhs.end_slope);
std::swap(offset, rhs.offset);
}
void init(long long x)
{
while (!empty()) pop();
push(x), push(x);
end_slope = 1;
offset = 0;
}
void merge(Curve &rhs)
{
if (size() < rhs.size()) swap(rhs);
if (rhs.empty()) return;
if (top() < rhs.top()) offset = offset + rhs.offset + (long long) end_slope * (rhs.top() - top());
else offset = offset + rhs.offset + (long long) rhs.end_slope * (top() - rhs.top());
end_slope += rhs.end_slope;
for (; !rhs.empty(); rhs.pop()) push(rhs.top());
}
void normalize(long long x)
{
for (; end_slope > 1; --end_slope) {
long long t = top();
pop();
offset -= (long long) (t - top()) * (end_slope - 1);
}
long long first = top();
pop();
long long second = top();
pop();
push(second + x);
push(first + x);
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> P(N + M + 1), C(N + M + 1);
for (int i = 2; i <= N + M; ++i) cin >> P[i] >> C[i];
vector<Curve> curves(N + M + 1);
for (int i = N + M; i > N; --i) {
curves[i].init(C[i]);
curves[P[i]].merge(curves[i]);
}
for (int i = N; i > 1; --i) {
curves[i].normalize(C[i]);
curves[P[i]].merge(curves[i]);
}
curves[1].normalize(0);
cout << curves[1].offset << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
392 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
0 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
512 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
768 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
3 ms |
896 KB |
Output is correct |
38 |
Correct |
3 ms |
896 KB |
Output is correct |
39 |
Correct |
3 ms |
896 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
640 KB |
Output is correct |
42 |
Correct |
2 ms |
640 KB |
Output is correct |
43 |
Correct |
3 ms |
768 KB |
Output is correct |
44 |
Correct |
3 ms |
896 KB |
Output is correct |
45 |
Correct |
3 ms |
896 KB |
Output is correct |
46 |
Correct |
3 ms |
1024 KB |
Output is correct |
47 |
Correct |
3 ms |
1024 KB |
Output is correct |
48 |
Correct |
3 ms |
896 KB |
Output is correct |
49 |
Correct |
3 ms |
1024 KB |
Output is correct |
50 |
Correct |
3 ms |
896 KB |
Output is correct |
51 |
Correct |
3 ms |
896 KB |
Output is correct |
52 |
Correct |
3 ms |
896 KB |
Output is correct |
53 |
Correct |
3 ms |
896 KB |
Output is correct |
54 |
Correct |
3 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
392 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
0 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
512 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
768 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
3 ms |
896 KB |
Output is correct |
38 |
Correct |
3 ms |
896 KB |
Output is correct |
39 |
Correct |
3 ms |
896 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
640 KB |
Output is correct |
42 |
Correct |
2 ms |
640 KB |
Output is correct |
43 |
Correct |
3 ms |
768 KB |
Output is correct |
44 |
Correct |
3 ms |
896 KB |
Output is correct |
45 |
Correct |
3 ms |
896 KB |
Output is correct |
46 |
Correct |
3 ms |
1024 KB |
Output is correct |
47 |
Correct |
3 ms |
1024 KB |
Output is correct |
48 |
Correct |
3 ms |
896 KB |
Output is correct |
49 |
Correct |
3 ms |
1024 KB |
Output is correct |
50 |
Correct |
3 ms |
896 KB |
Output is correct |
51 |
Correct |
3 ms |
896 KB |
Output is correct |
52 |
Correct |
3 ms |
896 KB |
Output is correct |
53 |
Correct |
3 ms |
896 KB |
Output is correct |
54 |
Correct |
3 ms |
896 KB |
Output is correct |
55 |
Correct |
7 ms |
1784 KB |
Output is correct |
56 |
Correct |
25 ms |
5752 KB |
Output is correct |
57 |
Correct |
46 ms |
9340 KB |
Output is correct |
58 |
Correct |
68 ms |
12120 KB |
Output is correct |
59 |
Correct |
78 ms |
15600 KB |
Output is correct |
60 |
Correct |
105 ms |
19468 KB |
Output is correct |
61 |
Correct |
117 ms |
22640 KB |
Output is correct |
62 |
Correct |
133 ms |
24556 KB |
Output is correct |
63 |
Correct |
154 ms |
29156 KB |
Output is correct |
64 |
Correct |
164 ms |
30448 KB |
Output is correct |
65 |
Correct |
93 ms |
21624 KB |
Output is correct |
66 |
Correct |
86 ms |
21624 KB |
Output is correct |
67 |
Correct |
86 ms |
21752 KB |
Output is correct |
68 |
Correct |
150 ms |
28468 KB |
Output is correct |
69 |
Correct |
161 ms |
29164 KB |
Output is correct |
70 |
Correct |
172 ms |
29036 KB |
Output is correct |
71 |
Correct |
216 ms |
43908 KB |
Output is correct |
72 |
Correct |
204 ms |
44136 KB |
Output is correct |
73 |
Correct |
175 ms |
40460 KB |
Output is correct |
74 |
Correct |
181 ms |
40904 KB |
Output is correct |
75 |
Correct |
181 ms |
39276 KB |
Output is correct |
76 |
Correct |
188 ms |
39480 KB |
Output is correct |
77 |
Correct |
193 ms |
38576 KB |
Output is correct |
78 |
Correct |
195 ms |
38120 KB |
Output is correct |
79 |
Correct |
172 ms |
36768 KB |
Output is correct |