#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long long max_weights(int N, int MM, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<pair<int, ll>>P[N];
vector<int>Q_[N + 4];
vector<int>*Q = Q_ + 2;
for (int i = 0; i < MM; i++)
{
P[X[i]].push_back({Y[i], W[i]});
Q[X[i]].push_back(Y[i]);
}
for (int i = -1; i <= N; i++)
{
Q[i].push_back(N);
}
for (int i = 0; i < N; i++)
{
sort(P[i].begin(), P[i].end());
for (int j = 1; j < (int)P[i].size(); j++)
P[i][j].second += P[i][j - 1].second;
sort(Q[i].begin(), Q[i].end());
}
ll dp_[N + 3];
ll* dp = dp_ + 2;
for (int i = -2; i <= N; i++)
dp[i] = 0;
vector<ll> u[N];
vector<ll> d[N];
auto get = [&](int x, int y)->ll
{
if (x < 0 || x >= N)
return 0;
auto it = lower_bound(P[x].begin(), P[x].end(), make_pair(y + 1, -1ll));
if (it == P[x].begin())
return 0;
it--;
return it->second;
};
ll vv = 0;
for (int x = 0; x < N; x++)
{
dp[x] = dp[x - 1];
dp[x] = max(dp[x], vv);
vv = 0;
for (int y : Q[x - 1])
u[x].push_back(0);
for (int y : Q[x + 1])
d[x].push_back(0);
for (int i = 0; i < (int)Q[x - 1].size(); i++)
{
ll&v = u[x][i];
int y = Q[x - 1][i];
v = max(v, dp[x - 1]);
v = max(v, dp[x - 2] + get(x - 1, y));
}
if (x != 0)
{
ll mx = -1e18;
int j = 0;
for (int i = 0; i < (int)Q[x - 1].size(); i++)
{
ll&v = u[x][i];
int y = Q[x - 1][i];
while (j < (int)Q[x - 2].size() && Q[x - 2][j] < y)
{
mx = max(mx, u[x - 1][j] - get(x - 1, Q[x - 2][j]));
j++;
}
v = max(v, mx + get(x - 1, y));
}
}
for (int i = 0; i < (int)Q[x - 1].size(); i++)
{
ll&v = u[x][i];
int y = Q[x - 1][i];
dp[x] = max(dp[x], v);
vv = max(vv, v + get(x + 1, y));
}
for (int i = 0; i < (int)Q[x + 1].size(); i++)
{
ll&v = d[x][i];
int y = Q[x + 1][i];
if (y == N)
v = max(v, u[x].back());
v = max(v, dp[x - 1]);
v = max(v, dp[x - 2] + get(x - 1, y));
}
if (x != 0)
{
int j = (int)Q[x].size() - 1;
ll mx = -1e18;
for (int i = (int)Q[x + 1].size() - 1; i >= 0; i--)
{
ll&v = d[x][i];
int y = Q[x + 1][i];
while (j >= 0 && Q[x][j] > y)
{
mx = max(mx, d[x - 1][j] + get(x, Q[x][j]));
j--;
}
v = max(v, mx - get(x, y));
}
}
for (int i = 0; i < (int)Q[x + 1].size(); i++)
{
ll&v = d[x][i];
int y = Q[x + 1][i];
dp[x] = max(dp[x], v);
vv = max(vv, v + get(x + 1, y));
}
}
return dp[N - 1];
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:49:18: warning: unused variable 'y' [-Wunused-variable]
49 | for (int y : Q[x - 1])
| ^
fish.cpp:51:18: warning: unused variable 'y' [-Wunused-variable]
51 | for (int y : Q[x + 1])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
21972 KB |
Output is correct |
2 |
Correct |
79 ms |
24852 KB |
Output is correct |
3 |
Correct |
31 ms |
19864 KB |
Output is correct |
4 |
Correct |
27 ms |
19780 KB |
Output is correct |
5 |
Correct |
220 ms |
38836 KB |
Output is correct |
6 |
Correct |
243 ms |
40856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
118 ms |
27008 KB |
Output is correct |
3 |
Correct |
147 ms |
32396 KB |
Output is correct |
4 |
Correct |
64 ms |
23372 KB |
Output is correct |
5 |
Correct |
78 ms |
26424 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
28 ms |
19796 KB |
Output is correct |
11 |
Correct |
25 ms |
19788 KB |
Output is correct |
12 |
Correct |
72 ms |
24080 KB |
Output is correct |
13 |
Correct |
83 ms |
27496 KB |
Output is correct |
14 |
Correct |
69 ms |
23768 KB |
Output is correct |
15 |
Correct |
79 ms |
26416 KB |
Output is correct |
16 |
Correct |
70 ms |
23708 KB |
Output is correct |
17 |
Correct |
79 ms |
26364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
19744 KB |
Output is correct |
2 |
Correct |
25 ms |
19860 KB |
Output is correct |
3 |
Correct |
62 ms |
20848 KB |
Output is correct |
4 |
Correct |
51 ms |
21840 KB |
Output is correct |
5 |
Correct |
108 ms |
25412 KB |
Output is correct |
6 |
Correct |
97 ms |
25260 KB |
Output is correct |
7 |
Correct |
123 ms |
25292 KB |
Output is correct |
8 |
Correct |
103 ms |
25280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
33 ms |
3124 KB |
Output is correct |
18 |
Correct |
33 ms |
4556 KB |
Output is correct |
19 |
Correct |
27 ms |
4240 KB |
Output is correct |
20 |
Correct |
26 ms |
4172 KB |
Output is correct |
21 |
Correct |
26 ms |
4200 KB |
Output is correct |
22 |
Correct |
58 ms |
8144 KB |
Output is correct |
23 |
Correct |
6 ms |
980 KB |
Output is correct |
24 |
Correct |
22 ms |
2368 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
5 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
33 ms |
3124 KB |
Output is correct |
18 |
Correct |
33 ms |
4556 KB |
Output is correct |
19 |
Correct |
27 ms |
4240 KB |
Output is correct |
20 |
Correct |
26 ms |
4172 KB |
Output is correct |
21 |
Correct |
26 ms |
4200 KB |
Output is correct |
22 |
Correct |
58 ms |
8144 KB |
Output is correct |
23 |
Correct |
6 ms |
980 KB |
Output is correct |
24 |
Correct |
22 ms |
2368 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
5 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
161 ms |
18444 KB |
Output is correct |
29 |
Correct |
220 ms |
22220 KB |
Output is correct |
30 |
Correct |
200 ms |
22092 KB |
Output is correct |
31 |
Correct |
208 ms |
24140 KB |
Output is correct |
32 |
Correct |
217 ms |
28864 KB |
Output is correct |
33 |
Correct |
200 ms |
24100 KB |
Output is correct |
34 |
Correct |
199 ms |
24264 KB |
Output is correct |
35 |
Correct |
75 ms |
11440 KB |
Output is correct |
36 |
Correct |
223 ms |
27464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
19744 KB |
Output is correct |
2 |
Correct |
25 ms |
19860 KB |
Output is correct |
3 |
Correct |
62 ms |
20848 KB |
Output is correct |
4 |
Correct |
51 ms |
21840 KB |
Output is correct |
5 |
Correct |
108 ms |
25412 KB |
Output is correct |
6 |
Correct |
97 ms |
25260 KB |
Output is correct |
7 |
Correct |
123 ms |
25292 KB |
Output is correct |
8 |
Correct |
103 ms |
25280 KB |
Output is correct |
9 |
Correct |
105 ms |
25284 KB |
Output is correct |
10 |
Correct |
76 ms |
16716 KB |
Output is correct |
11 |
Correct |
172 ms |
33148 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
25 ms |
19860 KB |
Output is correct |
19 |
Correct |
25 ms |
19868 KB |
Output is correct |
20 |
Correct |
26 ms |
19780 KB |
Output is correct |
21 |
Correct |
25 ms |
19936 KB |
Output is correct |
22 |
Correct |
101 ms |
25804 KB |
Output is correct |
23 |
Correct |
172 ms |
33120 KB |
Output is correct |
24 |
Correct |
177 ms |
33164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
21972 KB |
Output is correct |
2 |
Correct |
79 ms |
24852 KB |
Output is correct |
3 |
Correct |
31 ms |
19864 KB |
Output is correct |
4 |
Correct |
27 ms |
19780 KB |
Output is correct |
5 |
Correct |
220 ms |
38836 KB |
Output is correct |
6 |
Correct |
243 ms |
40856 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
118 ms |
27008 KB |
Output is correct |
9 |
Correct |
147 ms |
32396 KB |
Output is correct |
10 |
Correct |
64 ms |
23372 KB |
Output is correct |
11 |
Correct |
78 ms |
26424 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
28 ms |
19796 KB |
Output is correct |
17 |
Correct |
25 ms |
19788 KB |
Output is correct |
18 |
Correct |
72 ms |
24080 KB |
Output is correct |
19 |
Correct |
83 ms |
27496 KB |
Output is correct |
20 |
Correct |
69 ms |
23768 KB |
Output is correct |
21 |
Correct |
79 ms |
26416 KB |
Output is correct |
22 |
Correct |
70 ms |
23708 KB |
Output is correct |
23 |
Correct |
79 ms |
26364 KB |
Output is correct |
24 |
Correct |
25 ms |
19744 KB |
Output is correct |
25 |
Correct |
25 ms |
19860 KB |
Output is correct |
26 |
Correct |
62 ms |
20848 KB |
Output is correct |
27 |
Correct |
51 ms |
21840 KB |
Output is correct |
28 |
Correct |
108 ms |
25412 KB |
Output is correct |
29 |
Correct |
97 ms |
25260 KB |
Output is correct |
30 |
Correct |
123 ms |
25292 KB |
Output is correct |
31 |
Correct |
103 ms |
25280 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
340 KB |
Output is correct |
48 |
Correct |
33 ms |
3124 KB |
Output is correct |
49 |
Correct |
33 ms |
4556 KB |
Output is correct |
50 |
Correct |
27 ms |
4240 KB |
Output is correct |
51 |
Correct |
26 ms |
4172 KB |
Output is correct |
52 |
Correct |
26 ms |
4200 KB |
Output is correct |
53 |
Correct |
58 ms |
8144 KB |
Output is correct |
54 |
Correct |
6 ms |
980 KB |
Output is correct |
55 |
Correct |
22 ms |
2368 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
5 ms |
852 KB |
Output is correct |
58 |
Correct |
3 ms |
980 KB |
Output is correct |
59 |
Correct |
161 ms |
18444 KB |
Output is correct |
60 |
Correct |
220 ms |
22220 KB |
Output is correct |
61 |
Correct |
200 ms |
22092 KB |
Output is correct |
62 |
Correct |
208 ms |
24140 KB |
Output is correct |
63 |
Correct |
217 ms |
28864 KB |
Output is correct |
64 |
Correct |
200 ms |
24100 KB |
Output is correct |
65 |
Correct |
199 ms |
24264 KB |
Output is correct |
66 |
Correct |
75 ms |
11440 KB |
Output is correct |
67 |
Correct |
223 ms |
27464 KB |
Output is correct |
68 |
Correct |
105 ms |
25284 KB |
Output is correct |
69 |
Correct |
76 ms |
16716 KB |
Output is correct |
70 |
Correct |
172 ms |
33148 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
25 ms |
19860 KB |
Output is correct |
78 |
Correct |
25 ms |
19868 KB |
Output is correct |
79 |
Correct |
26 ms |
19780 KB |
Output is correct |
80 |
Correct |
25 ms |
19936 KB |
Output is correct |
81 |
Correct |
101 ms |
25804 KB |
Output is correct |
82 |
Correct |
172 ms |
33120 KB |
Output is correct |
83 |
Correct |
177 ms |
33164 KB |
Output is correct |
84 |
Correct |
250 ms |
38344 KB |
Output is correct |
85 |
Correct |
254 ms |
40096 KB |
Output is correct |
86 |
Correct |
267 ms |
38912 KB |
Output is correct |
87 |
Correct |
275 ms |
40344 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
276 ms |
40968 KB |
Output is correct |
90 |
Correct |
234 ms |
41252 KB |
Output is correct |
91 |
Correct |
208 ms |
42012 KB |
Output is correct |