#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 100001;
struct state
{
vector<int64_t> s, l, z;
};
state f[N];
vector<int64_t> p[N];
vector<uint32_t> coords[N];
size_t ind(size_t x, int y)
{
return lower_bound(coords[x].begin(), coords[x].end(), y) - coords[x].begin();
}
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w)
{
for (size_t i = 0; i < m; ++i)
{
coords[x[i]].push_back(y[i] + 1);
if (x[i])
coords[x[i] - 1].push_back(y[i] + 1);
if (x[i] + 1 < n)
coords[x[i] + 1].push_back(y[i] + 1);
}
for (size_t i = 0; i < n; ++i)
{
coords[i].push_back(0);
sort(coords[i].begin(), coords[i].end());
coords[i].resize(unique(coords[i].begin(), coords[i].end()) - coords[i].begin());
f[i].s.resize(coords[i].size());
f[i].l.resize(coords[i].size());
f[i].z.resize(i ? coords[i - 1].size() : 1);
p[i].resize(coords[i].size());
}
for (size_t i = 1; i < n; ++i)
{
fill(f[i].s.begin(), f[i].s.end(), INT64_MIN / 2);
fill(f[i].l.begin(), f[i].l.end(), INT64_MIN / 2);
fill(f[i].z.begin(), f[i].z.end(), INT64_MIN / 2);
}
for (size_t i = 0; i < m; ++i)
p[x[i]][ind(x[i], y[i] + 1)] = w[i];
for (size_t i = 0; i < n; ++i)
for (size_t j = 1; j < p[i].size(); ++j)
p[i][j] += p[i][j - 1];
for (size_t i = 1; i < n; ++i)
{
// Update s
int64_t h = f[i - 1].s[0];
size_t k = 0, l = i == 1 ? SIZE_MAX : 0;
for (size_t j = 1; j < coords[i].size(); ++j)
{
while (k < coords[i - 1].size() && coords[i - 1][k] < coords[i][j])
{
h = max(h, f[i - 1].s[k] - p[i - 1][k]);
++k;
}
while (l < f[i - 1].z.size() && coords[i - 2][l] < coords[i][j])
{
size_t v = ind(i - 1, coords[i - 2][l]);
if (v == coords[i - 1].size() || coords[i - 1][v] > coords[i - 2][l])
--v;
h = max(h, f[i - 1].z[l] - p[i - 1][v]);
++l;
}
size_t u = ind(i - 1, coords[i][j]);
if (u == coords[i - 1].size() || coords[i - 1][u] > coords[i][j])
--u;
f[i].s[j] = max(f[i].s[j], h + p[i - 1][u]);
}
h = INT64_MIN;
l = f[i - 1].z.size() - 1;
for (size_t j = coords[i].size() - 1; j < coords[i].size(); --j)
{
while (l != SIZE_MAX && (i == 1 || coords[i - 2][l] > coords[i][j]))
{
h = max(h, f[i - 1].z[l]);
f[i].s[j] = max(f[i].s[j], h);
--l;
}
if (l != SIZE_MAX)
{
h = max(h, f[i - 1].z[l]);
f[i].s[j] = max(f[i].s[j], h);
--l;
}
}
// Update l
int64_t g = 0; // max f_i-1,k,s f_i-1,k-l + p_ik over all k >= j
k = coords[i - 1].size() - 1;
for (size_t j = coords[i].size() - 1; j < coords[i].size(); --j)
{
while (k != SIZE_MAX && coords[i - 1][k] > coords[i][j])
{
size_t u = ind(i, coords[i - 1][k]);
if (u == coords[i].size() || coords[i][u] > coords[i - 1][k])
--u;
g = max(g, max(f[i - 1].l[k], f[i - 1].s[k]) + p[i][u]);
--k;
}
if (k != SIZE_MAX)
{
size_t u = ind(i, coords[i - 1][k]);
if (u == coords[i].size() || coords[i][u] > coords[i - 1][k])
--u;
g = max(g, max(f[i - 1].l[k], f[i - 1].s[k]) + p[i][u]);
--k;
}
f[i].l[j] = max(f[i].l[j], g - p[i][j]);
}
// Update z
k = 0;
for (size_t j = 0; j < f[i].z.size(); ++j)
{
while (k + 1 < coords[i].size() && coords[i][k + 1] <= coords[i - 1][j])
++k;
f[i].z[j] = max(f[i].z[j], max(f[i - 1].s[j], f[i - 1].l[j]) + p[i][k]);
if (!j)
{
for (size_t k = 0; k < f[i - 1].z.size(); ++k)
f[i].z[j] = max(f[i].z[j], f[i - 1].z[k]);
}
}
}
int64_t ans = 0;
for (size_t j = 0; j < f[n - 1].s.size(); ++j)
ans = max(ans, max(f[n - 1].s[j], f[n - 1].l[j]));
for (size_t j = 0; j < f[n - 1].z.size(); ++j)
ans = max(ans, f[n - 1].z[j]);
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:24:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
24 | for (size_t i = 0; i < m; ++i)
| ~~^~~
fish.cpp:33:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
33 | for (size_t i = 0; i < n; ++i)
| ~~^~~
fish.cpp:44:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | for (size_t i = 1; i < n; ++i)
| ~~^~~
fish.cpp:51:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | for (size_t i = 0; i < m; ++i)
| ~~^~~
fish.cpp:53:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
53 | for (size_t i = 0; i < n; ++i)
| ~~^~~
fish.cpp:57:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
57 | for (size_t i = 1; i < n; ++i)
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
33672 KB |
Output is correct |
2 |
Correct |
90 ms |
37036 KB |
Output is correct |
3 |
Correct |
29 ms |
27588 KB |
Output is correct |
4 |
Correct |
30 ms |
27648 KB |
Output is correct |
5 |
Correct |
303 ms |
66540 KB |
Output is correct |
6 |
Correct |
250 ms |
62172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
136 ms |
39540 KB |
Output is correct |
3 |
Correct |
165 ms |
43800 KB |
Output is correct |
4 |
Correct |
73 ms |
33716 KB |
Output is correct |
5 |
Correct |
91 ms |
37116 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
5 ms |
11988 KB |
Output is correct |
8 |
Correct |
5 ms |
11988 KB |
Output is correct |
9 |
Correct |
5 ms |
11988 KB |
Output is correct |
10 |
Correct |
30 ms |
27604 KB |
Output is correct |
11 |
Correct |
29 ms |
27692 KB |
Output is correct |
12 |
Correct |
91 ms |
36624 KB |
Output is correct |
13 |
Correct |
108 ms |
40592 KB |
Output is correct |
14 |
Correct |
97 ms |
35236 KB |
Output is correct |
15 |
Correct |
87 ms |
35264 KB |
Output is correct |
16 |
Correct |
86 ms |
35172 KB |
Output is correct |
17 |
Correct |
94 ms |
37668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
27664 KB |
Output is correct |
2 |
Correct |
30 ms |
27608 KB |
Output is correct |
3 |
Correct |
54 ms |
27332 KB |
Output is correct |
4 |
Correct |
47 ms |
28492 KB |
Output is correct |
5 |
Correct |
76 ms |
29940 KB |
Output is correct |
6 |
Correct |
69 ms |
29992 KB |
Output is correct |
7 |
Correct |
85 ms |
30032 KB |
Output is correct |
8 |
Correct |
73 ms |
30036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
5 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
12048 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12040 KB |
Output is correct |
8 |
Correct |
5 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11968 KB |
Output is correct |
10 |
Correct |
6 ms |
12232 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12044 KB |
Output is correct |
14 |
Correct |
6 ms |
12100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
5 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
12048 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12040 KB |
Output is correct |
8 |
Correct |
5 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11968 KB |
Output is correct |
10 |
Correct |
6 ms |
12232 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12044 KB |
Output is correct |
14 |
Correct |
6 ms |
12100 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
6 ms |
12224 KB |
Output is correct |
17 |
Correct |
29 ms |
16536 KB |
Output is correct |
18 |
Correct |
28 ms |
16152 KB |
Output is correct |
19 |
Correct |
27 ms |
16204 KB |
Output is correct |
20 |
Correct |
26 ms |
15996 KB |
Output is correct |
21 |
Correct |
31 ms |
15884 KB |
Output is correct |
22 |
Correct |
49 ms |
19824 KB |
Output is correct |
23 |
Correct |
12 ms |
13268 KB |
Output is correct |
24 |
Correct |
27 ms |
15828 KB |
Output is correct |
25 |
Correct |
7 ms |
12116 KB |
Output is correct |
26 |
Correct |
11 ms |
13140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
5 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
12048 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12040 KB |
Output is correct |
8 |
Correct |
5 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11968 KB |
Output is correct |
10 |
Correct |
6 ms |
12232 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12044 KB |
Output is correct |
14 |
Correct |
6 ms |
12100 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
6 ms |
12224 KB |
Output is correct |
17 |
Correct |
29 ms |
16536 KB |
Output is correct |
18 |
Correct |
28 ms |
16152 KB |
Output is correct |
19 |
Correct |
27 ms |
16204 KB |
Output is correct |
20 |
Correct |
26 ms |
15996 KB |
Output is correct |
21 |
Correct |
31 ms |
15884 KB |
Output is correct |
22 |
Correct |
49 ms |
19824 KB |
Output is correct |
23 |
Correct |
12 ms |
13268 KB |
Output is correct |
24 |
Correct |
27 ms |
15828 KB |
Output is correct |
25 |
Correct |
7 ms |
12116 KB |
Output is correct |
26 |
Correct |
11 ms |
13140 KB |
Output is correct |
27 |
Correct |
7 ms |
12684 KB |
Output is correct |
28 |
Correct |
131 ms |
32480 KB |
Output is correct |
29 |
Correct |
208 ms |
44864 KB |
Output is correct |
30 |
Correct |
267 ms |
58176 KB |
Output is correct |
31 |
Correct |
271 ms |
57804 KB |
Output is correct |
32 |
Correct |
166 ms |
38396 KB |
Output is correct |
33 |
Correct |
288 ms |
58676 KB |
Output is correct |
34 |
Correct |
301 ms |
58804 KB |
Output is correct |
35 |
Correct |
96 ms |
27024 KB |
Output is correct |
36 |
Correct |
254 ms |
52660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
27664 KB |
Output is correct |
2 |
Correct |
30 ms |
27608 KB |
Output is correct |
3 |
Correct |
54 ms |
27332 KB |
Output is correct |
4 |
Correct |
47 ms |
28492 KB |
Output is correct |
5 |
Correct |
76 ms |
29940 KB |
Output is correct |
6 |
Correct |
69 ms |
29992 KB |
Output is correct |
7 |
Correct |
85 ms |
30032 KB |
Output is correct |
8 |
Correct |
73 ms |
30036 KB |
Output is correct |
9 |
Correct |
84 ms |
36300 KB |
Output is correct |
10 |
Correct |
61 ms |
23212 KB |
Output is correct |
11 |
Correct |
131 ms |
34384 KB |
Output is correct |
12 |
Correct |
5 ms |
11988 KB |
Output is correct |
13 |
Correct |
5 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
8 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
30 ms |
27648 KB |
Output is correct |
19 |
Correct |
30 ms |
27676 KB |
Output is correct |
20 |
Correct |
35 ms |
27696 KB |
Output is correct |
21 |
Correct |
31 ms |
27680 KB |
Output is correct |
22 |
Correct |
108 ms |
35344 KB |
Output is correct |
23 |
Correct |
174 ms |
45072 KB |
Output is correct |
24 |
Correct |
181 ms |
46608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
33672 KB |
Output is correct |
2 |
Correct |
90 ms |
37036 KB |
Output is correct |
3 |
Correct |
29 ms |
27588 KB |
Output is correct |
4 |
Correct |
30 ms |
27648 KB |
Output is correct |
5 |
Correct |
303 ms |
66540 KB |
Output is correct |
6 |
Correct |
250 ms |
62172 KB |
Output is correct |
7 |
Correct |
5 ms |
11988 KB |
Output is correct |
8 |
Correct |
136 ms |
39540 KB |
Output is correct |
9 |
Correct |
165 ms |
43800 KB |
Output is correct |
10 |
Correct |
73 ms |
33716 KB |
Output is correct |
11 |
Correct |
91 ms |
37116 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
5 ms |
11988 KB |
Output is correct |
14 |
Correct |
5 ms |
11988 KB |
Output is correct |
15 |
Correct |
5 ms |
11988 KB |
Output is correct |
16 |
Correct |
30 ms |
27604 KB |
Output is correct |
17 |
Correct |
29 ms |
27692 KB |
Output is correct |
18 |
Correct |
91 ms |
36624 KB |
Output is correct |
19 |
Correct |
108 ms |
40592 KB |
Output is correct |
20 |
Correct |
97 ms |
35236 KB |
Output is correct |
21 |
Correct |
87 ms |
35264 KB |
Output is correct |
22 |
Correct |
86 ms |
35172 KB |
Output is correct |
23 |
Correct |
94 ms |
37668 KB |
Output is correct |
24 |
Correct |
29 ms |
27664 KB |
Output is correct |
25 |
Correct |
30 ms |
27608 KB |
Output is correct |
26 |
Correct |
54 ms |
27332 KB |
Output is correct |
27 |
Correct |
47 ms |
28492 KB |
Output is correct |
28 |
Correct |
76 ms |
29940 KB |
Output is correct |
29 |
Correct |
69 ms |
29992 KB |
Output is correct |
30 |
Correct |
85 ms |
30032 KB |
Output is correct |
31 |
Correct |
73 ms |
30036 KB |
Output is correct |
32 |
Correct |
5 ms |
11988 KB |
Output is correct |
33 |
Correct |
5 ms |
11988 KB |
Output is correct |
34 |
Correct |
5 ms |
11988 KB |
Output is correct |
35 |
Correct |
5 ms |
11988 KB |
Output is correct |
36 |
Correct |
7 ms |
12048 KB |
Output is correct |
37 |
Correct |
6 ms |
11988 KB |
Output is correct |
38 |
Correct |
6 ms |
12040 KB |
Output is correct |
39 |
Correct |
5 ms |
11988 KB |
Output is correct |
40 |
Correct |
6 ms |
11968 KB |
Output is correct |
41 |
Correct |
6 ms |
12232 KB |
Output is correct |
42 |
Correct |
6 ms |
12116 KB |
Output is correct |
43 |
Correct |
7 ms |
12116 KB |
Output is correct |
44 |
Correct |
6 ms |
12044 KB |
Output is correct |
45 |
Correct |
6 ms |
12100 KB |
Output is correct |
46 |
Correct |
6 ms |
12116 KB |
Output is correct |
47 |
Correct |
6 ms |
12224 KB |
Output is correct |
48 |
Correct |
29 ms |
16536 KB |
Output is correct |
49 |
Correct |
28 ms |
16152 KB |
Output is correct |
50 |
Correct |
27 ms |
16204 KB |
Output is correct |
51 |
Correct |
26 ms |
15996 KB |
Output is correct |
52 |
Correct |
31 ms |
15884 KB |
Output is correct |
53 |
Correct |
49 ms |
19824 KB |
Output is correct |
54 |
Correct |
12 ms |
13268 KB |
Output is correct |
55 |
Correct |
27 ms |
15828 KB |
Output is correct |
56 |
Correct |
7 ms |
12116 KB |
Output is correct |
57 |
Correct |
11 ms |
13140 KB |
Output is correct |
58 |
Correct |
7 ms |
12684 KB |
Output is correct |
59 |
Correct |
131 ms |
32480 KB |
Output is correct |
60 |
Correct |
208 ms |
44864 KB |
Output is correct |
61 |
Correct |
267 ms |
58176 KB |
Output is correct |
62 |
Correct |
271 ms |
57804 KB |
Output is correct |
63 |
Correct |
166 ms |
38396 KB |
Output is correct |
64 |
Correct |
288 ms |
58676 KB |
Output is correct |
65 |
Correct |
301 ms |
58804 KB |
Output is correct |
66 |
Correct |
96 ms |
27024 KB |
Output is correct |
67 |
Correct |
254 ms |
52660 KB |
Output is correct |
68 |
Correct |
84 ms |
36300 KB |
Output is correct |
69 |
Correct |
61 ms |
23212 KB |
Output is correct |
70 |
Correct |
131 ms |
34384 KB |
Output is correct |
71 |
Correct |
5 ms |
11988 KB |
Output is correct |
72 |
Correct |
5 ms |
11988 KB |
Output is correct |
73 |
Correct |
6 ms |
11988 KB |
Output is correct |
74 |
Correct |
6 ms |
11988 KB |
Output is correct |
75 |
Correct |
8 ms |
11988 KB |
Output is correct |
76 |
Correct |
7 ms |
11988 KB |
Output is correct |
77 |
Correct |
30 ms |
27648 KB |
Output is correct |
78 |
Correct |
30 ms |
27676 KB |
Output is correct |
79 |
Correct |
35 ms |
27696 KB |
Output is correct |
80 |
Correct |
31 ms |
27680 KB |
Output is correct |
81 |
Correct |
108 ms |
35344 KB |
Output is correct |
82 |
Correct |
174 ms |
45072 KB |
Output is correct |
83 |
Correct |
181 ms |
46608 KB |
Output is correct |
84 |
Correct |
315 ms |
67608 KB |
Output is correct |
85 |
Correct |
319 ms |
68620 KB |
Output is correct |
86 |
Correct |
254 ms |
67648 KB |
Output is correct |
87 |
Correct |
273 ms |
68800 KB |
Output is correct |
88 |
Correct |
6 ms |
11988 KB |
Output is correct |
89 |
Correct |
267 ms |
68632 KB |
Output is correct |
90 |
Correct |
233 ms |
63156 KB |
Output is correct |
91 |
Correct |
185 ms |
55668 KB |
Output is correct |