#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#pragma GCC optimize("Ofast")
long long max_weights(int32_t n, int32_t m, vector<int32_t> X, vector<int32_t> Y,
vector<int32_t> W) {
for (int i = 0; i < m; i++) {
X[i]++; Y[i]++;
}
// int olda[n + 1][n + 1];
// for (int i = 0; i <= n; i++) {
// for (int j = 0; j <= n; j++) {
// olda[i][j] = 0;
// }
// }
// for (int i = 0; i < m; i++) {
// olda[X[i]][Y[i]] = W[i];
// }
// int oldps[n + 1][n + 1]; // ps[i][j] = a[i][1] + ... + a[i][j]
// for (int i = 1; i <= n; i++) {
// oldps[i][0] = 0;
// for (int j = 1; j <= n; j++) {
// oldps[i][j] = oldps[i][j - 1] + olda[i][j];
// }
// }
vector<pii> a[n + 1], ps[n + 1];
set<int> critpts[n + 1];
for (int i = 1; i <= n; i++) {
a[i].pb({0, 0});
ps[i].pb({0, 0});
critpts[i].insert(0);
critpts[i].insert(n);
}
for (int i = 0; i < m; i++) {
a[X[i]].pb({Y[i], W[i]});
// critical points: (X[i] +- 1, Y[i]), (X[i], Y[i] - 1)
if (X[i] > 1) {
critpts[X[i] - 1].insert(Y[i]);
}
if (X[i] < n) {
critpts[X[i] + 1].insert(Y[i]);
}
critpts[X[i]].insert(Y[i] - 1);
}
for (int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
for (pii &x : a[i]) {
ps[i].pb({x.ff, ps[i].back().ss + x.ss});
}
}
function<int(int, int)> GetPS = [&](int x, int y) -> int {
int lb = 0, rb = (int)(ps[x].size()) - 1;
while (lb < rb) {
int mid = (lb + rb + 1) >> 1;
if (ps[x][mid].ff <= y) lb = mid;
else rb = mid - 1;
}
return ps[x][lb].ss;
};
// int olddp[2][n + 1][n + 1];
// // olddp[increasing][pos][height] = max ans from 1 to pos with pos getting height, height must be increasing
// for (int i = 0; i <= n; i++) {
// olddp[0][0][i] = olddp[1][0][i] = (i == 0 ? 0 : -1e18);
// olddp[0][1][i] = olddp[1][1][i] = 0;
// }
// int oldcur[n + 1];
// function<void(void)> resetoldcur = [&]() {
// for (int i = 0; i <= n; i++) oldcur[i] = 0;
// };
vector<vector<pii>> dp[2];
dp[0].resize(n + 1); dp[1].resize(n + 1);
for (int i = 0; i <= n; i++) {
dp[0][0].pb({i, (i == 0 ? 0 : -1e18)});
dp[1][0].pb({i, (i == 0 ? 0 : -1e18)});
dp[0][1].pb({i, 0});
dp[1][1].pb({i, 0});
}
for (int i = 2; i <= n; i++) {
for (int x : critpts[i]) {
dp[0][i].pb({x, 0});
dp[1][i].pb({x, 0});
}
}
vector<pii> cur;
for (int i = 2; i <= n; i++) {
// for (int j = 0; j <= n; j++) {
// olddp[0][i][j] = olddp[1][i][j] = 0;
// }
// Case 0: height of i is 0
// resetoldcur();
// olddp[0][i][0] = max(olddp[0][i - 1][0], olddp[1][i - 1][0]);
// for (int j = 1; j <= n; j++) {
// olddp[0][i][0] = max(olddp[0][i][0], max(olddp[0][i - 1][j], olddp[1][i - 1][j]) + oldps[i][j]);
// }
// olddp[1][i][0] = olddp[0][i][0];
{
// dp[flag][i][0].ff is always 0
for (int j = 0; j < (int)(dp[0][i - 1].size()); j++) {
dp[0][i][0].ss = max(dp[0][i][0].ss, max(dp[0][i - 1][j].ss, dp[1][i - 1][j].ss) + GetPS(i, dp[0][i - 1][j].ff));
}
dp[1][i][0].ss = dp[0][i][0].ss;
}
// Case 1: height of i-1 is 0
// Case 1.1: height of i-2 <= height of i
// resetoldcur();
// oldcur[0] = olddp[0][i - 2][0];
// for (int j = 1; j <= n; j++) {
// oldcur[j] = max(oldcur[j - 1], max(olddp[0][i - 2][j], olddp[1][i - 2][j]));
// }
// for (int j = 0; j <= n; j++) {
// olddp[0][i][j] = max(olddp[0][i][j], oldcur[j] + oldps[i - 1][j]);
// olddp[1][i][j] = max(olddp[1][i][j], oldcur[j] + oldps[i - 1][j]);
// }
{
cur.clear();
for (int j = 0; j < (int)(dp[0][i - 2].size()); j++) {
cur.pb({dp[0][i - 2][j].ff, max(dp[0][i - 2][j].ss, dp[1][i - 2][j].ss)});
}
for (int j = 1; j < (int)(cur.size()); j++) {
cur[j].ss = max(cur[j].ss, cur[j - 1].ss);
}
int ptr = 0, curans = 0;
for (int j = 0; j < (int)(dp[0][i].size()); j++) {
while (ptr < (int)(cur.size()) && cur[ptr].ff <= dp[0][i][j].ff) {
curans = max(curans, cur[ptr].ss);
ptr++;
}
dp[0][i][j].ss = max(dp[0][i][j].ss, curans + GetPS(i - 1, dp[0][i][j].ff));
dp[1][i][j].ss = max(dp[1][i][j].ss, curans + GetPS(i - 1, dp[0][i][j].ff));
}
}
// Case 1.2: height of i-2 >= height of i
// resetoldcur();
// oldcur[n] = olddp[0][i - 2][n] + oldps[i - 1][n];
// for (int j = n - 1; j >= 0; j--) {
// oldcur[j] = max(oldcur[j + 1], max(olddp[0][i - 2][j], olddp[1][i - 2][j]) + oldps[i - 1][j]);
// }
// for (int j = 0; j <= n; j++) {
// olddp[0][i][j] = max(olddp[0][i][j], oldcur[j]);
// olddp[1][i][j] = max(olddp[1][i][j], oldcur[j]);
// }
{
cur.clear();
for (int j = 0; j < (int)(dp[0][i - 2].size()); j++) {
cur.pb({dp[0][i - 2][j].ff, max(dp[0][i - 2][j].ss, dp[1][i - 2][j].ss) + GetPS(i - 1, dp[0][i - 2][j].ff)});
}
for (int j = (int)(cur.size()) - 2; j >= 0; j--) {
cur[j].ss = max(cur[j].ss, cur[j + 1].ss);
}
int ptr = (int)(cur.size()) - 1, curans = 0;
for (int j = (int)(dp[0][i].size()) - 1; j >= 0; j--) {
while (ptr >= 0 && cur[ptr].ff >= dp[0][i][j].ff) {
curans = max(curans, cur[ptr].ss);
ptr--;
}
dp[0][i][j].ss = max(dp[0][i][j].ss, curans);
dp[1][i][j].ss = max(dp[1][i][j].ss, curans);
}
}
// now height of i-1 > 0
// Case 2: height of i-1 <= height of i
// resetoldcur();
// oldcur[1] = olddp[0][i - 1][1];
// for (int j = 2; j <= n; j++) {
// oldcur[j] = max(oldcur[j - 1] + olda[i - 1][j], olddp[0][i - 1][j]);
// }
// for (int j = 1; j <= n; j++) {
// olddp[0][i][j] = max(olddp[0][i][j], oldcur[j]);
// }
{
cur.clear();
int ptr = 1, curans = 0;
for (int j = 1; j < (int)(dp[0][i].size()); j++) {
curans += (GetPS(i - 1, dp[0][i][j].ff) - GetPS(i - 1, (j == 1 ? 1 : dp[0][i][j - 1].ff)));
while (ptr < (int)(dp[0][i - 1].size()) && dp[0][i - 1][ptr].ff <= dp[0][i][j].ff) {
curans = max(curans, dp[0][i - 1][ptr].ss + (GetPS(i - 1, dp[0][i][j].ff) - GetPS(i - 1, dp[0][i - 1][ptr].ff)));
ptr++;
}
dp[0][i][j].ss = max(dp[0][i][j].ss, curans);
}
}
// Case 3: height of i-1 >= height of i
// resetoldcur();
// oldcur[n] = max(olddp[0][i - 1][n], olddp[1][i - 1][n]);
// for (int j = n - 1; j >= 1; j--) {
// oldcur[j] = max(oldcur[j + 1] + olda[i][j + 1], max(olddp[0][i - 1][j], olddp[1][i - 1][j]));
// }
// for (int j = 1; j <= n; j++) {
// olddp[1][i][j] = max(olddp[1][i][j], oldcur[j]);
// }
{
cur.clear();
int ptr = (int)(dp[1][i - 1].size()) - 1, curans = 0;
for (int j = (int)(dp[1][i].size()) - 1; j >= 1; j--) {
curans += (GetPS(i, (j + 1 == (int)(dp[1][i].size()) ? n : dp[1][i][j + 1].ff)) - GetPS(i, dp[1][i][j].ff));
while (ptr >= 0 && dp[1][i - 1][ptr].ff >= dp[0][i][j].ff) {
curans = max(curans, max(dp[0][i - 1][ptr].ss, dp[1][i - 1][ptr].ss) + (GetPS(i, dp[1][i - 1][ptr].ff) - GetPS(i, dp[1][i][j].ff)));
ptr--;
}
dp[1][i][j].ss = max(dp[1][i][j].ss, curans);
}
}
}
// int ans = max(dp[0][n][0].ss, dp[1][n][0].ss);
// for (int i = 1; i <= n; i++) {
// // cerr << olddp[0][n][i] << ' ' << olddp[1][n][i] << endl;
// ans = max(ans, max(olddp[0][n][i], olddp[1][n][i]));
// }
// return ans;
// for (int i = 1; i <= n; i++) {
// cerr << i << ": ";
// for (pii &x : dp[1][i]) cerr << "(" << x.ff << ", " << x.ss << ") ";
// cerr << endl;
// }
int ans = 0;
for (pii &x : dp[0][n]) ans = max(ans, x.ss);
for (pii &x : dp[1][n]) ans = max(ans, x.ss);
return ans;
}
#undef int
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
63672 KB |
Output is correct |
2 |
Correct |
254 ms |
71988 KB |
Output is correct |
3 |
Correct |
96 ms |
53424 KB |
Output is correct |
4 |
Correct |
93 ms |
53380 KB |
Output is correct |
5 |
Execution timed out |
1081 ms |
146736 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
436 ms |
75408 KB |
Output is correct |
3 |
Correct |
526 ms |
85312 KB |
Output is correct |
4 |
Correct |
217 ms |
63568 KB |
Output is correct |
5 |
Correct |
253 ms |
71968 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
100 ms |
53364 KB |
Output is correct |
11 |
Correct |
91 ms |
53420 KB |
Output is correct |
12 |
Correct |
295 ms |
70204 KB |
Output is correct |
13 |
Correct |
344 ms |
79744 KB |
Output is correct |
14 |
Correct |
279 ms |
66936 KB |
Output is correct |
15 |
Correct |
277 ms |
68132 KB |
Output is correct |
16 |
Correct |
280 ms |
67116 KB |
Output is correct |
17 |
Correct |
309 ms |
73784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
53448 KB |
Output is correct |
2 |
Correct |
103 ms |
53392 KB |
Output is correct |
3 |
Correct |
171 ms |
57804 KB |
Output is correct |
4 |
Correct |
156 ms |
59924 KB |
Output is correct |
5 |
Correct |
252 ms |
74612 KB |
Output is correct |
6 |
Correct |
248 ms |
74548 KB |
Output is correct |
7 |
Correct |
243 ms |
74568 KB |
Output is correct |
8 |
Correct |
245 ms |
74636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
66 ms |
9132 KB |
Output is correct |
18 |
Correct |
62 ms |
9420 KB |
Output is correct |
19 |
Correct |
57 ms |
9392 KB |
Output is correct |
20 |
Correct |
55 ms |
8648 KB |
Output is correct |
21 |
Correct |
54 ms |
8528 KB |
Output is correct |
22 |
Correct |
129 ms |
16720 KB |
Output is correct |
23 |
Correct |
17 ms |
3360 KB |
Output is correct |
24 |
Correct |
56 ms |
7636 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
16 ms |
3084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
66 ms |
9132 KB |
Output is correct |
18 |
Correct |
62 ms |
9420 KB |
Output is correct |
19 |
Correct |
57 ms |
9392 KB |
Output is correct |
20 |
Correct |
55 ms |
8648 KB |
Output is correct |
21 |
Correct |
54 ms |
8528 KB |
Output is correct |
22 |
Correct |
129 ms |
16720 KB |
Output is correct |
23 |
Correct |
17 ms |
3360 KB |
Output is correct |
24 |
Correct |
56 ms |
7636 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
16 ms |
3084 KB |
Output is correct |
27 |
Correct |
6 ms |
2644 KB |
Output is correct |
28 |
Correct |
444 ms |
40048 KB |
Output is correct |
29 |
Correct |
771 ms |
59852 KB |
Output is correct |
30 |
Execution timed out |
1044 ms |
108592 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
53448 KB |
Output is correct |
2 |
Correct |
103 ms |
53392 KB |
Output is correct |
3 |
Correct |
171 ms |
57804 KB |
Output is correct |
4 |
Correct |
156 ms |
59924 KB |
Output is correct |
5 |
Correct |
252 ms |
74612 KB |
Output is correct |
6 |
Correct |
248 ms |
74548 KB |
Output is correct |
7 |
Correct |
243 ms |
74568 KB |
Output is correct |
8 |
Correct |
245 ms |
74636 KB |
Output is correct |
9 |
Correct |
298 ms |
79256 KB |
Output is correct |
10 |
Correct |
174 ms |
43160 KB |
Output is correct |
11 |
Correct |
380 ms |
86036 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
103 ms |
53396 KB |
Output is correct |
19 |
Correct |
98 ms |
53444 KB |
Output is correct |
20 |
Correct |
101 ms |
53516 KB |
Output is correct |
21 |
Correct |
94 ms |
53464 KB |
Output is correct |
22 |
Correct |
402 ms |
88236 KB |
Output is correct |
23 |
Correct |
484 ms |
105860 KB |
Output is correct |
24 |
Correct |
520 ms |
107292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
63672 KB |
Output is correct |
2 |
Correct |
254 ms |
71988 KB |
Output is correct |
3 |
Correct |
96 ms |
53424 KB |
Output is correct |
4 |
Correct |
93 ms |
53380 KB |
Output is correct |
5 |
Execution timed out |
1081 ms |
146736 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |