#include "fish.h"
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
vector<vector<pair<int, ll>>> fishes;
vector<map<int, ll>> prefixWeight;
vector<ll> totals;
inline ll weightSum(int x, int y1, int y2) {
if (x >= fishes.size() || y2 <= y1)
return 0;
auto it1 = prefixWeight[x].upper_bound(y2 - 1);
auto it2 = prefixWeight[x].upper_bound(y1 - 1);
ll res = (--it1)->second - (--it2)->second;
//cout << x << ' ' << y1 << ' ' << y2 << " = " << res << endl;
return res;
}
long long max_weights(signed N, signed M, vector<signed> X, std::vector<signed> Y, std::vector<signed> W) {
fishes = vector<vector<pair<int, ll>>>(N);
loop(i, M) {
fishes[X[i]].push_back({Y[i], W[i]});
}
fishes.push_back({});
prefixWeight = vector<map<int, ll>>(N + 1);
totals = vector<ll>(N + 1);
loop(x, N + 1) {
sort(all(fishes[x]));
ll total = 0;
prefixWeight[x][-2] = 0;
for (auto [y, w] : fishes[x]) {
total += w;
prefixWeight[x][y] = total;
}
totals[x] = total;
}
vector<map<int, ll>> beforeDp(N);
vector<map<int, ll>> bothDp(N);
vector<ll> maxBefore(N);
vector<ll> maxBoth(N);
vector<ll> maxBeforeMinCur(N);
//bestBefore, bestBeforeAndAfter
int counter = 0;
loop(x, N) {
if (x == 0) {
for (const auto& [y, w] : fishes[x + 1]) {
int height = y + 1;
bothDp[0][height] = weightSum(x + 1, 0, height);
maxBoth[x] = max(maxBoth[x], bothDp[0][height]);
maxBeforeMinCur[x] = max(maxBeforeMinCur[x], 0 - weightSum(x, 0, height));
}
beforeDp[0][0] = 0;
maxBefore[0] = 0;
continue;
}
ll bestBefore2 = 0;
if (x >= 2) {
bestBefore2 = maxBefore[x - 2];
}
if (x >= 3) {
bestBefore2 = max(bestBefore2, maxBoth[x - 3]);
}
for (const auto& [y, w] : fishes.at(x - 1)) {
int height = y + 1;
ll& both = bothDp[x][height];
ll& before = beforeDp[x][height];
both = maxBeforeMinCur[x - 1] + weightSum(x - 1, 0, height);
before = maxBeforeMinCur[x - 1] + weightSum(x - 1, 0, height);
// for (const auto& [prevHeight, prevScore] : beforeDp.at(x - 1)) {
// counter++;
// //ll prevCur = weightSum(x - 1, prevHeight, height);
// ll prevCur = -weightSum(x - 1, 0, prevHeight);
// both = max(both, prevScore + prevCur);
// before = max(before, prevScore + prevCur);
// }
// both += weightSum(x - 1, 0, height);
// before += weightSum(x - 1, 0, height);
//both = max(both, maxBeforeMinNext[x]);
// for (const auto& [prevY, prevScore] : bothDp.at(x - 1)) {
// counter++;
// if (prevY <= height)
// continue;
// ll prevCur = -weightSum(x, 0, height);
// both = max(both, prevScore + prevCur);
// }
ll prevWeight = weightSum(x - 1, 0, height);
both = max(both, bestBefore2 + prevWeight);
before = max(before, bestBefore2 + prevWeight);
maxBefore[x] = max(maxBefore[x], before);
maxBeforeMinCur[x] = max(maxBeforeMinCur[x], before - weightSum(x, 0, height));
}
for (const auto& [y, w] : fishes.at(x + 1)) {
int height = y + 1;
ll& both = bothDp[x][height];
both = max(both, maxBeforeMinCur[x - 1] + weightSum(x - 1, 0, height));
//both = max(both, maxBeforeMinNext[x]);
// for (const auto& [prevHeight, prevScore] : beforeDp.at(x - 1)) {
// counter++;
// ll prevCur = weightSum(x - 1, prevHeight, height);
// both = max(both, prevScore + prevCur);
// }
// for (const auto& [prevY, prevScore] : bothDp.at(x - 1)) {
// counter++;
// if (prevY <= height)
// continue;
// ll prevCur = -weightSum(x, 0, height);
// both = max(both, prevScore + prevCur);
// }
both = max(both, maxBoth[x - 1] - weightSum(x, 0, height));
ll prevWeight = weightSum(x - 1, 0, height);
both = max(both, bestBefore2 + prevWeight);
}
for (const auto& [height, score] : bothDp.at(x)) {
counter++;
ll nextWeight = weightSum(x + 1, 0, height);
bothDp[x][height] += nextWeight;
maxBoth[x] = max(maxBoth[x], bothDp[x][height]);
}
ll& both = bothDp[x][0];
ll& before = beforeDp[x][0];
both = max({ both, maxBefore[x - 1], bestBefore2 });
before = max(before, both);
maxBefore[x] = max(maxBefore[x], before);
maxBoth[x] = max(maxBoth[x], both);
maxBeforeMinCur[x] = max(maxBeforeMinCur[x], before);
}
//cout << "Counter: " << counter << endl;
ll res = 0;
loop(x, N) {
for (auto [y, score] : beforeDp[x]) {
res = max(res, score);
}
for (auto [y, score] : bothDp[x]) {
res = max(res, score);
}
}
return res;
}
Compilation message
fish.cpp: In function 'long long int weightSum(int, int, int)':
fish.cpp:38:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (x >= fishes.size() || y2 <= y1)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
53116 KB |
Output is correct |
2 |
Correct |
193 ms |
61332 KB |
Output is correct |
3 |
Correct |
32 ms |
38708 KB |
Output is correct |
4 |
Correct |
30 ms |
38540 KB |
Output is correct |
5 |
Correct |
665 ms |
125484 KB |
Output is correct |
6 |
Correct |
353 ms |
128084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
412 KB |
Output is correct |
2 |
Correct |
331 ms |
76636 KB |
Output is correct |
3 |
Correct |
420 ms |
90400 KB |
Output is correct |
4 |
Correct |
150 ms |
53120 KB |
Output is correct |
5 |
Correct |
203 ms |
61432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
26 ms |
38708 KB |
Output is correct |
11 |
Correct |
28 ms |
38704 KB |
Output is correct |
12 |
Correct |
178 ms |
58236 KB |
Output is correct |
13 |
Correct |
212 ms |
67736 KB |
Output is correct |
14 |
Correct |
154 ms |
55740 KB |
Output is correct |
15 |
Correct |
176 ms |
62100 KB |
Output is correct |
16 |
Correct |
158 ms |
55688 KB |
Output is correct |
17 |
Correct |
211 ms |
61816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
38704 KB |
Output is correct |
2 |
Correct |
27 ms |
38704 KB |
Output is correct |
3 |
Correct |
51 ms |
48080 KB |
Output is correct |
4 |
Correct |
51 ms |
47660 KB |
Output is correct |
5 |
Correct |
77 ms |
62812 KB |
Output is correct |
6 |
Correct |
81 ms |
62808 KB |
Output is correct |
7 |
Correct |
81 ms |
63016 KB |
Output is correct |
8 |
Correct |
84 ms |
63016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
52 ms |
11760 KB |
Output is correct |
18 |
Correct |
44 ms |
11652 KB |
Output is correct |
19 |
Correct |
36 ms |
11612 KB |
Output is correct |
20 |
Correct |
30 ms |
11124 KB |
Output is correct |
21 |
Correct |
30 ms |
11260 KB |
Output is correct |
22 |
Correct |
83 ms |
21888 KB |
Output is correct |
23 |
Correct |
9 ms |
3164 KB |
Output is correct |
24 |
Correct |
34 ms |
8628 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
8 ms |
2940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
52 ms |
11760 KB |
Output is correct |
18 |
Correct |
44 ms |
11652 KB |
Output is correct |
19 |
Correct |
36 ms |
11612 KB |
Output is correct |
20 |
Correct |
30 ms |
11124 KB |
Output is correct |
21 |
Correct |
30 ms |
11260 KB |
Output is correct |
22 |
Correct |
83 ms |
21888 KB |
Output is correct |
23 |
Correct |
9 ms |
3164 KB |
Output is correct |
24 |
Correct |
34 ms |
8628 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
8 ms |
2940 KB |
Output is correct |
27 |
Correct |
5 ms |
2396 KB |
Output is correct |
28 |
Correct |
274 ms |
54156 KB |
Output is correct |
29 |
Correct |
442 ms |
86868 KB |
Output is correct |
30 |
Correct |
380 ms |
94300 KB |
Output is correct |
31 |
Correct |
378 ms |
94180 KB |
Output is correct |
32 |
Correct |
334 ms |
76624 KB |
Output is correct |
33 |
Correct |
385 ms |
94804 KB |
Output is correct |
34 |
Correct |
363 ms |
94804 KB |
Output is correct |
35 |
Correct |
113 ms |
35712 KB |
Output is correct |
36 |
Correct |
403 ms |
93008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
38704 KB |
Output is correct |
2 |
Correct |
27 ms |
38704 KB |
Output is correct |
3 |
Correct |
51 ms |
48080 KB |
Output is correct |
4 |
Correct |
51 ms |
47660 KB |
Output is correct |
5 |
Correct |
77 ms |
62812 KB |
Output is correct |
6 |
Correct |
81 ms |
62808 KB |
Output is correct |
7 |
Correct |
81 ms |
63016 KB |
Output is correct |
8 |
Correct |
84 ms |
63016 KB |
Output is correct |
9 |
Correct |
92 ms |
69164 KB |
Output is correct |
10 |
Correct |
67 ms |
43256 KB |
Output is correct |
11 |
Correct |
144 ms |
86052 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
27 ms |
38756 KB |
Output is correct |
19 |
Correct |
27 ms |
38704 KB |
Output is correct |
20 |
Correct |
30 ms |
38696 KB |
Output is correct |
21 |
Correct |
26 ms |
38708 KB |
Output is correct |
22 |
Correct |
118 ms |
68648 KB |
Output is correct |
23 |
Correct |
154 ms |
95164 KB |
Output is correct |
24 |
Correct |
167 ms |
97068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
53116 KB |
Output is correct |
2 |
Correct |
193 ms |
61332 KB |
Output is correct |
3 |
Correct |
32 ms |
38708 KB |
Output is correct |
4 |
Correct |
30 ms |
38540 KB |
Output is correct |
5 |
Correct |
665 ms |
125484 KB |
Output is correct |
6 |
Correct |
353 ms |
128084 KB |
Output is correct |
7 |
Correct |
1 ms |
412 KB |
Output is correct |
8 |
Correct |
331 ms |
76636 KB |
Output is correct |
9 |
Correct |
420 ms |
90400 KB |
Output is correct |
10 |
Correct |
150 ms |
53120 KB |
Output is correct |
11 |
Correct |
203 ms |
61432 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
26 ms |
38708 KB |
Output is correct |
17 |
Correct |
28 ms |
38704 KB |
Output is correct |
18 |
Correct |
178 ms |
58236 KB |
Output is correct |
19 |
Correct |
212 ms |
67736 KB |
Output is correct |
20 |
Correct |
154 ms |
55740 KB |
Output is correct |
21 |
Correct |
176 ms |
62100 KB |
Output is correct |
22 |
Correct |
158 ms |
55688 KB |
Output is correct |
23 |
Correct |
211 ms |
61816 KB |
Output is correct |
24 |
Correct |
26 ms |
38704 KB |
Output is correct |
25 |
Correct |
27 ms |
38704 KB |
Output is correct |
26 |
Correct |
51 ms |
48080 KB |
Output is correct |
27 |
Correct |
51 ms |
47660 KB |
Output is correct |
28 |
Correct |
77 ms |
62812 KB |
Output is correct |
29 |
Correct |
81 ms |
62808 KB |
Output is correct |
30 |
Correct |
81 ms |
63016 KB |
Output is correct |
31 |
Correct |
84 ms |
63016 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
860 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
52 ms |
11760 KB |
Output is correct |
49 |
Correct |
44 ms |
11652 KB |
Output is correct |
50 |
Correct |
36 ms |
11612 KB |
Output is correct |
51 |
Correct |
30 ms |
11124 KB |
Output is correct |
52 |
Correct |
30 ms |
11260 KB |
Output is correct |
53 |
Correct |
83 ms |
21888 KB |
Output is correct |
54 |
Correct |
9 ms |
3164 KB |
Output is correct |
55 |
Correct |
34 ms |
8628 KB |
Output is correct |
56 |
Correct |
1 ms |
860 KB |
Output is correct |
57 |
Correct |
8 ms |
2940 KB |
Output is correct |
58 |
Correct |
5 ms |
2396 KB |
Output is correct |
59 |
Correct |
274 ms |
54156 KB |
Output is correct |
60 |
Correct |
442 ms |
86868 KB |
Output is correct |
61 |
Correct |
380 ms |
94300 KB |
Output is correct |
62 |
Correct |
378 ms |
94180 KB |
Output is correct |
63 |
Correct |
334 ms |
76624 KB |
Output is correct |
64 |
Correct |
385 ms |
94804 KB |
Output is correct |
65 |
Correct |
363 ms |
94804 KB |
Output is correct |
66 |
Correct |
113 ms |
35712 KB |
Output is correct |
67 |
Correct |
403 ms |
93008 KB |
Output is correct |
68 |
Correct |
92 ms |
69164 KB |
Output is correct |
69 |
Correct |
67 ms |
43256 KB |
Output is correct |
70 |
Correct |
144 ms |
86052 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
27 ms |
38756 KB |
Output is correct |
78 |
Correct |
27 ms |
38704 KB |
Output is correct |
79 |
Correct |
30 ms |
38696 KB |
Output is correct |
80 |
Correct |
26 ms |
38708 KB |
Output is correct |
81 |
Correct |
118 ms |
68648 KB |
Output is correct |
82 |
Correct |
154 ms |
95164 KB |
Output is correct |
83 |
Correct |
167 ms |
97068 KB |
Output is correct |
84 |
Correct |
593 ms |
126016 KB |
Output is correct |
85 |
Correct |
650 ms |
128988 KB |
Output is correct |
86 |
Correct |
286 ms |
130064 KB |
Output is correct |
87 |
Correct |
294 ms |
134960 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
305 ms |
134972 KB |
Output is correct |
90 |
Correct |
326 ms |
128040 KB |
Output is correct |
91 |
Correct |
295 ms |
118976 KB |
Output is correct |