#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXNM 300005
int N, M, p, c;
ll cost[MAXNM], totalCost = 0;
vector<int> asd[MAXNM];
struct slope {
long long gradient;
long long width;
slope(long long g, long long w) {
gradient = g;
width = w;
}
};
struct conv_func {
long long intercept;
vector<slope> lines;
conv_func() {
intercept = 0;
lines.push_back(slope(1, -1));
}
void extend(int c) {
intercept += c;
int pt = 0;
vector<slope> prev;
while (lines[pt].gradient < -1)
prev.push_back(lines[pt++]);
if (lines[pt].gradient == -1) lines[pt].width += c;
else prev.push_back(slope(-1, c));
while (lines[pt].gradient < 1)
prev.push_back(lines[pt++]);
prev.push_back(slope(1, -1));
lines = prev;
}
conv_func merge(conv_func f) {
conv_func ans, &g = (*this);
ans.lines.clear();
ans.intercept = f.intercept + g.intercept;
int i = 0, j = 0, flag = 1;
long long ax = 0, bx = 0;
while(flag) {
slope a = f.lines[i];
slope b = g.lines[j];
slope next_line(a.gradient + b.gradient, -1);
long long next_ax = ax + a.width;
long long next_bx = bx + b.width;
long long prev_x = max(ax, bx);
if (a.width == -1 && b.width == -1) flag = 0;
else if (a.width == -1) {
next_line.width = next_bx - prev_x;
j++; bx = next_bx;
} else if (b.width == -1) {
next_line.width = next_ax - prev_x;
i++; ax = next_ax;
} else {
next_line.width = min(next_ax, next_bx) - prev_x;
if (next_ax <= next_bx) i++, ax = next_ax;
if (next_bx <= next_ax) j++, bx = next_bx;
}
ans.lines.push_back(next_line);
}
return ans;
}
};
conv_func dfs(int u) {
conv_func ans;
for (int v: asd[u]) {
conv_func f = dfs(v);
f.extend(cost[v]);
if (ans.lines.size() == 1) ans = f;
else ans = ans.merge(f);
}
return ans;
}
int main() {
cin >> N >> M;
for (int i=1; i<N+M; i++) {
cin >> p >> c;
asd[p-1].push_back(i);
cost[i] = c;
}
conv_func func = dfs(0);
long long totalCost = 1e18;
long long temp = func.intercept;
for (int i=0; i<func.lines.size(); i++) {
if (func.lines[i].width == -1) break;
temp += func.lines[i].width * func.lines[i].gradient;
totalCost = min(totalCost, temp);
}
cout << totalCost << '\n';
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:108:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<slope>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (int i=0; i<func.lines.size(); i++) {
| ~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7356 KB |
Output is correct |
4 |
Correct |
5 ms |
7348 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
6 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7360 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
5 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7360 KB |
Output is correct |
2 |
Correct |
4 ms |
7360 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7368 KB |
Output is correct |
5 |
Correct |
4 ms |
7360 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7360 KB |
Output is correct |
9 |
Correct |
4 ms |
7364 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
5 ms |
7360 KB |
Output is correct |
12 |
Correct |
6 ms |
7380 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7356 KB |
Output is correct |
19 |
Correct |
4 ms |
7356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7356 KB |
Output is correct |
4 |
Correct |
5 ms |
7348 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
6 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7360 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
5 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7360 KB |
Output is correct |
12 |
Correct |
4 ms |
7360 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
5 ms |
7368 KB |
Output is correct |
15 |
Correct |
4 ms |
7360 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7360 KB |
Output is correct |
19 |
Correct |
4 ms |
7364 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
5 ms |
7360 KB |
Output is correct |
22 |
Correct |
6 ms |
7380 KB |
Output is correct |
23 |
Correct |
5 ms |
7252 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7356 KB |
Output is correct |
29 |
Correct |
4 ms |
7356 KB |
Output is correct |
30 |
Correct |
5 ms |
7360 KB |
Output is correct |
31 |
Correct |
5 ms |
7396 KB |
Output is correct |
32 |
Correct |
5 ms |
7496 KB |
Output is correct |
33 |
Correct |
6 ms |
7508 KB |
Output is correct |
34 |
Correct |
8 ms |
7496 KB |
Output is correct |
35 |
Correct |
10 ms |
7628 KB |
Output is correct |
36 |
Correct |
10 ms |
7628 KB |
Output is correct |
37 |
Correct |
9 ms |
7696 KB |
Output is correct |
38 |
Correct |
11 ms |
7748 KB |
Output is correct |
39 |
Correct |
11 ms |
7636 KB |
Output is correct |
40 |
Correct |
9 ms |
8788 KB |
Output is correct |
41 |
Correct |
10 ms |
8776 KB |
Output is correct |
42 |
Correct |
9 ms |
7636 KB |
Output is correct |
43 |
Correct |
33 ms |
8228 KB |
Output is correct |
44 |
Correct |
32 ms |
8024 KB |
Output is correct |
45 |
Correct |
31 ms |
8020 KB |
Output is correct |
46 |
Correct |
13 ms |
7668 KB |
Output is correct |
47 |
Correct |
15 ms |
7636 KB |
Output is correct |
48 |
Correct |
50 ms |
7640 KB |
Output is correct |
49 |
Correct |
56 ms |
7628 KB |
Output is correct |
50 |
Correct |
90 ms |
7640 KB |
Output is correct |
51 |
Correct |
84 ms |
7640 KB |
Output is correct |
52 |
Correct |
61 ms |
7652 KB |
Output is correct |
53 |
Correct |
45 ms |
7588 KB |
Output is correct |
54 |
Correct |
141 ms |
7728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7356 KB |
Output is correct |
4 |
Correct |
5 ms |
7348 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
6 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7360 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
5 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7360 KB |
Output is correct |
12 |
Correct |
4 ms |
7360 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
5 ms |
7368 KB |
Output is correct |
15 |
Correct |
4 ms |
7360 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7360 KB |
Output is correct |
19 |
Correct |
4 ms |
7364 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
5 ms |
7360 KB |
Output is correct |
22 |
Correct |
6 ms |
7380 KB |
Output is correct |
23 |
Correct |
5 ms |
7252 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7356 KB |
Output is correct |
29 |
Correct |
4 ms |
7356 KB |
Output is correct |
30 |
Correct |
5 ms |
7360 KB |
Output is correct |
31 |
Correct |
5 ms |
7396 KB |
Output is correct |
32 |
Correct |
5 ms |
7496 KB |
Output is correct |
33 |
Correct |
6 ms |
7508 KB |
Output is correct |
34 |
Correct |
8 ms |
7496 KB |
Output is correct |
35 |
Correct |
10 ms |
7628 KB |
Output is correct |
36 |
Correct |
10 ms |
7628 KB |
Output is correct |
37 |
Correct |
9 ms |
7696 KB |
Output is correct |
38 |
Correct |
11 ms |
7748 KB |
Output is correct |
39 |
Correct |
11 ms |
7636 KB |
Output is correct |
40 |
Correct |
9 ms |
8788 KB |
Output is correct |
41 |
Correct |
10 ms |
8776 KB |
Output is correct |
42 |
Correct |
9 ms |
7636 KB |
Output is correct |
43 |
Correct |
33 ms |
8228 KB |
Output is correct |
44 |
Correct |
32 ms |
8024 KB |
Output is correct |
45 |
Correct |
31 ms |
8020 KB |
Output is correct |
46 |
Correct |
13 ms |
7668 KB |
Output is correct |
47 |
Correct |
15 ms |
7636 KB |
Output is correct |
48 |
Correct |
50 ms |
7640 KB |
Output is correct |
49 |
Correct |
56 ms |
7628 KB |
Output is correct |
50 |
Correct |
90 ms |
7640 KB |
Output is correct |
51 |
Correct |
84 ms |
7640 KB |
Output is correct |
52 |
Correct |
61 ms |
7652 KB |
Output is correct |
53 |
Correct |
45 ms |
7588 KB |
Output is correct |
54 |
Correct |
141 ms |
7728 KB |
Output is correct |
55 |
Correct |
23 ms |
8240 KB |
Output is correct |
56 |
Correct |
129 ms |
10512 KB |
Output is correct |
57 |
Correct |
296 ms |
12976 KB |
Output is correct |
58 |
Correct |
322 ms |
14016 KB |
Output is correct |
59 |
Correct |
671 ms |
17040 KB |
Output is correct |
60 |
Correct |
669 ms |
18644 KB |
Output is correct |
61 |
Correct |
895 ms |
20096 KB |
Output is correct |
62 |
Correct |
914 ms |
21228 KB |
Output is correct |
63 |
Correct |
863 ms |
23172 KB |
Output is correct |
64 |
Correct |
1417 ms |
26432 KB |
Output is correct |
65 |
Correct |
313 ms |
99176 KB |
Output is correct |
66 |
Correct |
307 ms |
99012 KB |
Output is correct |
67 |
Correct |
291 ms |
24132 KB |
Output is correct |
68 |
Execution timed out |
2070 ms |
56988 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |