#include "fish.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
#pragma GCC optimize ("Ofast,fast-math")
#pragma GCC target ("sse4")
typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXM = 600000 + 10;
struct Position
{
int y;
int idx;
llong in;
llong left;
llong right;
Position(int _y, int _idx, llong _in, llong _left, llong _right)
{
y = _y;
idx = _idx;
left = _left;
right = _right;
in = _in;
}
};
int cnt;
int n, m;
llong dp[MAXM][2];
std::vector <Position> pos[MAXN];
std::vector <Position> posUncleared[MAXN];
std::vector <std::pair <int,int>> fish[MAXN];
std::vector <llong> minusIN[MAXM];
std::vector <llong> minusL[MAXM];
std::vector <llong> LR[MAXM];
llong max_weights(int N, int M, std::vector <int> X, std::vector <int> Y, std::vector <int> W)
{
n = N; m = M;
for (int i = 0 ; i < m ; ++i)
{
fish[X[i] + 1].emplace_back(Y[i] + 1, W[i]);
}
for (int x = 1 ; x <= n ; ++x)
{
std::sort(fish[x].begin(), fish[x].end());
for (const auto &[y, w] : fish[x])
{
if (x != 1)
{
posUncleared[x - 1].push_back({y, 0, 0LL, 0LL, 0LL});
}
if (x != n)
{
posUncleared[x + 1].push_back({y, 0, 0LL, 0LL, 0LL});
}
}
}
for (int x = 1 ; x <= n ; ++x)
{
std::sort(posUncleared[x].begin(), posUncleared[x].end(), [&](Position a, Position b)
{
return a.y < b.y;
});
for (Position p : posUncleared[x])
{
if (!pos[x].empty() && pos[x].back().y == p.y)
{
continue;
}
pos[x].push_back(p);
}
int ptrL = 0;
int ptrR = 0;
int ptrIN = 0;
llong inSum = 0;
llong leftSum = 0;
llong rightSum = 0;
for (Position &p : pos[x])
{
while (ptrL < fish[x - 1].size() && fish[x - 1][ptrL].first <= p.y)
{
leftSum += fish[x - 1][ptrL].second;
ptrL++;
}
while (ptrR < fish[x + 1].size() && fish[x + 1][ptrR].first <= p.y)
{
rightSum += fish[x + 1][ptrR].second;
ptrR++;
}
while (ptrIN < fish[x].size() && fish[x][ptrIN].first <= p.y)
{
inSum += fish[x][ptrIN].second;
ptrIN++;
}
p.in = inSum;
p.left = leftSum;
p.right = rightSum;
}
}
for (int x = 1 ; x <= n ; ++x)
{
for (Position &p : pos[x])
{
p.idx = ++cnt;
}
}
llong max = 0;
for (int x = n ; x >= 1 ; --x)
{
if (x + 3 <= n)
{
for (const Position &curr : pos[x + 3])
{
max = std::max(max, dp[curr.idx][0] + curr.left + curr.right);
}
}
for (int type = 0 ; type < 2 ; ++type)
{
for (const Position &curr : pos[x])
{
if (x == n)
{
continue;
}
dp[curr.idx][type] = max;
int l = -1, r = pos[x + 1].size(), mid;
while (l < r - 1)
{
mid = (l + r) >> 1;
if (pos[x + 1][mid].y < curr.y) l = mid;
else r = mid;
}
if (l >= 0)
{
dp[curr.idx][type] = std::max(dp[curr.idx][type], minusIN[x + 1][l]);
}
if (r < pos[x + 1].size() && type == 0)
{
dp[curr.idx][type] = std::max(dp[curr.idx][type], LR[x + 1][r] - curr.in - curr.right);
}
if (x + 2 <= n)
{
l = -1;
r = pos[x + 2].size();
while (l < r - 1)
{
mid = (l + r) >> 1;
if (pos[x + 2][mid].y < curr.y) l = mid;
else r = mid;
}
if (l >= 0)
{
dp[curr.idx][type] = std::max(dp[curr.idx][type], minusL[x + 2][l]);
}
if (r < pos[x + 2].size())
{
dp[curr.idx][type] = std::max(dp[curr.idx][type], LR[x + 2][r] - curr.right);
}
}
}
}
LR[x].reserve(pos[x].size());
minusL[x].reserve(pos[x].size());
minusIN[x].reserve(pos[x].size());
for (const Position &curr : pos[x])
{
minusIN[x].push_back(dp[curr.idx][1] - curr.in + curr.right);
LR[x].push_back(dp[curr.idx][0] + curr.left + curr.right);
minusL[x].push_back(dp[curr.idx][0] + curr.right);
}
for (int i = 1 ; i < pos[x].size() ; ++i)
{
minusIN[x][i] = std::max(minusIN[x][i - 1], minusIN[x][i]);
minusL[x][i] = std::max(minusIN[x][i - 1], minusL[x][i]);
}
for (int i = (int)pos[x].size() - 2 ; i >= 0 ; --i)
{
LR[x][i] = std::max(LR[x][i], LR[x][i + 1]);
}
}
for (int x = std::min(n, 3) ; x >= 1 ; --x)
{
for (const Position &curr : pos[x])
{
max = std::max(max, dp[curr.idx][0] + curr.left + curr.right);
}
}
return max;
}
Compilation message
fish.cpp: In function 'llong max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:93:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | while (ptrL < fish[x - 1].size() && fish[x - 1][ptrL].first <= p.y)
| ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:99:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | while (ptrR < fish[x + 1].size() && fish[x + 1][ptrR].first <= p.y)
| ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:105:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | while (ptrIN < fish[x].size() && fish[x][ptrIN].first <= p.y)
| ~~~~~~^~~~~~~~~~~~~~~~
fish.cpp:159:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | if (r < pos[x + 1].size() && type == 0)
| ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:180:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
180 | if (r < pos[x + 2].size())
| ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:198:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
198 | for (int i = 1 ; i < pos[x].size() ; ++i)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
60732 KB |
Output is correct |
2 |
Correct |
76 ms |
62936 KB |
Output is correct |
3 |
Correct |
26 ms |
49564 KB |
Output is correct |
4 |
Correct |
25 ms |
49532 KB |
Output is correct |
5 |
Correct |
244 ms |
120520 KB |
Output is correct |
6 |
Correct |
277 ms |
138640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
49524 KB |
Output is correct |
2 |
Correct |
144 ms |
79320 KB |
Output is correct |
3 |
Correct |
179 ms |
86444 KB |
Output is correct |
4 |
Correct |
65 ms |
60848 KB |
Output is correct |
5 |
Correct |
75 ms |
62956 KB |
Output is correct |
6 |
Correct |
23 ms |
49620 KB |
Output is correct |
7 |
Correct |
23 ms |
49568 KB |
Output is correct |
8 |
Correct |
23 ms |
49584 KB |
Output is correct |
9 |
Correct |
24 ms |
49624 KB |
Output is correct |
10 |
Correct |
27 ms |
49528 KB |
Output is correct |
11 |
Correct |
26 ms |
49620 KB |
Output is correct |
12 |
Correct |
86 ms |
68640 KB |
Output is correct |
13 |
Correct |
98 ms |
73068 KB |
Output is correct |
14 |
Correct |
78 ms |
65292 KB |
Output is correct |
15 |
Correct |
90 ms |
66784 KB |
Output is correct |
16 |
Correct |
81 ms |
65344 KB |
Output is correct |
17 |
Correct |
86 ms |
66788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
49620 KB |
Output is correct |
2 |
Correct |
26 ms |
49520 KB |
Output is correct |
3 |
Correct |
64 ms |
65876 KB |
Output is correct |
4 |
Correct |
52 ms |
60792 KB |
Output is correct |
5 |
Correct |
95 ms |
78560 KB |
Output is correct |
6 |
Correct |
97 ms |
78528 KB |
Output is correct |
7 |
Correct |
102 ms |
78564 KB |
Output is correct |
8 |
Correct |
95 ms |
78564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
49620 KB |
Output is correct |
2 |
Correct |
27 ms |
49588 KB |
Output is correct |
3 |
Correct |
23 ms |
49620 KB |
Output is correct |
4 |
Correct |
23 ms |
49604 KB |
Output is correct |
5 |
Correct |
28 ms |
49512 KB |
Output is correct |
6 |
Correct |
24 ms |
49620 KB |
Output is correct |
7 |
Correct |
23 ms |
49620 KB |
Output is correct |
8 |
Correct |
23 ms |
49620 KB |
Output is correct |
9 |
Correct |
23 ms |
49728 KB |
Output is correct |
10 |
Correct |
24 ms |
50044 KB |
Output is correct |
11 |
Correct |
25 ms |
49748 KB |
Output is correct |
12 |
Correct |
24 ms |
49920 KB |
Output is correct |
13 |
Correct |
24 ms |
49652 KB |
Output is correct |
14 |
Correct |
23 ms |
49748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
49620 KB |
Output is correct |
2 |
Correct |
27 ms |
49588 KB |
Output is correct |
3 |
Correct |
23 ms |
49620 KB |
Output is correct |
4 |
Correct |
23 ms |
49604 KB |
Output is correct |
5 |
Correct |
28 ms |
49512 KB |
Output is correct |
6 |
Correct |
24 ms |
49620 KB |
Output is correct |
7 |
Correct |
23 ms |
49620 KB |
Output is correct |
8 |
Correct |
23 ms |
49620 KB |
Output is correct |
9 |
Correct |
23 ms |
49728 KB |
Output is correct |
10 |
Correct |
24 ms |
50044 KB |
Output is correct |
11 |
Correct |
25 ms |
49748 KB |
Output is correct |
12 |
Correct |
24 ms |
49920 KB |
Output is correct |
13 |
Correct |
24 ms |
49652 KB |
Output is correct |
14 |
Correct |
23 ms |
49748 KB |
Output is correct |
15 |
Correct |
28 ms |
49660 KB |
Output is correct |
16 |
Correct |
25 ms |
50140 KB |
Output is correct |
17 |
Correct |
56 ms |
59000 KB |
Output is correct |
18 |
Correct |
56 ms |
59372 KB |
Output is correct |
19 |
Correct |
53 ms |
60072 KB |
Output is correct |
20 |
Correct |
51 ms |
59468 KB |
Output is correct |
21 |
Correct |
51 ms |
59460 KB |
Output is correct |
22 |
Correct |
80 ms |
67180 KB |
Output is correct |
23 |
Correct |
30 ms |
52044 KB |
Output is correct |
24 |
Correct |
49 ms |
57332 KB |
Output is correct |
25 |
Correct |
24 ms |
49876 KB |
Output is correct |
26 |
Correct |
29 ms |
51764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
49620 KB |
Output is correct |
2 |
Correct |
27 ms |
49588 KB |
Output is correct |
3 |
Correct |
23 ms |
49620 KB |
Output is correct |
4 |
Correct |
23 ms |
49604 KB |
Output is correct |
5 |
Correct |
28 ms |
49512 KB |
Output is correct |
6 |
Correct |
24 ms |
49620 KB |
Output is correct |
7 |
Correct |
23 ms |
49620 KB |
Output is correct |
8 |
Correct |
23 ms |
49620 KB |
Output is correct |
9 |
Correct |
23 ms |
49728 KB |
Output is correct |
10 |
Correct |
24 ms |
50044 KB |
Output is correct |
11 |
Correct |
25 ms |
49748 KB |
Output is correct |
12 |
Correct |
24 ms |
49920 KB |
Output is correct |
13 |
Correct |
24 ms |
49652 KB |
Output is correct |
14 |
Correct |
23 ms |
49748 KB |
Output is correct |
15 |
Correct |
28 ms |
49660 KB |
Output is correct |
16 |
Correct |
25 ms |
50140 KB |
Output is correct |
17 |
Correct |
56 ms |
59000 KB |
Output is correct |
18 |
Correct |
56 ms |
59372 KB |
Output is correct |
19 |
Correct |
53 ms |
60072 KB |
Output is correct |
20 |
Correct |
51 ms |
59468 KB |
Output is correct |
21 |
Correct |
51 ms |
59460 KB |
Output is correct |
22 |
Correct |
80 ms |
67180 KB |
Output is correct |
23 |
Correct |
30 ms |
52044 KB |
Output is correct |
24 |
Correct |
49 ms |
57332 KB |
Output is correct |
25 |
Correct |
24 ms |
49876 KB |
Output is correct |
26 |
Correct |
29 ms |
51764 KB |
Output is correct |
27 |
Correct |
25 ms |
50516 KB |
Output is correct |
28 |
Correct |
211 ms |
92852 KB |
Output is correct |
29 |
Correct |
266 ms |
114884 KB |
Output is correct |
30 |
Correct |
310 ms |
131152 KB |
Output is correct |
31 |
Correct |
299 ms |
130936 KB |
Output is correct |
32 |
Correct |
242 ms |
105612 KB |
Output is correct |
33 |
Correct |
294 ms |
131304 KB |
Output is correct |
34 |
Correct |
299 ms |
131404 KB |
Output is correct |
35 |
Correct |
121 ms |
83916 KB |
Output is correct |
36 |
Correct |
297 ms |
128188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
49620 KB |
Output is correct |
2 |
Correct |
26 ms |
49520 KB |
Output is correct |
3 |
Correct |
64 ms |
65876 KB |
Output is correct |
4 |
Correct |
52 ms |
60792 KB |
Output is correct |
5 |
Correct |
95 ms |
78560 KB |
Output is correct |
6 |
Correct |
97 ms |
78528 KB |
Output is correct |
7 |
Correct |
102 ms |
78564 KB |
Output is correct |
8 |
Correct |
95 ms |
78564 KB |
Output is correct |
9 |
Correct |
145 ms |
83184 KB |
Output is correct |
10 |
Correct |
93 ms |
70692 KB |
Output is correct |
11 |
Correct |
156 ms |
91864 KB |
Output is correct |
12 |
Correct |
23 ms |
49620 KB |
Output is correct |
13 |
Correct |
22 ms |
49620 KB |
Output is correct |
14 |
Correct |
25 ms |
49652 KB |
Output is correct |
15 |
Correct |
22 ms |
49508 KB |
Output is correct |
16 |
Correct |
25 ms |
49596 KB |
Output is correct |
17 |
Correct |
23 ms |
49620 KB |
Output is correct |
18 |
Correct |
26 ms |
49620 KB |
Output is correct |
19 |
Correct |
26 ms |
49548 KB |
Output is correct |
20 |
Correct |
26 ms |
49620 KB |
Output is correct |
21 |
Correct |
25 ms |
49584 KB |
Output is correct |
22 |
Correct |
128 ms |
82620 KB |
Output is correct |
23 |
Correct |
201 ms |
102272 KB |
Output is correct |
24 |
Correct |
189 ms |
104320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
60732 KB |
Output is correct |
2 |
Correct |
76 ms |
62936 KB |
Output is correct |
3 |
Correct |
26 ms |
49564 KB |
Output is correct |
4 |
Correct |
25 ms |
49532 KB |
Output is correct |
5 |
Correct |
244 ms |
120520 KB |
Output is correct |
6 |
Correct |
277 ms |
138640 KB |
Output is correct |
7 |
Correct |
23 ms |
49524 KB |
Output is correct |
8 |
Correct |
144 ms |
79320 KB |
Output is correct |
9 |
Correct |
179 ms |
86444 KB |
Output is correct |
10 |
Correct |
65 ms |
60848 KB |
Output is correct |
11 |
Correct |
75 ms |
62956 KB |
Output is correct |
12 |
Correct |
23 ms |
49620 KB |
Output is correct |
13 |
Correct |
23 ms |
49568 KB |
Output is correct |
14 |
Correct |
23 ms |
49584 KB |
Output is correct |
15 |
Correct |
24 ms |
49624 KB |
Output is correct |
16 |
Correct |
27 ms |
49528 KB |
Output is correct |
17 |
Correct |
26 ms |
49620 KB |
Output is correct |
18 |
Correct |
86 ms |
68640 KB |
Output is correct |
19 |
Correct |
98 ms |
73068 KB |
Output is correct |
20 |
Correct |
78 ms |
65292 KB |
Output is correct |
21 |
Correct |
90 ms |
66784 KB |
Output is correct |
22 |
Correct |
81 ms |
65344 KB |
Output is correct |
23 |
Correct |
86 ms |
66788 KB |
Output is correct |
24 |
Correct |
26 ms |
49620 KB |
Output is correct |
25 |
Correct |
26 ms |
49520 KB |
Output is correct |
26 |
Correct |
64 ms |
65876 KB |
Output is correct |
27 |
Correct |
52 ms |
60792 KB |
Output is correct |
28 |
Correct |
95 ms |
78560 KB |
Output is correct |
29 |
Correct |
97 ms |
78528 KB |
Output is correct |
30 |
Correct |
102 ms |
78564 KB |
Output is correct |
31 |
Correct |
95 ms |
78564 KB |
Output is correct |
32 |
Correct |
23 ms |
49620 KB |
Output is correct |
33 |
Correct |
27 ms |
49588 KB |
Output is correct |
34 |
Correct |
23 ms |
49620 KB |
Output is correct |
35 |
Correct |
23 ms |
49604 KB |
Output is correct |
36 |
Correct |
28 ms |
49512 KB |
Output is correct |
37 |
Correct |
24 ms |
49620 KB |
Output is correct |
38 |
Correct |
23 ms |
49620 KB |
Output is correct |
39 |
Correct |
23 ms |
49620 KB |
Output is correct |
40 |
Correct |
23 ms |
49728 KB |
Output is correct |
41 |
Correct |
24 ms |
50044 KB |
Output is correct |
42 |
Correct |
25 ms |
49748 KB |
Output is correct |
43 |
Correct |
24 ms |
49920 KB |
Output is correct |
44 |
Correct |
24 ms |
49652 KB |
Output is correct |
45 |
Correct |
23 ms |
49748 KB |
Output is correct |
46 |
Correct |
28 ms |
49660 KB |
Output is correct |
47 |
Correct |
25 ms |
50140 KB |
Output is correct |
48 |
Correct |
56 ms |
59000 KB |
Output is correct |
49 |
Correct |
56 ms |
59372 KB |
Output is correct |
50 |
Correct |
53 ms |
60072 KB |
Output is correct |
51 |
Correct |
51 ms |
59468 KB |
Output is correct |
52 |
Correct |
51 ms |
59460 KB |
Output is correct |
53 |
Correct |
80 ms |
67180 KB |
Output is correct |
54 |
Correct |
30 ms |
52044 KB |
Output is correct |
55 |
Correct |
49 ms |
57332 KB |
Output is correct |
56 |
Correct |
24 ms |
49876 KB |
Output is correct |
57 |
Correct |
29 ms |
51764 KB |
Output is correct |
58 |
Correct |
25 ms |
50516 KB |
Output is correct |
59 |
Correct |
211 ms |
92852 KB |
Output is correct |
60 |
Correct |
266 ms |
114884 KB |
Output is correct |
61 |
Correct |
310 ms |
131152 KB |
Output is correct |
62 |
Correct |
299 ms |
130936 KB |
Output is correct |
63 |
Correct |
242 ms |
105612 KB |
Output is correct |
64 |
Correct |
294 ms |
131304 KB |
Output is correct |
65 |
Correct |
299 ms |
131404 KB |
Output is correct |
66 |
Correct |
121 ms |
83916 KB |
Output is correct |
67 |
Correct |
297 ms |
128188 KB |
Output is correct |
68 |
Correct |
145 ms |
83184 KB |
Output is correct |
69 |
Correct |
93 ms |
70692 KB |
Output is correct |
70 |
Correct |
156 ms |
91864 KB |
Output is correct |
71 |
Correct |
23 ms |
49620 KB |
Output is correct |
72 |
Correct |
22 ms |
49620 KB |
Output is correct |
73 |
Correct |
25 ms |
49652 KB |
Output is correct |
74 |
Correct |
22 ms |
49508 KB |
Output is correct |
75 |
Correct |
25 ms |
49596 KB |
Output is correct |
76 |
Correct |
23 ms |
49620 KB |
Output is correct |
77 |
Correct |
26 ms |
49620 KB |
Output is correct |
78 |
Correct |
26 ms |
49548 KB |
Output is correct |
79 |
Correct |
26 ms |
49620 KB |
Output is correct |
80 |
Correct |
25 ms |
49584 KB |
Output is correct |
81 |
Correct |
128 ms |
82620 KB |
Output is correct |
82 |
Correct |
201 ms |
102272 KB |
Output is correct |
83 |
Correct |
189 ms |
104320 KB |
Output is correct |
84 |
Correct |
328 ms |
127420 KB |
Output is correct |
85 |
Correct |
324 ms |
126964 KB |
Output is correct |
86 |
Correct |
352 ms |
146160 KB |
Output is correct |
87 |
Correct |
361 ms |
147520 KB |
Output is correct |
88 |
Correct |
26 ms |
49620 KB |
Output is correct |
89 |
Correct |
349 ms |
147416 KB |
Output is correct |
90 |
Correct |
321 ms |
138180 KB |
Output is correct |
91 |
Correct |
281 ms |
125152 KB |
Output is correct |