#include "fish.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
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;
struct SegmentTree
{
llong tree[4*MAXM];
SegmentTree()
{
std::fill(tree, tree + 4*MAXM, (llong)-1e18);
}
void update(int l, int r, int node, int queryIdx, llong value)
{
if (l == r)
{
tree[node] = value;
return;
}
int mid = (l + r) / 2;
if (queryIdx <= mid) update(l, mid, 2*node, queryIdx, value);
else update(mid + 1, r, 2*node + 1, queryIdx, value);
tree[node] = std::max(tree[2*node], tree[2*node + 1]);
}
llong query(int l, int r, int node, int queryL, int queryR)
{
if (queryL <= l && r <= queryR)
{
return tree[node];
}
llong ans = -1e18;
int mid = (l + r) / 2;
if (queryL <= mid) ans = std::max(ans, query(l, mid, 2*node, queryL, queryR));
if (mid + 1 <= queryR) ans = std::max(ans, query(mid + 1, r, 2*node + 1, queryL, queryR));
return ans;
}
void update(int pos, llong value)
{
update(1, cnt, 1, pos, value);
}
llong query(int l, int r)
{
return query(1, cnt, 1, l, r);
}
};
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];
SegmentTree LR[2], minusL[2], minusIN[2];
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;
}
}
for (int x = n ; x >= 1 ; --x)
{
for (int type = 0 ; type < 2 ; ++type)
{
for (const Position &curr : pos[x])
{
if (x == n)
{
minusL[type].update(curr.idx, dp[curr.idx][type] + curr.right);
minusIN[type].update(curr.idx, dp[curr.idx][type] - curr.in + curr.right);
LR[type].update(curr.idx, dp[curr.idx][type] + curr.left + curr.right);
continue;
}
int l = -1, r = pos[x + 1].size(), mid;
while (l < r - 1)
{
mid = (l + r) / 2;
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[1].query(pos[x + 1][0].idx, pos[x + 1][l].idx));
}
if (r < pos[x + 1].size() && type == 0)
{
dp[curr.idx][type] = std::max(dp[curr.idx][type], LR[0].query(pos[x + 1][r].idx, pos[x + 1].back().idx) - curr.in - curr.right);
}
if (x + 2 <= n)
{
l = -1;
r = pos[x + 2].size();
while (l < r - 1)
{
mid = (l + r) / 2;
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[0].query(pos[x + 2][0].idx, pos[x + 2][l].idx));
}
if (r < pos[x + 2].size())
{
dp[curr.idx][type] = std::max(dp[curr.idx][type], LR[0].query(pos[x + 2][r].idx, pos[x + 2].back().idx) - curr.right);
}
}
int firstIdx = pos[x].back().idx + 1;
if (x + 1 <= n && !pos[x + 1].empty())
{
firstIdx = pos[x + 1].back().idx + 1;
}
if (x + 2 <= n && !pos[x + 2].empty())
{
firstIdx = pos[x + 2].back().idx + 1;
}
if (firstIdx <= cnt)
{
dp[curr.idx][type] = std::max(dp[curr.idx][type], LR[0].query(firstIdx, cnt));
}
minusL[type].update(curr.idx, dp[curr.idx][type] + curr.right);
minusIN[type].update(curr.idx, dp[curr.idx][type] - curr.in + curr.right);
LR[type].update(curr.idx, dp[curr.idx][type] + curr.left + curr.right);
}
}
}
return LR[0].query(1, cnt);
}
Compilation message
fish.cpp: In function 'llong max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:135: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]
135 | while (ptrL < fish[x - 1].size() && fish[x - 1][ptrL].first <= p.y)
| ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:141: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]
141 | while (ptrR < fish[x + 1].size() && fish[x + 1][ptrR].first <= p.y)
| ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:147: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]
147 | while (ptrIN < fish[x].size() && fish[x][ptrIN].first <= p.y)
| ~~~~~~^~~~~~~~~~~~~~~~
fish.cpp:194:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
194 | if (r < pos[x + 1].size() && type == 0)
| ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:215:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
215 | if (r < pos[x + 2].size())
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
131260 KB |
Output is correct |
2 |
Correct |
151 ms |
132712 KB |
Output is correct |
3 |
Correct |
48 ms |
120008 KB |
Output is correct |
4 |
Correct |
54 ms |
120012 KB |
Output is correct |
5 |
Correct |
805 ms |
180212 KB |
Output is correct |
6 |
Correct |
911 ms |
194004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
120008 KB |
Output is correct |
2 |
Correct |
392 ms |
144000 KB |
Output is correct |
3 |
Correct |
488 ms |
148788 KB |
Output is correct |
4 |
Correct |
119 ms |
131472 KB |
Output is correct |
5 |
Correct |
136 ms |
132640 KB |
Output is correct |
6 |
Correct |
48 ms |
120028 KB |
Output is correct |
7 |
Correct |
47 ms |
120140 KB |
Output is correct |
8 |
Correct |
52 ms |
120216 KB |
Output is correct |
9 |
Correct |
49 ms |
120068 KB |
Output is correct |
10 |
Correct |
50 ms |
120016 KB |
Output is correct |
11 |
Correct |
48 ms |
120012 KB |
Output is correct |
12 |
Correct |
211 ms |
136644 KB |
Output is correct |
13 |
Correct |
243 ms |
139036 KB |
Output is correct |
14 |
Correct |
190 ms |
133124 KB |
Output is correct |
15 |
Correct |
243 ms |
133980 KB |
Output is correct |
16 |
Correct |
186 ms |
133108 KB |
Output is correct |
17 |
Correct |
226 ms |
133824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
120012 KB |
Output is correct |
2 |
Correct |
50 ms |
120016 KB |
Output is correct |
3 |
Correct |
126 ms |
131048 KB |
Output is correct |
4 |
Correct |
101 ms |
127540 KB |
Output is correct |
5 |
Correct |
208 ms |
139872 KB |
Output is correct |
6 |
Correct |
189 ms |
139724 KB |
Output is correct |
7 |
Correct |
191 ms |
139800 KB |
Output is correct |
8 |
Correct |
196 ms |
139816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
120072 KB |
Output is correct |
2 |
Correct |
50 ms |
120156 KB |
Output is correct |
3 |
Correct |
48 ms |
120012 KB |
Output is correct |
4 |
Correct |
56 ms |
119940 KB |
Output is correct |
5 |
Correct |
48 ms |
119988 KB |
Output is correct |
6 |
Correct |
47 ms |
119960 KB |
Output is correct |
7 |
Correct |
48 ms |
119944 KB |
Output is correct |
8 |
Correct |
51 ms |
120036 KB |
Output is correct |
9 |
Correct |
47 ms |
120084 KB |
Output is correct |
10 |
Correct |
53 ms |
120352 KB |
Output is correct |
11 |
Correct |
49 ms |
120172 KB |
Output is correct |
12 |
Correct |
52 ms |
120296 KB |
Output is correct |
13 |
Correct |
47 ms |
120064 KB |
Output is correct |
14 |
Correct |
49 ms |
120140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
120072 KB |
Output is correct |
2 |
Correct |
50 ms |
120156 KB |
Output is correct |
3 |
Correct |
48 ms |
120012 KB |
Output is correct |
4 |
Correct |
56 ms |
119940 KB |
Output is correct |
5 |
Correct |
48 ms |
119988 KB |
Output is correct |
6 |
Correct |
47 ms |
119960 KB |
Output is correct |
7 |
Correct |
48 ms |
119944 KB |
Output is correct |
8 |
Correct |
51 ms |
120036 KB |
Output is correct |
9 |
Correct |
47 ms |
120084 KB |
Output is correct |
10 |
Correct |
53 ms |
120352 KB |
Output is correct |
11 |
Correct |
49 ms |
120172 KB |
Output is correct |
12 |
Correct |
52 ms |
120296 KB |
Output is correct |
13 |
Correct |
47 ms |
120064 KB |
Output is correct |
14 |
Correct |
49 ms |
120140 KB |
Output is correct |
15 |
Correct |
48 ms |
120084 KB |
Output is correct |
16 |
Correct |
54 ms |
120396 KB |
Output is correct |
17 |
Correct |
164 ms |
128872 KB |
Output is correct |
18 |
Correct |
148 ms |
129292 KB |
Output is correct |
19 |
Correct |
164 ms |
130276 KB |
Output is correct |
20 |
Correct |
144 ms |
129816 KB |
Output is correct |
21 |
Correct |
139 ms |
129804 KB |
Output is correct |
22 |
Correct |
249 ms |
137136 KB |
Output is correct |
23 |
Correct |
77 ms |
122164 KB |
Output is correct |
24 |
Correct |
140 ms |
127212 KB |
Output is correct |
25 |
Correct |
52 ms |
120452 KB |
Output is correct |
26 |
Correct |
80 ms |
121992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
120072 KB |
Output is correct |
2 |
Correct |
50 ms |
120156 KB |
Output is correct |
3 |
Correct |
48 ms |
120012 KB |
Output is correct |
4 |
Correct |
56 ms |
119940 KB |
Output is correct |
5 |
Correct |
48 ms |
119988 KB |
Output is correct |
6 |
Correct |
47 ms |
119960 KB |
Output is correct |
7 |
Correct |
48 ms |
119944 KB |
Output is correct |
8 |
Correct |
51 ms |
120036 KB |
Output is correct |
9 |
Correct |
47 ms |
120084 KB |
Output is correct |
10 |
Correct |
53 ms |
120352 KB |
Output is correct |
11 |
Correct |
49 ms |
120172 KB |
Output is correct |
12 |
Correct |
52 ms |
120296 KB |
Output is correct |
13 |
Correct |
47 ms |
120064 KB |
Output is correct |
14 |
Correct |
49 ms |
120140 KB |
Output is correct |
15 |
Correct |
48 ms |
120084 KB |
Output is correct |
16 |
Correct |
54 ms |
120396 KB |
Output is correct |
17 |
Correct |
164 ms |
128872 KB |
Output is correct |
18 |
Correct |
148 ms |
129292 KB |
Output is correct |
19 |
Correct |
164 ms |
130276 KB |
Output is correct |
20 |
Correct |
144 ms |
129816 KB |
Output is correct |
21 |
Correct |
139 ms |
129804 KB |
Output is correct |
22 |
Correct |
249 ms |
137136 KB |
Output is correct |
23 |
Correct |
77 ms |
122164 KB |
Output is correct |
24 |
Correct |
140 ms |
127212 KB |
Output is correct |
25 |
Correct |
52 ms |
120452 KB |
Output is correct |
26 |
Correct |
80 ms |
121992 KB |
Output is correct |
27 |
Correct |
61 ms |
120700 KB |
Output is correct |
28 |
Correct |
642 ms |
161012 KB |
Output is correct |
29 |
Correct |
959 ms |
180440 KB |
Output is correct |
30 |
Execution timed out |
1081 ms |
192112 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
120012 KB |
Output is correct |
2 |
Correct |
50 ms |
120016 KB |
Output is correct |
3 |
Correct |
126 ms |
131048 KB |
Output is correct |
4 |
Correct |
101 ms |
127540 KB |
Output is correct |
5 |
Correct |
208 ms |
139872 KB |
Output is correct |
6 |
Correct |
189 ms |
139724 KB |
Output is correct |
7 |
Correct |
191 ms |
139800 KB |
Output is correct |
8 |
Correct |
196 ms |
139816 KB |
Output is correct |
9 |
Correct |
340 ms |
144532 KB |
Output is correct |
10 |
Correct |
230 ms |
136748 KB |
Output is correct |
11 |
Correct |
379 ms |
153248 KB |
Output is correct |
12 |
Correct |
48 ms |
119976 KB |
Output is correct |
13 |
Correct |
52 ms |
120064 KB |
Output is correct |
14 |
Correct |
61 ms |
120056 KB |
Output is correct |
15 |
Correct |
49 ms |
119996 KB |
Output is correct |
16 |
Correct |
47 ms |
119944 KB |
Output is correct |
17 |
Correct |
49 ms |
120012 KB |
Output is correct |
18 |
Correct |
48 ms |
119996 KB |
Output is correct |
19 |
Correct |
49 ms |
120052 KB |
Output is correct |
20 |
Correct |
52 ms |
120012 KB |
Output is correct |
21 |
Correct |
48 ms |
120056 KB |
Output is correct |
22 |
Correct |
349 ms |
144572 KB |
Output is correct |
23 |
Correct |
616 ms |
164176 KB |
Output is correct |
24 |
Correct |
639 ms |
165896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
131260 KB |
Output is correct |
2 |
Correct |
151 ms |
132712 KB |
Output is correct |
3 |
Correct |
48 ms |
120008 KB |
Output is correct |
4 |
Correct |
54 ms |
120012 KB |
Output is correct |
5 |
Correct |
805 ms |
180212 KB |
Output is correct |
6 |
Correct |
911 ms |
194004 KB |
Output is correct |
7 |
Correct |
47 ms |
120008 KB |
Output is correct |
8 |
Correct |
392 ms |
144000 KB |
Output is correct |
9 |
Correct |
488 ms |
148788 KB |
Output is correct |
10 |
Correct |
119 ms |
131472 KB |
Output is correct |
11 |
Correct |
136 ms |
132640 KB |
Output is correct |
12 |
Correct |
48 ms |
120028 KB |
Output is correct |
13 |
Correct |
47 ms |
120140 KB |
Output is correct |
14 |
Correct |
52 ms |
120216 KB |
Output is correct |
15 |
Correct |
49 ms |
120068 KB |
Output is correct |
16 |
Correct |
50 ms |
120016 KB |
Output is correct |
17 |
Correct |
48 ms |
120012 KB |
Output is correct |
18 |
Correct |
211 ms |
136644 KB |
Output is correct |
19 |
Correct |
243 ms |
139036 KB |
Output is correct |
20 |
Correct |
190 ms |
133124 KB |
Output is correct |
21 |
Correct |
243 ms |
133980 KB |
Output is correct |
22 |
Correct |
186 ms |
133108 KB |
Output is correct |
23 |
Correct |
226 ms |
133824 KB |
Output is correct |
24 |
Correct |
51 ms |
120012 KB |
Output is correct |
25 |
Correct |
50 ms |
120016 KB |
Output is correct |
26 |
Correct |
126 ms |
131048 KB |
Output is correct |
27 |
Correct |
101 ms |
127540 KB |
Output is correct |
28 |
Correct |
208 ms |
139872 KB |
Output is correct |
29 |
Correct |
189 ms |
139724 KB |
Output is correct |
30 |
Correct |
191 ms |
139800 KB |
Output is correct |
31 |
Correct |
196 ms |
139816 KB |
Output is correct |
32 |
Correct |
59 ms |
120072 KB |
Output is correct |
33 |
Correct |
50 ms |
120156 KB |
Output is correct |
34 |
Correct |
48 ms |
120012 KB |
Output is correct |
35 |
Correct |
56 ms |
119940 KB |
Output is correct |
36 |
Correct |
48 ms |
119988 KB |
Output is correct |
37 |
Correct |
47 ms |
119960 KB |
Output is correct |
38 |
Correct |
48 ms |
119944 KB |
Output is correct |
39 |
Correct |
51 ms |
120036 KB |
Output is correct |
40 |
Correct |
47 ms |
120084 KB |
Output is correct |
41 |
Correct |
53 ms |
120352 KB |
Output is correct |
42 |
Correct |
49 ms |
120172 KB |
Output is correct |
43 |
Correct |
52 ms |
120296 KB |
Output is correct |
44 |
Correct |
47 ms |
120064 KB |
Output is correct |
45 |
Correct |
49 ms |
120140 KB |
Output is correct |
46 |
Correct |
48 ms |
120084 KB |
Output is correct |
47 |
Correct |
54 ms |
120396 KB |
Output is correct |
48 |
Correct |
164 ms |
128872 KB |
Output is correct |
49 |
Correct |
148 ms |
129292 KB |
Output is correct |
50 |
Correct |
164 ms |
130276 KB |
Output is correct |
51 |
Correct |
144 ms |
129816 KB |
Output is correct |
52 |
Correct |
139 ms |
129804 KB |
Output is correct |
53 |
Correct |
249 ms |
137136 KB |
Output is correct |
54 |
Correct |
77 ms |
122164 KB |
Output is correct |
55 |
Correct |
140 ms |
127212 KB |
Output is correct |
56 |
Correct |
52 ms |
120452 KB |
Output is correct |
57 |
Correct |
80 ms |
121992 KB |
Output is correct |
58 |
Correct |
61 ms |
120700 KB |
Output is correct |
59 |
Correct |
642 ms |
161012 KB |
Output is correct |
60 |
Correct |
959 ms |
180440 KB |
Output is correct |
61 |
Execution timed out |
1081 ms |
192112 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |