#include "fish.h"
#include "bits/stdc++.h"
#include <vector>
using namespace std;
vector<pair<int, long long> > cs[100010];
long long columnSum(int i, int R) {
if(R < 0) return 0;
if(cs[i].empty()) return 0;
int lb = 0, rb = cs[i].size() - 1;
while(lb < rb) {
int mid = (lb + rb + 1) >> 1;
if(cs[i][mid].first <= R) lb = mid;
else rb = mid - 1;
}
if(cs[i][lb].first <= R) return cs[i][lb].second;
else return 0;
}
vector<int> possible_heights[100010];
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
for(int i=0; i<M; i++) {
cs[X[i]].push_back({Y[i], W[i]});
possible_heights[X[i]].push_back(Y[i]);
}
for(int i=0; i<N; i++) {
possible_heights[i].push_back(0);
possible_heights[i].push_back(N);
}
for(int i=0; i<N; i++) {
sort(possible_heights[i].begin(), possible_heights[i].end());
}
for(int i=0; i<N; i++) sort(cs[i].begin(), cs[i].end());
for(int i=0; i<N; i++) {
for(int j=1; j<cs[i].size(); j++) {
cs[i][j].second += cs[i][j-1].second;
}
}
unordered_map<int, long long> DP[N][2];//0-> upwards, 1->downwards or plains
for(int i=1; i<N; i++) {
// State = 0: upwards (H[i-1] < H[i])
{
// H[i-1] > 0
long long ptr = -1, qq = -5e14;
for(int x: possible_heights[i]) {
while(ptr + 1 < possible_heights[i-1].size() && possible_heights[i-1][ptr + 1] < x) {
ptr++;
int hh = possible_heights[i-1][ptr];
if(hh > 0 || i == 1) qq = max(qq, DP[i-1][0][hh] - columnSum(i-1, hh-1)); // If i = 1, hh = 0 is ok
}
DP[i][0][x] = max(DP[i][0][x], qq + columnSum(i-1, x-1));
}
// H[i-1] = 0
if(i > 1) {
// y 0 x, y < x
ptr = -1, qq = -5e14;
for(int x: possible_heights[i]) {
while(ptr + 1 < possible_heights[i-2].size() && possible_heights[i-2][ptr + 1] < x) {
ptr++;
int hh = possible_heights[i-2][ptr];
long long vv = max(DP[i-2][0][hh], DP[i-2][1][hh]);
qq = max(qq, vv);
}
DP[i][0][x] = max(DP[i][0][x], qq + columnSum(i-1, x-1));
}
// y 0 x, y >= x
ptr = possible_heights[i-2].size(), qq = -5e14;
reverse(possible_heights[i].begin(), possible_heights[i].end());
for(int x: possible_heights[i]) {
while(ptr - 1 >= 0 && possible_heights[i-2][ptr - 1] >= x) {
ptr--;
int hh = possible_heights[i-2][ptr];
long long vv = max(DP[i-2][0][hh], DP[i-2][1][hh]);
qq = max(qq, vv + columnSum(i-1, hh-1));
}
DP[i][0][x] = max(DP[i][0][x], qq);
}
reverse(possible_heights[i].begin(), possible_heights[i].end());
}
}
// State = 1: downwards (H[i-1] >= H[i])
{
reverse(possible_heights[i].begin(), possible_heights[i].end());
long long ptr = possible_heights[i-1].size(), qq = -5e14;
for(int x: possible_heights[i]) {
while(ptr - 1 >= 0 && possible_heights[i-1][ptr - 1] >= x) {
ptr--;
int hh = possible_heights[i-1][ptr];
qq = max(qq, max(DP[i-1][0][hh], DP[i-1][1][hh]) + columnSum(i, hh-1));
}
DP[i][1][x] = max(DP[i][1][x], qq - columnSum(i, x-1));
}
reverse(possible_heights[i].begin(), possible_heights[i].end());
}
for(int x: possible_heights[i]) {
DP[i][0][x] = max(DP[i][0][x], 0ll);
DP[i][1][x] = max(DP[i][1][x], 0ll);
}
}
long long ans = 0;
for(int x: possible_heights[N-1]) ans = max(ans, max(DP[N-1][0][x], DP[N-1][1][x]));
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:48:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int j=1; j<cs[i].size(); j++) {
| ~^~~~~~~~~~~~~
fish.cpp:63:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | while(ptr + 1 < possible_heights[i-1].size() && possible_heights[i-1][ptr + 1] < x) {
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fish.cpp:75:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while(ptr + 1 < possible_heights[i-2].size() && possible_heights[i-2][ptr + 1] < x) {
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
59932 KB |
Output is correct |
2 |
Correct |
166 ms |
68460 KB |
Output is correct |
3 |
Correct |
94 ms |
53496 KB |
Output is correct |
4 |
Correct |
96 ms |
53452 KB |
Output is correct |
5 |
Correct |
379 ms |
100504 KB |
Output is correct |
6 |
Correct |
456 ms |
94680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
214 ms |
71408 KB |
Output is correct |
3 |
Correct |
289 ms |
83532 KB |
Output is correct |
4 |
Correct |
140 ms |
59836 KB |
Output is correct |
5 |
Correct |
166 ms |
68460 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4984 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
4996 KB |
Output is correct |
10 |
Correct |
97 ms |
53392 KB |
Output is correct |
11 |
Correct |
111 ms |
53512 KB |
Output is correct |
12 |
Correct |
147 ms |
59928 KB |
Output is correct |
13 |
Correct |
185 ms |
68504 KB |
Output is correct |
14 |
Correct |
155 ms |
59956 KB |
Output is correct |
15 |
Correct |
173 ms |
67424 KB |
Output is correct |
16 |
Correct |
146 ms |
59900 KB |
Output is correct |
17 |
Correct |
178 ms |
67280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
53456 KB |
Output is correct |
2 |
Correct |
96 ms |
53420 KB |
Output is correct |
3 |
Correct |
125 ms |
52488 KB |
Output is correct |
4 |
Correct |
120 ms |
55996 KB |
Output is correct |
5 |
Correct |
178 ms |
60520 KB |
Output is correct |
6 |
Correct |
172 ms |
60072 KB |
Output is correct |
7 |
Correct |
191 ms |
60548 KB |
Output is correct |
8 |
Correct |
176 ms |
60552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5000 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
4996 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5332 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5000 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5000 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
4996 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5332 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5000 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
5 ms |
5332 KB |
Output is correct |
17 |
Correct |
62 ms |
11524 KB |
Output is correct |
18 |
Correct |
58 ms |
12712 KB |
Output is correct |
19 |
Correct |
48 ms |
12344 KB |
Output is correct |
20 |
Correct |
48 ms |
12420 KB |
Output is correct |
21 |
Correct |
43 ms |
12340 KB |
Output is correct |
22 |
Correct |
99 ms |
19788 KB |
Output is correct |
23 |
Correct |
14 ms |
6484 KB |
Output is correct |
24 |
Correct |
45 ms |
9424 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
14 ms |
6356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5000 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
4996 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5332 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5000 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
5 ms |
5332 KB |
Output is correct |
17 |
Correct |
62 ms |
11524 KB |
Output is correct |
18 |
Correct |
58 ms |
12712 KB |
Output is correct |
19 |
Correct |
48 ms |
12344 KB |
Output is correct |
20 |
Correct |
48 ms |
12420 KB |
Output is correct |
21 |
Correct |
43 ms |
12340 KB |
Output is correct |
22 |
Correct |
99 ms |
19788 KB |
Output is correct |
23 |
Correct |
14 ms |
6484 KB |
Output is correct |
24 |
Correct |
45 ms |
9424 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
14 ms |
6356 KB |
Output is correct |
27 |
Correct |
8 ms |
6740 KB |
Output is correct |
28 |
Correct |
228 ms |
40416 KB |
Output is correct |
29 |
Correct |
320 ms |
55484 KB |
Output is correct |
30 |
Correct |
334 ms |
50808 KB |
Output is correct |
31 |
Correct |
348 ms |
50744 KB |
Output is correct |
32 |
Correct |
336 ms |
55100 KB |
Output is correct |
33 |
Correct |
386 ms |
50864 KB |
Output is correct |
34 |
Correct |
380 ms |
50780 KB |
Output is correct |
35 |
Correct |
136 ms |
23772 KB |
Output is correct |
36 |
Correct |
421 ms |
54640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
53456 KB |
Output is correct |
2 |
Correct |
96 ms |
53420 KB |
Output is correct |
3 |
Correct |
125 ms |
52488 KB |
Output is correct |
4 |
Correct |
120 ms |
55996 KB |
Output is correct |
5 |
Correct |
178 ms |
60520 KB |
Output is correct |
6 |
Correct |
172 ms |
60072 KB |
Output is correct |
7 |
Correct |
191 ms |
60548 KB |
Output is correct |
8 |
Correct |
176 ms |
60552 KB |
Output is correct |
9 |
Correct |
240 ms |
66608 KB |
Output is correct |
10 |
Correct |
158 ms |
38816 KB |
Output is correct |
11 |
Correct |
315 ms |
72728 KB |
Output is correct |
12 |
Correct |
5 ms |
4996 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4928 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5000 KB |
Output is correct |
18 |
Correct |
105 ms |
53508 KB |
Output is correct |
19 |
Correct |
96 ms |
53416 KB |
Output is correct |
20 |
Correct |
120 ms |
53392 KB |
Output is correct |
21 |
Correct |
125 ms |
53400 KB |
Output is correct |
22 |
Correct |
230 ms |
66720 KB |
Output is correct |
23 |
Correct |
352 ms |
78532 KB |
Output is correct |
24 |
Correct |
357 ms |
79464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
59932 KB |
Output is correct |
2 |
Correct |
166 ms |
68460 KB |
Output is correct |
3 |
Correct |
94 ms |
53496 KB |
Output is correct |
4 |
Correct |
96 ms |
53452 KB |
Output is correct |
5 |
Correct |
379 ms |
100504 KB |
Output is correct |
6 |
Correct |
456 ms |
94680 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
214 ms |
71408 KB |
Output is correct |
9 |
Correct |
289 ms |
83532 KB |
Output is correct |
10 |
Correct |
140 ms |
59836 KB |
Output is correct |
11 |
Correct |
166 ms |
68460 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4984 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
4996 KB |
Output is correct |
16 |
Correct |
97 ms |
53392 KB |
Output is correct |
17 |
Correct |
111 ms |
53512 KB |
Output is correct |
18 |
Correct |
147 ms |
59928 KB |
Output is correct |
19 |
Correct |
185 ms |
68504 KB |
Output is correct |
20 |
Correct |
155 ms |
59956 KB |
Output is correct |
21 |
Correct |
173 ms |
67424 KB |
Output is correct |
22 |
Correct |
146 ms |
59900 KB |
Output is correct |
23 |
Correct |
178 ms |
67280 KB |
Output is correct |
24 |
Correct |
99 ms |
53456 KB |
Output is correct |
25 |
Correct |
96 ms |
53420 KB |
Output is correct |
26 |
Correct |
125 ms |
52488 KB |
Output is correct |
27 |
Correct |
120 ms |
55996 KB |
Output is correct |
28 |
Correct |
178 ms |
60520 KB |
Output is correct |
29 |
Correct |
172 ms |
60072 KB |
Output is correct |
30 |
Correct |
191 ms |
60548 KB |
Output is correct |
31 |
Correct |
176 ms |
60552 KB |
Output is correct |
32 |
Correct |
3 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
3 ms |
5000 KB |
Output is correct |
35 |
Correct |
3 ms |
4948 KB |
Output is correct |
36 |
Correct |
3 ms |
4996 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
3 ms |
5000 KB |
Output is correct |
39 |
Correct |
3 ms |
4996 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
5 ms |
5332 KB |
Output is correct |
42 |
Correct |
4 ms |
5204 KB |
Output is correct |
43 |
Correct |
4 ms |
5204 KB |
Output is correct |
44 |
Correct |
3 ms |
5000 KB |
Output is correct |
45 |
Correct |
4 ms |
5204 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
5 ms |
5332 KB |
Output is correct |
48 |
Correct |
62 ms |
11524 KB |
Output is correct |
49 |
Correct |
58 ms |
12712 KB |
Output is correct |
50 |
Correct |
48 ms |
12344 KB |
Output is correct |
51 |
Correct |
48 ms |
12420 KB |
Output is correct |
52 |
Correct |
43 ms |
12340 KB |
Output is correct |
53 |
Correct |
99 ms |
19788 KB |
Output is correct |
54 |
Correct |
14 ms |
6484 KB |
Output is correct |
55 |
Correct |
45 ms |
9424 KB |
Output is correct |
56 |
Correct |
4 ms |
5204 KB |
Output is correct |
57 |
Correct |
14 ms |
6356 KB |
Output is correct |
58 |
Correct |
8 ms |
6740 KB |
Output is correct |
59 |
Correct |
228 ms |
40416 KB |
Output is correct |
60 |
Correct |
320 ms |
55484 KB |
Output is correct |
61 |
Correct |
334 ms |
50808 KB |
Output is correct |
62 |
Correct |
348 ms |
50744 KB |
Output is correct |
63 |
Correct |
336 ms |
55100 KB |
Output is correct |
64 |
Correct |
386 ms |
50864 KB |
Output is correct |
65 |
Correct |
380 ms |
50780 KB |
Output is correct |
66 |
Correct |
136 ms |
23772 KB |
Output is correct |
67 |
Correct |
421 ms |
54640 KB |
Output is correct |
68 |
Correct |
240 ms |
66608 KB |
Output is correct |
69 |
Correct |
158 ms |
38816 KB |
Output is correct |
70 |
Correct |
315 ms |
72728 KB |
Output is correct |
71 |
Correct |
5 ms |
4996 KB |
Output is correct |
72 |
Correct |
4 ms |
4948 KB |
Output is correct |
73 |
Correct |
4 ms |
5004 KB |
Output is correct |
74 |
Correct |
3 ms |
4928 KB |
Output is correct |
75 |
Correct |
3 ms |
4948 KB |
Output is correct |
76 |
Correct |
4 ms |
5000 KB |
Output is correct |
77 |
Correct |
105 ms |
53508 KB |
Output is correct |
78 |
Correct |
96 ms |
53416 KB |
Output is correct |
79 |
Correct |
120 ms |
53392 KB |
Output is correct |
80 |
Correct |
125 ms |
53400 KB |
Output is correct |
81 |
Correct |
230 ms |
66720 KB |
Output is correct |
82 |
Correct |
352 ms |
78532 KB |
Output is correct |
83 |
Correct |
357 ms |
79464 KB |
Output is correct |
84 |
Correct |
542 ms |
92444 KB |
Output is correct |
85 |
Correct |
514 ms |
95920 KB |
Output is correct |
86 |
Correct |
487 ms |
88868 KB |
Output is correct |
87 |
Correct |
564 ms |
94172 KB |
Output is correct |
88 |
Correct |
3 ms |
4948 KB |
Output is correct |
89 |
Correct |
509 ms |
94136 KB |
Output is correct |
90 |
Correct |
492 ms |
93300 KB |
Output is correct |
91 |
Correct |
501 ms |
94508 KB |
Output is correct |