#include <bits/stdc++.h>
#define ll long long
#include "fish.h"
//#define all(lpv) lpv.begin(), lpv.end()
using namespace std;
const int N = 3e5 + 5;
const ll oo = -1e16;
int n, m;
void vin(ll &x,ll y){
x = max(x, y);
}
vector<ll> a[N], b[N], f[3][N], g[3][N];
long long 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 <= n + 1; i ++){
b[i].push_back(0);
b[i].push_back(n + 1);
}
for(int i = 1; i <= m; i ++){
int x = X[i - 1] + 1, y = Y[i - 1] + 1, c = W[i - 1];
b[x].push_back(y);
b[x - 1].push_back(y);
b[x + 1].push_back(y);
}
for(int i = 0; i <= n + 1; i ++){
sort(b[i].begin(), b[i].end());
b[i].resize(unique(b[i].begin(), b[i].end()) - b[i].begin());
int cnt = (int)(b[i].size());
a[i].resize(cnt + 1);
for(int k = 0; k <= 1; k ++){
f[k][i].resize(cnt + 1);
g[k][i].resize(cnt + 1);
}
for(int k = 0; k <= 1; k ++) for(int j = 0; j < a[i].size(); j ++){
a[i][j] = 0;
f[k][i][j] = oo;
g[k][i][j] = oo;
}
}
for(int i = 1; i <= m; i ++){
int x = X[i - 1] + 1, y = Y[i - 1] + 1, c = W[i - 1];
int u = lower_bound(b[x].begin(), b[x].end(), y) - b[x].begin();
a[x][u] += c;
}
for(int i = 0; i <= n; i ++){
for(int j = 1; j < b[i].size(); j ++) a[i][j] += a[i][j - 1];
}
f[0][0][0] = f[1][0][0] = 0;
for(int i = 0; i < b[0].size(); i ++) g[1][0][i] = 0;
ll ans = 0;
for(int i = 1; i <= n; i ++){
ll ret = oo;
for(int j = 0; j < b[i - 1].size(); j ++) ret = max({ret, f[0][i - 1][j], f[1][i - 1][j]});
f[0][i][0] = f[1][i][0] = ret;
for(int j = 0; j < b[i].size(); j ++){
int val = b[i][j];
vin(f[1][i][j], 0);
vin(f[0][i][j], 0);
int pos = upper_bound(b[i - 1].begin(), b[i - 1].end(), val) - b[i - 1].begin() - 1;
int tmp = lower_bound(b[i - 1].begin(), b[i - 1].end(), val) - b[i - 1].begin() - 1;
if(tmp >= 0 && pos >= 0 && j > 0) vin(f[1][i][j], a[i - 1][pos] + g[1][i - 1][tmp]);
tmp = upper_bound(b[i - 1].begin(), b[i - 1].end(), val) - b[i - 1].begin();
vin(f[0][i][j], g[0][i - 1][tmp] - a[i][j]);
vin(ans, max(f[1][i][j], f[0][i][j]));
}
for(int j = 0; j < b[i].size(); j ++){
g[1][i][j] = f[1][i][j] - a[i][j];
if(j > 0) vin(g[1][i][j], g[1][i][j - 1]);
}
for(int j = b[i].size() - 1; j >= 0; j --){
int pos = upper_bound(b[i + 1].begin(), b[i + 1].end(), b[i][j]) - b[i + 1].begin() - 1;
g[0][i][j] = max(f[0][i][j], f[1][i][j]) + a[i + 1][pos];
if(j < b[i].size() - 1) vin(g[0][i][j], g[0][i][j + 1]);
}
}
return ans;
}
//#define LOCAL
#ifdef LOCAL
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
int _N, M;
assert(2 == scanf("%d %d", &_N, &M));
std::vector<int> X(M), Y(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
}
long long result = max_weights(_N, M, X, Y, W);
printf("%lld\n", result);
return 0;
}
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
#endif // LOCAL
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:27:45: warning: unused variable 'c' [-Wunused-variable]
27 | int x = X[i - 1] + 1, y = Y[i - 1] + 1, c = W[i - 1];
| ^
fish.cpp:41:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int k = 0; k <= 1; k ++) for(int j = 0; j < a[i].size(); j ++){
| ~~^~~~~~~~~~~~~
fish.cpp:54:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int j = 1; j < b[i].size(); j ++) a[i][j] += a[i][j - 1];
| ~~^~~~~~~~~~~~~
fish.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 0; i < b[0].size(); i ++) g[1][0][i] = 0;
| ~~^~~~~~~~~~~~~
fish.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int j = 0; j < b[i - 1].size(); j ++) ret = max({ret, f[0][i - 1][j], f[1][i - 1][j]});
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int j = 0; j < b[i].size(); j ++){
| ~~^~~~~~~~~~~~~
fish.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int j = 0; j < b[i].size(); j ++){
| ~~^~~~~~~~~~~~~
fish.cpp:85:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | if(j < b[i].size() - 1) vin(g[0][i][j], g[0][i][j + 1]);
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
86824 KB |
Output is correct |
2 |
Correct |
145 ms |
93680 KB |
Output is correct |
3 |
Correct |
49 ms |
75412 KB |
Output is correct |
4 |
Correct |
50 ms |
75336 KB |
Output is correct |
5 |
Correct |
382 ms |
131344 KB |
Output is correct |
6 |
Correct |
342 ms |
136712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
56656 KB |
Output is correct |
2 |
Correct |
191 ms |
98860 KB |
Output is correct |
3 |
Correct |
223 ms |
104860 KB |
Output is correct |
4 |
Correct |
127 ms |
88360 KB |
Output is correct |
5 |
Correct |
166 ms |
93764 KB |
Output is correct |
6 |
Correct |
10 ms |
56656 KB |
Output is correct |
7 |
Correct |
11 ms |
56656 KB |
Output is correct |
8 |
Correct |
11 ms |
56656 KB |
Output is correct |
9 |
Correct |
11 ms |
56808 KB |
Output is correct |
10 |
Correct |
49 ms |
75336 KB |
Output is correct |
11 |
Correct |
49 ms |
75612 KB |
Output is correct |
12 |
Correct |
124 ms |
88360 KB |
Output is correct |
13 |
Correct |
155 ms |
93736 KB |
Output is correct |
14 |
Correct |
131 ms |
88344 KB |
Output is correct |
15 |
Correct |
131 ms |
88696 KB |
Output is correct |
16 |
Correct |
124 ms |
88528 KB |
Output is correct |
17 |
Correct |
157 ms |
91868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
75344 KB |
Output is correct |
2 |
Correct |
49 ms |
75480 KB |
Output is correct |
3 |
Correct |
84 ms |
83016 KB |
Output is correct |
4 |
Correct |
82 ms |
82272 KB |
Output is correct |
5 |
Correct |
139 ms |
92488 KB |
Output is correct |
6 |
Correct |
137 ms |
91732 KB |
Output is correct |
7 |
Correct |
125 ms |
92488 KB |
Output is correct |
8 |
Correct |
116 ms |
92744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
56656 KB |
Output is correct |
2 |
Correct |
12 ms |
56656 KB |
Output is correct |
3 |
Correct |
10 ms |
56656 KB |
Output is correct |
4 |
Correct |
10 ms |
56656 KB |
Output is correct |
5 |
Correct |
12 ms |
56656 KB |
Output is correct |
6 |
Correct |
10 ms |
56656 KB |
Output is correct |
7 |
Correct |
10 ms |
56804 KB |
Output is correct |
8 |
Correct |
10 ms |
56824 KB |
Output is correct |
9 |
Correct |
10 ms |
56656 KB |
Output is correct |
10 |
Correct |
11 ms |
56912 KB |
Output is correct |
11 |
Correct |
11 ms |
56912 KB |
Output is correct |
12 |
Correct |
11 ms |
56928 KB |
Output is correct |
13 |
Correct |
10 ms |
56656 KB |
Output is correct |
14 |
Correct |
11 ms |
56912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
56656 KB |
Output is correct |
2 |
Correct |
12 ms |
56656 KB |
Output is correct |
3 |
Correct |
10 ms |
56656 KB |
Output is correct |
4 |
Correct |
10 ms |
56656 KB |
Output is correct |
5 |
Correct |
12 ms |
56656 KB |
Output is correct |
6 |
Correct |
10 ms |
56656 KB |
Output is correct |
7 |
Correct |
10 ms |
56804 KB |
Output is correct |
8 |
Correct |
10 ms |
56824 KB |
Output is correct |
9 |
Correct |
10 ms |
56656 KB |
Output is correct |
10 |
Correct |
11 ms |
56912 KB |
Output is correct |
11 |
Correct |
11 ms |
56912 KB |
Output is correct |
12 |
Correct |
11 ms |
56928 KB |
Output is correct |
13 |
Correct |
10 ms |
56656 KB |
Output is correct |
14 |
Correct |
11 ms |
56912 KB |
Output is correct |
15 |
Correct |
11 ms |
56912 KB |
Output is correct |
16 |
Correct |
13 ms |
56912 KB |
Output is correct |
17 |
Correct |
32 ms |
62544 KB |
Output is correct |
18 |
Correct |
31 ms |
62196 KB |
Output is correct |
19 |
Correct |
31 ms |
62032 KB |
Output is correct |
20 |
Correct |
30 ms |
61520 KB |
Output is correct |
21 |
Correct |
30 ms |
61512 KB |
Output is correct |
22 |
Correct |
50 ms |
66528 KB |
Output is correct |
23 |
Correct |
15 ms |
58448 KB |
Output is correct |
24 |
Correct |
30 ms |
61772 KB |
Output is correct |
25 |
Correct |
11 ms |
56912 KB |
Output is correct |
26 |
Correct |
15 ms |
58192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
56656 KB |
Output is correct |
2 |
Correct |
12 ms |
56656 KB |
Output is correct |
3 |
Correct |
10 ms |
56656 KB |
Output is correct |
4 |
Correct |
10 ms |
56656 KB |
Output is correct |
5 |
Correct |
12 ms |
56656 KB |
Output is correct |
6 |
Correct |
10 ms |
56656 KB |
Output is correct |
7 |
Correct |
10 ms |
56804 KB |
Output is correct |
8 |
Correct |
10 ms |
56824 KB |
Output is correct |
9 |
Correct |
10 ms |
56656 KB |
Output is correct |
10 |
Correct |
11 ms |
56912 KB |
Output is correct |
11 |
Correct |
11 ms |
56912 KB |
Output is correct |
12 |
Correct |
11 ms |
56928 KB |
Output is correct |
13 |
Correct |
10 ms |
56656 KB |
Output is correct |
14 |
Correct |
11 ms |
56912 KB |
Output is correct |
15 |
Correct |
11 ms |
56912 KB |
Output is correct |
16 |
Correct |
13 ms |
56912 KB |
Output is correct |
17 |
Correct |
32 ms |
62544 KB |
Output is correct |
18 |
Correct |
31 ms |
62196 KB |
Output is correct |
19 |
Correct |
31 ms |
62032 KB |
Output is correct |
20 |
Correct |
30 ms |
61520 KB |
Output is correct |
21 |
Correct |
30 ms |
61512 KB |
Output is correct |
22 |
Correct |
50 ms |
66528 KB |
Output is correct |
23 |
Correct |
15 ms |
58448 KB |
Output is correct |
24 |
Correct |
30 ms |
61772 KB |
Output is correct |
25 |
Correct |
11 ms |
56912 KB |
Output is correct |
26 |
Correct |
15 ms |
58192 KB |
Output is correct |
27 |
Correct |
13 ms |
57936 KB |
Output is correct |
28 |
Correct |
133 ms |
82480 KB |
Output is correct |
29 |
Correct |
245 ms |
97608 KB |
Output is correct |
30 |
Correct |
238 ms |
115528 KB |
Output is correct |
31 |
Correct |
225 ms |
115016 KB |
Output is correct |
32 |
Correct |
166 ms |
89992 KB |
Output is correct |
33 |
Correct |
256 ms |
116360 KB |
Output is correct |
34 |
Correct |
248 ms |
116296 KB |
Output is correct |
35 |
Correct |
85 ms |
75936 KB |
Output is correct |
36 |
Correct |
283 ms |
108184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
75344 KB |
Output is correct |
2 |
Correct |
49 ms |
75480 KB |
Output is correct |
3 |
Correct |
84 ms |
83016 KB |
Output is correct |
4 |
Correct |
82 ms |
82272 KB |
Output is correct |
5 |
Correct |
139 ms |
92488 KB |
Output is correct |
6 |
Correct |
137 ms |
91732 KB |
Output is correct |
7 |
Correct |
125 ms |
92488 KB |
Output is correct |
8 |
Correct |
116 ms |
92744 KB |
Output is correct |
9 |
Correct |
144 ms |
100320 KB |
Output is correct |
10 |
Correct |
79 ms |
76768 KB |
Output is correct |
11 |
Correct |
161 ms |
96584 KB |
Output is correct |
12 |
Correct |
11 ms |
56824 KB |
Output is correct |
13 |
Correct |
11 ms |
56656 KB |
Output is correct |
14 |
Correct |
11 ms |
56656 KB |
Output is correct |
15 |
Correct |
11 ms |
56656 KB |
Output is correct |
16 |
Correct |
11 ms |
56656 KB |
Output is correct |
17 |
Correct |
14 ms |
56804 KB |
Output is correct |
18 |
Correct |
51 ms |
75600 KB |
Output is correct |
19 |
Correct |
50 ms |
75564 KB |
Output is correct |
20 |
Correct |
49 ms |
75560 KB |
Output is correct |
21 |
Correct |
51 ms |
75336 KB |
Output is correct |
22 |
Correct |
161 ms |
97096 KB |
Output is correct |
23 |
Correct |
190 ms |
105808 KB |
Output is correct |
24 |
Correct |
180 ms |
107264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
86824 KB |
Output is correct |
2 |
Correct |
145 ms |
93680 KB |
Output is correct |
3 |
Correct |
49 ms |
75412 KB |
Output is correct |
4 |
Correct |
50 ms |
75336 KB |
Output is correct |
5 |
Correct |
382 ms |
131344 KB |
Output is correct |
6 |
Correct |
342 ms |
136712 KB |
Output is correct |
7 |
Correct |
11 ms |
56656 KB |
Output is correct |
8 |
Correct |
191 ms |
98860 KB |
Output is correct |
9 |
Correct |
223 ms |
104860 KB |
Output is correct |
10 |
Correct |
127 ms |
88360 KB |
Output is correct |
11 |
Correct |
166 ms |
93764 KB |
Output is correct |
12 |
Correct |
10 ms |
56656 KB |
Output is correct |
13 |
Correct |
11 ms |
56656 KB |
Output is correct |
14 |
Correct |
11 ms |
56656 KB |
Output is correct |
15 |
Correct |
11 ms |
56808 KB |
Output is correct |
16 |
Correct |
49 ms |
75336 KB |
Output is correct |
17 |
Correct |
49 ms |
75612 KB |
Output is correct |
18 |
Correct |
124 ms |
88360 KB |
Output is correct |
19 |
Correct |
155 ms |
93736 KB |
Output is correct |
20 |
Correct |
131 ms |
88344 KB |
Output is correct |
21 |
Correct |
131 ms |
88696 KB |
Output is correct |
22 |
Correct |
124 ms |
88528 KB |
Output is correct |
23 |
Correct |
157 ms |
91868 KB |
Output is correct |
24 |
Correct |
49 ms |
75344 KB |
Output is correct |
25 |
Correct |
49 ms |
75480 KB |
Output is correct |
26 |
Correct |
84 ms |
83016 KB |
Output is correct |
27 |
Correct |
82 ms |
82272 KB |
Output is correct |
28 |
Correct |
139 ms |
92488 KB |
Output is correct |
29 |
Correct |
137 ms |
91732 KB |
Output is correct |
30 |
Correct |
125 ms |
92488 KB |
Output is correct |
31 |
Correct |
116 ms |
92744 KB |
Output is correct |
32 |
Correct |
10 ms |
56656 KB |
Output is correct |
33 |
Correct |
12 ms |
56656 KB |
Output is correct |
34 |
Correct |
10 ms |
56656 KB |
Output is correct |
35 |
Correct |
10 ms |
56656 KB |
Output is correct |
36 |
Correct |
12 ms |
56656 KB |
Output is correct |
37 |
Correct |
10 ms |
56656 KB |
Output is correct |
38 |
Correct |
10 ms |
56804 KB |
Output is correct |
39 |
Correct |
10 ms |
56824 KB |
Output is correct |
40 |
Correct |
10 ms |
56656 KB |
Output is correct |
41 |
Correct |
11 ms |
56912 KB |
Output is correct |
42 |
Correct |
11 ms |
56912 KB |
Output is correct |
43 |
Correct |
11 ms |
56928 KB |
Output is correct |
44 |
Correct |
10 ms |
56656 KB |
Output is correct |
45 |
Correct |
11 ms |
56912 KB |
Output is correct |
46 |
Correct |
11 ms |
56912 KB |
Output is correct |
47 |
Correct |
13 ms |
56912 KB |
Output is correct |
48 |
Correct |
32 ms |
62544 KB |
Output is correct |
49 |
Correct |
31 ms |
62196 KB |
Output is correct |
50 |
Correct |
31 ms |
62032 KB |
Output is correct |
51 |
Correct |
30 ms |
61520 KB |
Output is correct |
52 |
Correct |
30 ms |
61512 KB |
Output is correct |
53 |
Correct |
50 ms |
66528 KB |
Output is correct |
54 |
Correct |
15 ms |
58448 KB |
Output is correct |
55 |
Correct |
30 ms |
61772 KB |
Output is correct |
56 |
Correct |
11 ms |
56912 KB |
Output is correct |
57 |
Correct |
15 ms |
58192 KB |
Output is correct |
58 |
Correct |
13 ms |
57936 KB |
Output is correct |
59 |
Correct |
133 ms |
82480 KB |
Output is correct |
60 |
Correct |
245 ms |
97608 KB |
Output is correct |
61 |
Correct |
238 ms |
115528 KB |
Output is correct |
62 |
Correct |
225 ms |
115016 KB |
Output is correct |
63 |
Correct |
166 ms |
89992 KB |
Output is correct |
64 |
Correct |
256 ms |
116360 KB |
Output is correct |
65 |
Correct |
248 ms |
116296 KB |
Output is correct |
66 |
Correct |
85 ms |
75936 KB |
Output is correct |
67 |
Correct |
283 ms |
108184 KB |
Output is correct |
68 |
Correct |
144 ms |
100320 KB |
Output is correct |
69 |
Correct |
79 ms |
76768 KB |
Output is correct |
70 |
Correct |
161 ms |
96584 KB |
Output is correct |
71 |
Correct |
11 ms |
56824 KB |
Output is correct |
72 |
Correct |
11 ms |
56656 KB |
Output is correct |
73 |
Correct |
11 ms |
56656 KB |
Output is correct |
74 |
Correct |
11 ms |
56656 KB |
Output is correct |
75 |
Correct |
11 ms |
56656 KB |
Output is correct |
76 |
Correct |
14 ms |
56804 KB |
Output is correct |
77 |
Correct |
51 ms |
75600 KB |
Output is correct |
78 |
Correct |
50 ms |
75564 KB |
Output is correct |
79 |
Correct |
49 ms |
75560 KB |
Output is correct |
80 |
Correct |
51 ms |
75336 KB |
Output is correct |
81 |
Correct |
161 ms |
97096 KB |
Output is correct |
82 |
Correct |
190 ms |
105808 KB |
Output is correct |
83 |
Correct |
180 ms |
107264 KB |
Output is correct |
84 |
Correct |
371 ms |
124900 KB |
Output is correct |
85 |
Correct |
360 ms |
125940 KB |
Output is correct |
86 |
Correct |
291 ms |
133748 KB |
Output is correct |
87 |
Correct |
305 ms |
136520 KB |
Output is correct |
88 |
Correct |
11 ms |
56656 KB |
Output is correct |
89 |
Correct |
297 ms |
136528 KB |
Output is correct |
90 |
Correct |
229 ms |
125196 KB |
Output is correct |
91 |
Correct |
193 ms |
113548 KB |
Output is correct |