#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
const long long INF = (1LL << 60);
struct value{
int y; long long ans;
};
struct catfish{
int y, w; long long s;
bool operator< (const catfish& o) const {
return y < o.y;
}
};
const int MAXN = 1e5 + 11;
vector<value> DP[3][MAXN];
vector<catfish> F[MAXN];
int S[MAXN];
long long prefix_weight(int x, int y){
int idx = upper_bound(F[x].begin(), F[x].end(), catfish{y}) - F[x].begin() - 1;
return F[x][idx].s;
}
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
for(int i = 0; i < M; i++) X[i]++;
for(int i = 0; i <= N + 1; i++){
F[i].push_back({-1, 0});
}
for(int i = 0; i < M; i++){
F[X[i]].push_back({Y[i], W[i]});
}
for(int i = 1; i <= N; i++){
sort(F[i].begin(), F[i].end());
for(int j = 1; j < (int) F[i].size(); j++){
F[i][j].s = F[i][j - 1].s + F[i][j].w;
}
}
for(int i = 1; i <= N; i++){
vector<int> sp {-1};
for(auto [y, w, s] : F[i - 1]) sp.push_back(y);
for(auto [y, w, s] : F[i]) sp.push_back(y);
for(auto [y, w, s] : F[i + 1]) sp.push_back(y);
sort(sp.begin(), sp.end()); sp.resize(unique(sp.begin(), sp.end()) - sp.begin());
for(auto v : sp) DP[0][i].push_back({v, 0}), DP[1][i].push_back({v, 0}), DP[2][i].push_back({v, 0}); S[i] = sp.size();
}
for(int x = 1; x <= N; x++){
int pd0 = 0, pd1 = 0, pd2 = 0;
// 0 to 0
pd0 = 0; long long dp_max = 0;
for(auto& [h, ans] : DP[0][x]){
while(pd0 < S[x - 1] && DP[0][x - 1][pd0].y <= h) dp_max = max(dp_max, DP[0][x - 1][pd0].ans - prefix_weight(x - 1, DP[0][x - 1][pd0].y) - prefix_weight(x, DP[0][x - 1][pd0].y)), pd0++;
ans = max(ans, prefix_weight(x - 1, h) + prefix_weight(x + 1, h) + dp_max);
}
// 2 to 0
pd2 = S[x - 1] - 1; dp_max = -INF;
for(int i = S[x] - 1; i >= 0; i--){
int h = DP[0][x][i].y;
while(pd2 >= 0 && DP[2][x - 1][pd2].y >= h) dp_max = max(dp_max, DP[2][x - 1][pd2--].ans);
DP[0][x][i].ans = max(DP[0][x][i].ans, prefix_weight(x + 1, h) + dp_max);
}
// 2 to 0
pd2 = 0; dp_max = 0;
for(auto& [h, ans] : DP[0][x]){
while(pd2 < S[x - 1] && DP[2][x - 1][pd2].y <= h) dp_max = max(dp_max, DP[2][x - 1][pd2].ans - prefix_weight(x - 1, DP[2][x - 1][pd2].y)), pd2++;
ans = max(ans, prefix_weight(x - 1, h) + prefix_weight(x + 1, h) + dp_max);
}
// 0 and 1 to 1
pd0 = S[x - 1] - 1, pd1 = S[x - 1] - 1; dp_max = -INF;
for(int i = S[x] - 1; i >= 0; i--){
int h = DP[1][x][i].y;
while(pd0 >= 0 && DP[0][x - 1][pd0].y >= h) dp_max = max(dp_max, DP[0][x - 1][pd0--].ans);
while(pd1 >= 0 && DP[1][x - 1][pd1].y >= h) dp_max = max(dp_max, DP[1][x - 1][pd1--].ans);
DP[1][x][i].ans = max(DP[1][x][i].ans, dp_max + prefix_weight(x + 1, h) - prefix_weight(x, h));
}
// 0 and 1 to 2
pd0 = S[x - 1] - 1, pd1 = S[x - 1] - 1;
for(int i = S[x] - 1; i >= 0; i--){
int h = DP[2][x][i].y; dp_max = 0;
while(pd0 >= 0 && DP[0][x - 1][pd0].y >= h) dp_max = max(dp_max, DP[0][x - 1][pd0].ans), pd0--;
while(pd1 >= 0 && DP[1][x - 1][pd1].y >= h) dp_max = max(dp_max, DP[1][x - 1][pd1].ans), pd1--;
DP[2][x][i].ans = max(DP[2][x][i].ans, dp_max);
}
// 2 to 2
dp_max = 0;
for(int i = 0; i < S[x - 1]; i++){
dp_max = max(dp_max, DP[2][x - 1][i].ans);
}
DP[2][x][0].ans = max(DP[2][x][0].ans, dp_max);
}
long long ans = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < S[N]; j++){
ans = max(ans, DP[i][N][j].ans);
}
}
return ans;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:48:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
48 | for(auto v : sp) DP[0][i].push_back({v, 0}), DP[1][i].push_back({v, 0}), DP[2][i].push_back({v, 0}); S[i] = sp.size();
| ^~~
fish.cpp:48:110: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
48 | for(auto v : sp) DP[0][i].push_back({v, 0}), DP[1][i].push_back({v, 0}), DP[2][i].push_back({v, 0}); S[i] = sp.size();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
32868 KB |
Output is correct |
2 |
Correct |
116 ms |
36476 KB |
Output is correct |
3 |
Correct |
34 ms |
22516 KB |
Output is correct |
4 |
Correct |
34 ms |
22592 KB |
Output is correct |
5 |
Correct |
367 ms |
80628 KB |
Output is correct |
6 |
Correct |
351 ms |
96976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
187 ms |
40456 KB |
Output is correct |
3 |
Correct |
218 ms |
45020 KB |
Output is correct |
4 |
Correct |
101 ms |
32840 KB |
Output is correct |
5 |
Correct |
116 ms |
36396 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
41 ms |
22620 KB |
Output is correct |
11 |
Correct |
34 ms |
22540 KB |
Output is correct |
12 |
Correct |
123 ms |
36744 KB |
Output is correct |
13 |
Correct |
149 ms |
41128 KB |
Output is correct |
14 |
Correct |
119 ms |
35240 KB |
Output is correct |
15 |
Correct |
121 ms |
33184 KB |
Output is correct |
16 |
Correct |
123 ms |
35132 KB |
Output is correct |
17 |
Correct |
129 ms |
37672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
22588 KB |
Output is correct |
2 |
Correct |
33 ms |
22584 KB |
Output is correct |
3 |
Correct |
64 ms |
26364 KB |
Output is correct |
4 |
Correct |
56 ms |
26152 KB |
Output is correct |
5 |
Correct |
101 ms |
32104 KB |
Output is correct |
6 |
Correct |
88 ms |
31996 KB |
Output is correct |
7 |
Correct |
91 ms |
32000 KB |
Output is correct |
8 |
Correct |
92 ms |
32044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
10020 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9912 KB |
Output is correct |
13 |
Correct |
4 ms |
9732 KB |
Output is correct |
14 |
Correct |
5 ms |
9864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
10020 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9912 KB |
Output is correct |
13 |
Correct |
4 ms |
9732 KB |
Output is correct |
14 |
Correct |
5 ms |
9864 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9940 KB |
Output is correct |
17 |
Correct |
44 ms |
16400 KB |
Output is correct |
18 |
Correct |
41 ms |
16340 KB |
Output is correct |
19 |
Correct |
34 ms |
16340 KB |
Output is correct |
20 |
Correct |
28 ms |
15736 KB |
Output is correct |
21 |
Correct |
31 ms |
15708 KB |
Output is correct |
22 |
Correct |
80 ms |
21636 KB |
Output is correct |
23 |
Correct |
16 ms |
11860 KB |
Output is correct |
24 |
Correct |
39 ms |
14736 KB |
Output is correct |
25 |
Correct |
6 ms |
9964 KB |
Output is correct |
26 |
Correct |
15 ms |
11732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
10020 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9912 KB |
Output is correct |
13 |
Correct |
4 ms |
9732 KB |
Output is correct |
14 |
Correct |
5 ms |
9864 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9940 KB |
Output is correct |
17 |
Correct |
44 ms |
16400 KB |
Output is correct |
18 |
Correct |
41 ms |
16340 KB |
Output is correct |
19 |
Correct |
34 ms |
16340 KB |
Output is correct |
20 |
Correct |
28 ms |
15736 KB |
Output is correct |
21 |
Correct |
31 ms |
15708 KB |
Output is correct |
22 |
Correct |
80 ms |
21636 KB |
Output is correct |
23 |
Correct |
16 ms |
11860 KB |
Output is correct |
24 |
Correct |
39 ms |
14736 KB |
Output is correct |
25 |
Correct |
6 ms |
9964 KB |
Output is correct |
26 |
Correct |
15 ms |
11732 KB |
Output is correct |
27 |
Correct |
7 ms |
10580 KB |
Output is correct |
28 |
Correct |
193 ms |
37884 KB |
Output is correct |
29 |
Correct |
290 ms |
48956 KB |
Output is correct |
30 |
Correct |
413 ms |
95052 KB |
Output is correct |
31 |
Correct |
427 ms |
94048 KB |
Output is correct |
32 |
Correct |
226 ms |
46172 KB |
Output is correct |
33 |
Correct |
439 ms |
95192 KB |
Output is correct |
34 |
Correct |
434 ms |
95020 KB |
Output is correct |
35 |
Correct |
129 ms |
33356 KB |
Output is correct |
36 |
Correct |
408 ms |
73684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
22588 KB |
Output is correct |
2 |
Correct |
33 ms |
22584 KB |
Output is correct |
3 |
Correct |
64 ms |
26364 KB |
Output is correct |
4 |
Correct |
56 ms |
26152 KB |
Output is correct |
5 |
Correct |
101 ms |
32104 KB |
Output is correct |
6 |
Correct |
88 ms |
31996 KB |
Output is correct |
7 |
Correct |
91 ms |
32000 KB |
Output is correct |
8 |
Correct |
92 ms |
32044 KB |
Output is correct |
9 |
Correct |
110 ms |
41512 KB |
Output is correct |
10 |
Correct |
78 ms |
28484 KB |
Output is correct |
11 |
Correct |
160 ms |
47348 KB |
Output is correct |
12 |
Correct |
4 ms |
9580 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
38 ms |
22484 KB |
Output is correct |
19 |
Correct |
34 ms |
22584 KB |
Output is correct |
20 |
Correct |
33 ms |
22504 KB |
Output is correct |
21 |
Correct |
33 ms |
22612 KB |
Output is correct |
22 |
Correct |
157 ms |
46812 KB |
Output is correct |
23 |
Correct |
218 ms |
59556 KB |
Output is correct |
24 |
Correct |
211 ms |
61368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
32868 KB |
Output is correct |
2 |
Correct |
116 ms |
36476 KB |
Output is correct |
3 |
Correct |
34 ms |
22516 KB |
Output is correct |
4 |
Correct |
34 ms |
22592 KB |
Output is correct |
5 |
Correct |
367 ms |
80628 KB |
Output is correct |
6 |
Correct |
351 ms |
96976 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
187 ms |
40456 KB |
Output is correct |
9 |
Correct |
218 ms |
45020 KB |
Output is correct |
10 |
Correct |
101 ms |
32840 KB |
Output is correct |
11 |
Correct |
116 ms |
36396 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
41 ms |
22620 KB |
Output is correct |
17 |
Correct |
34 ms |
22540 KB |
Output is correct |
18 |
Correct |
123 ms |
36744 KB |
Output is correct |
19 |
Correct |
149 ms |
41128 KB |
Output is correct |
20 |
Correct |
119 ms |
35240 KB |
Output is correct |
21 |
Correct |
121 ms |
33184 KB |
Output is correct |
22 |
Correct |
123 ms |
35132 KB |
Output is correct |
23 |
Correct |
129 ms |
37672 KB |
Output is correct |
24 |
Correct |
34 ms |
22588 KB |
Output is correct |
25 |
Correct |
33 ms |
22584 KB |
Output is correct |
26 |
Correct |
64 ms |
26364 KB |
Output is correct |
27 |
Correct |
56 ms |
26152 KB |
Output is correct |
28 |
Correct |
101 ms |
32104 KB |
Output is correct |
29 |
Correct |
88 ms |
31996 KB |
Output is correct |
30 |
Correct |
91 ms |
32000 KB |
Output is correct |
31 |
Correct |
92 ms |
32044 KB |
Output is correct |
32 |
Correct |
4 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
4 ms |
9684 KB |
Output is correct |
35 |
Correct |
4 ms |
9684 KB |
Output is correct |
36 |
Correct |
4 ms |
9684 KB |
Output is correct |
37 |
Correct |
4 ms |
9684 KB |
Output is correct |
38 |
Correct |
4 ms |
9684 KB |
Output is correct |
39 |
Correct |
4 ms |
9684 KB |
Output is correct |
40 |
Correct |
5 ms |
9684 KB |
Output is correct |
41 |
Correct |
6 ms |
10020 KB |
Output is correct |
42 |
Correct |
5 ms |
9812 KB |
Output is correct |
43 |
Correct |
5 ms |
9912 KB |
Output is correct |
44 |
Correct |
4 ms |
9732 KB |
Output is correct |
45 |
Correct |
5 ms |
9864 KB |
Output is correct |
46 |
Correct |
4 ms |
9684 KB |
Output is correct |
47 |
Correct |
6 ms |
9940 KB |
Output is correct |
48 |
Correct |
44 ms |
16400 KB |
Output is correct |
49 |
Correct |
41 ms |
16340 KB |
Output is correct |
50 |
Correct |
34 ms |
16340 KB |
Output is correct |
51 |
Correct |
28 ms |
15736 KB |
Output is correct |
52 |
Correct |
31 ms |
15708 KB |
Output is correct |
53 |
Correct |
80 ms |
21636 KB |
Output is correct |
54 |
Correct |
16 ms |
11860 KB |
Output is correct |
55 |
Correct |
39 ms |
14736 KB |
Output is correct |
56 |
Correct |
6 ms |
9964 KB |
Output is correct |
57 |
Correct |
15 ms |
11732 KB |
Output is correct |
58 |
Correct |
7 ms |
10580 KB |
Output is correct |
59 |
Correct |
193 ms |
37884 KB |
Output is correct |
60 |
Correct |
290 ms |
48956 KB |
Output is correct |
61 |
Correct |
413 ms |
95052 KB |
Output is correct |
62 |
Correct |
427 ms |
94048 KB |
Output is correct |
63 |
Correct |
226 ms |
46172 KB |
Output is correct |
64 |
Correct |
439 ms |
95192 KB |
Output is correct |
65 |
Correct |
434 ms |
95020 KB |
Output is correct |
66 |
Correct |
129 ms |
33356 KB |
Output is correct |
67 |
Correct |
408 ms |
73684 KB |
Output is correct |
68 |
Correct |
110 ms |
41512 KB |
Output is correct |
69 |
Correct |
78 ms |
28484 KB |
Output is correct |
70 |
Correct |
160 ms |
47348 KB |
Output is correct |
71 |
Correct |
4 ms |
9580 KB |
Output is correct |
72 |
Correct |
4 ms |
9684 KB |
Output is correct |
73 |
Correct |
4 ms |
9684 KB |
Output is correct |
74 |
Correct |
4 ms |
9684 KB |
Output is correct |
75 |
Correct |
4 ms |
9684 KB |
Output is correct |
76 |
Correct |
4 ms |
9684 KB |
Output is correct |
77 |
Correct |
38 ms |
22484 KB |
Output is correct |
78 |
Correct |
34 ms |
22584 KB |
Output is correct |
79 |
Correct |
33 ms |
22504 KB |
Output is correct |
80 |
Correct |
33 ms |
22612 KB |
Output is correct |
81 |
Correct |
157 ms |
46812 KB |
Output is correct |
82 |
Correct |
218 ms |
59556 KB |
Output is correct |
83 |
Correct |
211 ms |
61368 KB |
Output is correct |
84 |
Correct |
439 ms |
72304 KB |
Output is correct |
85 |
Correct |
443 ms |
72420 KB |
Output is correct |
86 |
Correct |
379 ms |
94228 KB |
Output is correct |
87 |
Correct |
399 ms |
96440 KB |
Output is correct |
88 |
Correct |
4 ms |
9684 KB |
Output is correct |
89 |
Correct |
394 ms |
96340 KB |
Output is correct |
90 |
Correct |
332 ms |
79972 KB |
Output is correct |
91 |
Correct |
241 ms |
63908 KB |
Output is correct |