#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;
struct SegmentTree
{
llong tree[1 << 21];
SegmentTree()
{
std::fill(tree, tree + (1 << 21), (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) >> 1;
if (queryIdx <= mid) update(l, mid, node << 1, queryIdx, value);
else update(mid + 1, r, (node << 1) | 1, queryIdx, value);
tree[node] = std::max(tree[node << 1], tree[(node << 1) | 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) >> 1;
if (queryL <= mid) ans = std::max(ans, query(l, mid, node << 1, queryL, queryR));
if (mid + 1 <= queryR) ans = std::max(ans, query(mid + 1, r, (node << 1) | 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, minusL, minusIN;
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.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.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) >> 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.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.query(pos[x + 2][r].idx, pos[x + 2].back().idx) - curr.right);
}
}
}
for (const Position &curr : pos[x])
{
minusIN.update(curr.idx, dp[curr.idx][1] - curr.in + curr.right);
LR.update(curr.idx, dp[curr.idx][0] + curr.left + curr.right);
minusL.update(curr.idx, dp[curr.idx][0] + curr.right);
}
}
}
return LR.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:138: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]
138 | while (ptrL < fish[x - 1].size() && fish[x - 1][ptrL].first <= p.y)
| ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:144: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]
144 | while (ptrR < fish[x + 1].size() && fish[x + 1][ptrR].first <= p.y)
| ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:150: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]
150 | while (ptrIN < fish[x].size() && fish[x][ptrIN].first <= p.y)
| ~~~~~~^~~~~~~~~~~~~~~~
fish.cpp:204:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
204 | if (r < pos[x + 1].size() && type == 0)
| ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:225:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
225 | if (r < pos[x + 2].size())
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
67768 KB |
Output is correct |
2 |
Correct |
109 ms |
69016 KB |
Output is correct |
3 |
Correct |
23 ms |
56584 KB |
Output is correct |
4 |
Correct |
28 ms |
56608 KB |
Output is correct |
5 |
Correct |
681 ms |
117760 KB |
Output is correct |
6 |
Correct |
716 ms |
130128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
56576 KB |
Output is correct |
2 |
Correct |
369 ms |
80772 KB |
Output is correct |
3 |
Correct |
427 ms |
86520 KB |
Output is correct |
4 |
Correct |
105 ms |
67768 KB |
Output is correct |
5 |
Correct |
113 ms |
68912 KB |
Output is correct |
6 |
Correct |
23 ms |
56592 KB |
Output is correct |
7 |
Correct |
22 ms |
56528 KB |
Output is correct |
8 |
Correct |
23 ms |
56488 KB |
Output is correct |
9 |
Correct |
22 ms |
56588 KB |
Output is correct |
10 |
Correct |
24 ms |
56496 KB |
Output is correct |
11 |
Correct |
25 ms |
56556 KB |
Output is correct |
12 |
Correct |
176 ms |
72960 KB |
Output is correct |
13 |
Correct |
208 ms |
75648 KB |
Output is correct |
14 |
Correct |
149 ms |
69600 KB |
Output is correct |
15 |
Correct |
232 ms |
70816 KB |
Output is correct |
16 |
Correct |
154 ms |
69596 KB |
Output is correct |
17 |
Correct |
175 ms |
70904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
56532 KB |
Output is correct |
2 |
Correct |
24 ms |
56520 KB |
Output is correct |
3 |
Correct |
108 ms |
67412 KB |
Output is correct |
4 |
Correct |
85 ms |
63964 KB |
Output is correct |
5 |
Correct |
147 ms |
76040 KB |
Output is correct |
6 |
Correct |
140 ms |
76080 KB |
Output is correct |
7 |
Correct |
142 ms |
76120 KB |
Output is correct |
8 |
Correct |
146 ms |
76108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
56604 KB |
Output is correct |
2 |
Correct |
25 ms |
56588 KB |
Output is correct |
3 |
Correct |
24 ms |
56524 KB |
Output is correct |
4 |
Correct |
23 ms |
56512 KB |
Output is correct |
5 |
Correct |
22 ms |
56548 KB |
Output is correct |
6 |
Correct |
22 ms |
56560 KB |
Output is correct |
7 |
Correct |
23 ms |
56536 KB |
Output is correct |
8 |
Correct |
24 ms |
56584 KB |
Output is correct |
9 |
Correct |
24 ms |
56604 KB |
Output is correct |
10 |
Correct |
31 ms |
56924 KB |
Output is correct |
11 |
Correct |
31 ms |
56660 KB |
Output is correct |
12 |
Correct |
32 ms |
56808 KB |
Output is correct |
13 |
Correct |
24 ms |
56532 KB |
Output is correct |
14 |
Correct |
24 ms |
56788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
56604 KB |
Output is correct |
2 |
Correct |
25 ms |
56588 KB |
Output is correct |
3 |
Correct |
24 ms |
56524 KB |
Output is correct |
4 |
Correct |
23 ms |
56512 KB |
Output is correct |
5 |
Correct |
22 ms |
56548 KB |
Output is correct |
6 |
Correct |
22 ms |
56560 KB |
Output is correct |
7 |
Correct |
23 ms |
56536 KB |
Output is correct |
8 |
Correct |
24 ms |
56584 KB |
Output is correct |
9 |
Correct |
24 ms |
56604 KB |
Output is correct |
10 |
Correct |
31 ms |
56924 KB |
Output is correct |
11 |
Correct |
31 ms |
56660 KB |
Output is correct |
12 |
Correct |
32 ms |
56808 KB |
Output is correct |
13 |
Correct |
24 ms |
56532 KB |
Output is correct |
14 |
Correct |
24 ms |
56788 KB |
Output is correct |
15 |
Correct |
22 ms |
56632 KB |
Output is correct |
16 |
Correct |
26 ms |
56916 KB |
Output is correct |
17 |
Correct |
120 ms |
64568 KB |
Output is correct |
18 |
Correct |
112 ms |
65196 KB |
Output is correct |
19 |
Correct |
123 ms |
65752 KB |
Output is correct |
20 |
Correct |
104 ms |
65452 KB |
Output is correct |
21 |
Correct |
102 ms |
65428 KB |
Output is correct |
22 |
Correct |
195 ms |
72100 KB |
Output is correct |
23 |
Correct |
47 ms |
58580 KB |
Output is correct |
24 |
Correct |
101 ms |
63160 KB |
Output is correct |
25 |
Correct |
27 ms |
56776 KB |
Output is correct |
26 |
Correct |
52 ms |
58388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
56604 KB |
Output is correct |
2 |
Correct |
25 ms |
56588 KB |
Output is correct |
3 |
Correct |
24 ms |
56524 KB |
Output is correct |
4 |
Correct |
23 ms |
56512 KB |
Output is correct |
5 |
Correct |
22 ms |
56548 KB |
Output is correct |
6 |
Correct |
22 ms |
56560 KB |
Output is correct |
7 |
Correct |
23 ms |
56536 KB |
Output is correct |
8 |
Correct |
24 ms |
56584 KB |
Output is correct |
9 |
Correct |
24 ms |
56604 KB |
Output is correct |
10 |
Correct |
31 ms |
56924 KB |
Output is correct |
11 |
Correct |
31 ms |
56660 KB |
Output is correct |
12 |
Correct |
32 ms |
56808 KB |
Output is correct |
13 |
Correct |
24 ms |
56532 KB |
Output is correct |
14 |
Correct |
24 ms |
56788 KB |
Output is correct |
15 |
Correct |
22 ms |
56632 KB |
Output is correct |
16 |
Correct |
26 ms |
56916 KB |
Output is correct |
17 |
Correct |
120 ms |
64568 KB |
Output is correct |
18 |
Correct |
112 ms |
65196 KB |
Output is correct |
19 |
Correct |
123 ms |
65752 KB |
Output is correct |
20 |
Correct |
104 ms |
65452 KB |
Output is correct |
21 |
Correct |
102 ms |
65428 KB |
Output is correct |
22 |
Correct |
195 ms |
72100 KB |
Output is correct |
23 |
Correct |
47 ms |
58580 KB |
Output is correct |
24 |
Correct |
101 ms |
63160 KB |
Output is correct |
25 |
Correct |
27 ms |
56776 KB |
Output is correct |
26 |
Correct |
52 ms |
58388 KB |
Output is correct |
27 |
Correct |
28 ms |
57248 KB |
Output is correct |
28 |
Correct |
521 ms |
93736 KB |
Output is correct |
29 |
Correct |
796 ms |
111432 KB |
Output is correct |
30 |
Correct |
970 ms |
124356 KB |
Output is correct |
31 |
Correct |
984 ms |
130000 KB |
Output is correct |
32 |
Correct |
614 ms |
110780 KB |
Output is correct |
33 |
Correct |
998 ms |
129960 KB |
Output is correct |
34 |
Execution timed out |
1028 ms |
130208 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
56532 KB |
Output is correct |
2 |
Correct |
24 ms |
56520 KB |
Output is correct |
3 |
Correct |
108 ms |
67412 KB |
Output is correct |
4 |
Correct |
85 ms |
63964 KB |
Output is correct |
5 |
Correct |
147 ms |
76040 KB |
Output is correct |
6 |
Correct |
140 ms |
76080 KB |
Output is correct |
7 |
Correct |
142 ms |
76120 KB |
Output is correct |
8 |
Correct |
146 ms |
76108 KB |
Output is correct |
9 |
Correct |
242 ms |
80852 KB |
Output is correct |
10 |
Correct |
181 ms |
73036 KB |
Output is correct |
11 |
Correct |
328 ms |
89456 KB |
Output is correct |
12 |
Correct |
23 ms |
56548 KB |
Output is correct |
13 |
Correct |
22 ms |
56532 KB |
Output is correct |
14 |
Correct |
22 ms |
56516 KB |
Output is correct |
15 |
Correct |
22 ms |
56500 KB |
Output is correct |
16 |
Correct |
22 ms |
56504 KB |
Output is correct |
17 |
Correct |
22 ms |
56564 KB |
Output is correct |
18 |
Correct |
26 ms |
56532 KB |
Output is correct |
19 |
Correct |
28 ms |
56540 KB |
Output is correct |
20 |
Correct |
27 ms |
56532 KB |
Output is correct |
21 |
Correct |
25 ms |
56532 KB |
Output is correct |
22 |
Correct |
277 ms |
80752 KB |
Output is correct |
23 |
Correct |
471 ms |
97144 KB |
Output is correct |
24 |
Correct |
513 ms |
98432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
67768 KB |
Output is correct |
2 |
Correct |
109 ms |
69016 KB |
Output is correct |
3 |
Correct |
23 ms |
56584 KB |
Output is correct |
4 |
Correct |
28 ms |
56608 KB |
Output is correct |
5 |
Correct |
681 ms |
117760 KB |
Output is correct |
6 |
Correct |
716 ms |
130128 KB |
Output is correct |
7 |
Correct |
26 ms |
56576 KB |
Output is correct |
8 |
Correct |
369 ms |
80772 KB |
Output is correct |
9 |
Correct |
427 ms |
86520 KB |
Output is correct |
10 |
Correct |
105 ms |
67768 KB |
Output is correct |
11 |
Correct |
113 ms |
68912 KB |
Output is correct |
12 |
Correct |
23 ms |
56592 KB |
Output is correct |
13 |
Correct |
22 ms |
56528 KB |
Output is correct |
14 |
Correct |
23 ms |
56488 KB |
Output is correct |
15 |
Correct |
22 ms |
56588 KB |
Output is correct |
16 |
Correct |
24 ms |
56496 KB |
Output is correct |
17 |
Correct |
25 ms |
56556 KB |
Output is correct |
18 |
Correct |
176 ms |
72960 KB |
Output is correct |
19 |
Correct |
208 ms |
75648 KB |
Output is correct |
20 |
Correct |
149 ms |
69600 KB |
Output is correct |
21 |
Correct |
232 ms |
70816 KB |
Output is correct |
22 |
Correct |
154 ms |
69596 KB |
Output is correct |
23 |
Correct |
175 ms |
70904 KB |
Output is correct |
24 |
Correct |
24 ms |
56532 KB |
Output is correct |
25 |
Correct |
24 ms |
56520 KB |
Output is correct |
26 |
Correct |
108 ms |
67412 KB |
Output is correct |
27 |
Correct |
85 ms |
63964 KB |
Output is correct |
28 |
Correct |
147 ms |
76040 KB |
Output is correct |
29 |
Correct |
140 ms |
76080 KB |
Output is correct |
30 |
Correct |
142 ms |
76120 KB |
Output is correct |
31 |
Correct |
146 ms |
76108 KB |
Output is correct |
32 |
Correct |
27 ms |
56604 KB |
Output is correct |
33 |
Correct |
25 ms |
56588 KB |
Output is correct |
34 |
Correct |
24 ms |
56524 KB |
Output is correct |
35 |
Correct |
23 ms |
56512 KB |
Output is correct |
36 |
Correct |
22 ms |
56548 KB |
Output is correct |
37 |
Correct |
22 ms |
56560 KB |
Output is correct |
38 |
Correct |
23 ms |
56536 KB |
Output is correct |
39 |
Correct |
24 ms |
56584 KB |
Output is correct |
40 |
Correct |
24 ms |
56604 KB |
Output is correct |
41 |
Correct |
31 ms |
56924 KB |
Output is correct |
42 |
Correct |
31 ms |
56660 KB |
Output is correct |
43 |
Correct |
32 ms |
56808 KB |
Output is correct |
44 |
Correct |
24 ms |
56532 KB |
Output is correct |
45 |
Correct |
24 ms |
56788 KB |
Output is correct |
46 |
Correct |
22 ms |
56632 KB |
Output is correct |
47 |
Correct |
26 ms |
56916 KB |
Output is correct |
48 |
Correct |
120 ms |
64568 KB |
Output is correct |
49 |
Correct |
112 ms |
65196 KB |
Output is correct |
50 |
Correct |
123 ms |
65752 KB |
Output is correct |
51 |
Correct |
104 ms |
65452 KB |
Output is correct |
52 |
Correct |
102 ms |
65428 KB |
Output is correct |
53 |
Correct |
195 ms |
72100 KB |
Output is correct |
54 |
Correct |
47 ms |
58580 KB |
Output is correct |
55 |
Correct |
101 ms |
63160 KB |
Output is correct |
56 |
Correct |
27 ms |
56776 KB |
Output is correct |
57 |
Correct |
52 ms |
58388 KB |
Output is correct |
58 |
Correct |
28 ms |
57248 KB |
Output is correct |
59 |
Correct |
521 ms |
93736 KB |
Output is correct |
60 |
Correct |
796 ms |
111432 KB |
Output is correct |
61 |
Correct |
970 ms |
124356 KB |
Output is correct |
62 |
Correct |
984 ms |
130000 KB |
Output is correct |
63 |
Correct |
614 ms |
110780 KB |
Output is correct |
64 |
Correct |
998 ms |
129960 KB |
Output is correct |
65 |
Execution timed out |
1028 ms |
130208 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |