#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
const int MAX = 1e5 + 5, SINF = 2e9;
const long long INF = 1e18;
bool markLow[MAX];
vector<ii> g[MAX];
vector<long long> S[MAX];
vector<vector<long long>> f[MAX];
struct FenwickTreeLow {
int n;
vector<int> buffer;
vector<long long> tree;
FenwickTreeLow(int _n) {
n = _n;
tree.resize(n + 5, -INF);
}
void update(int idx, long long val) {
idx++;
for (int i = idx; i <= n; i += i & -i) {
buffer.push_back(i);
tree[i] = max(tree[i], val);
}
}
long long get(int idx) {
idx++;
long long rs = 0;
for (int i = idx; i > 0; i -= i & -i)
rs = max(rs, tree[i]);
return rs;
}
void reset() {
for (int id: buffer)
tree[id] = -INF;
buffer.clear();
}
};
struct FenwickTreeHigh {
int n;
vector<int> buffer;
vector<long long> tree;
FenwickTreeHigh(int _n) {
n = _n;
tree.resize(n + 5, -INF);
}
void update(int idx, long long val) {
idx++;
for (int i = idx; i > 0; i -= i & -i) {
buffer.push_back(i);
tree[i] = max(tree[i], val);
}
}
long long get(int idx) {
idx++;
long long rs = 0;
for (int i = idx; i <= n; i += i & -i)
rs = max(rs, tree[i]);
return rs;
}
void reset() {
for (int id: buffer)
tree[id] = -INF;
buffer.clear();
}
};
long long calcCostLeft(int col, int L) {
int l = lower_bound(g[col].begin(), g[col].end(), ii(L, 0)) - g[col].begin();
return l ? S[col][l - 1] : 0;
}
long long calcCostRight(int col, int R) {
int r = upper_bound(g[col].begin(), g[col].end(), ii(R, 0)) - g[col].begin() - 1;
return r >= 0 ? S[col][r] : 0;
}
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
for (int i = 0; i < M; i++) {
g[X[i]].emplace_back(Y[i], W[i]);
if (!Y[i])
markLow[X[i]] = true;
}
for (int i = 0; i < N; i++) {
if (!markLow[i])
g[i].emplace_back(0, 0);
g[i].emplace_back(N, 0);
}
for (int i = 0; i < N; i++) {
sort(g[i].begin(), g[i].end());
S[i].resize(g[i].size());
S[i][0] = g[i][0].second;
for (int j = 1; j < g[i].size(); j++)
S[i][j] = S[i][j - 1] + g[i][j].second;
f[i].resize(g[i].size(), vector<long long>(2));
}
FenwickTreeLow lowTree(N + 5), lowJump(N + 5);
FenwickTreeHigh highTree(N + 5), highJump(N + 5);
long long rs = 0;
for (int i = 1; i < N; i++) {
if (i > 1) {
lowJump.reset(), highJump.reset();
for (int t = 0; t < g[i - 2].size(); t++) {
lowJump.update(t, f[i - 2][t][1]);
highJump.update(t, f[i - 2][t][1] + calcCostRight(i - 1, g[i - 2][t].first));
}
}
lowTree.reset(), highTree.reset();
for (int t = 0; t < g[i - 1].size(); t++) {
lowTree.update(t, f[i - 1][t][0] - calcCostLeft(i - 1, g[i - 1][t].first));
highTree.update(t, f[i - 1][t][1] + calcCostRight(i, g[i - 1][t].first));
}
for (int j = 0; j < g[i].size(); j++) {
if (i > 1) {
int low = upper_bound(g[i - 2].begin(), g[i - 2].end(), ii(g[i][j].first, SINF)) - g[i - 2].begin() - 1;
long long tmp = lowJump.get(low) + calcCostRight(i - 1, g[i][j].first);
f[i][j][0] = max(f[i][j][0], tmp);
f[i][j][1] = max(f[i][j][1], tmp);
int high = low + 1;
tmp = highJump.get(high);
f[i][j][0] = max(f[i][j][0], tmp);
f[i][j][1] = max(f[i][j][1], tmp);
} else {
long long tmp = calcCostRight(0, g[i][j].first);
f[i][j][0] = max(f[i][j][0], tmp);
f[i][j][1] = max(f[i][j][1], tmp);
}
int low = upper_bound(g[i - 1].begin(), g[i - 1].end(), ii(g[i][j].first, SINF)) - g[i - 1].begin() - 1;
long long tmp = lowTree.get(low) + calcCostRight(i - 1, g[i][j].first);
f[i][j][0] = max(f[i][j][0], tmp);
f[i][j][1] = max(f[i][j][1], tmp);
int high = lower_bound(g[i - 1].begin(), g[i - 1].end(), ii(g[i][j].first, 0)) - g[i - 1].begin();
f[i][j][1] = max(f[i][j][1], highTree.get(high) - calcCostLeft(i, g[i][j].first));
rs = max({rs, f[i][j][0], f[i][j][1]});
}
}
return rs;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:104:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int j = 1; j < g[i].size(); j++)
| ~~^~~~~~~~~~~~~
fish.cpp:114:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int t = 0; t < g[i - 2].size(); t++) {
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:120:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int t = 0; t < g[i - 1].size(); t++) {
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:124:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int j = 0; j < g[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
49708 KB |
Output is correct |
2 |
Correct |
215 ms |
55680 KB |
Output is correct |
3 |
Correct |
123 ms |
29220 KB |
Output is correct |
4 |
Correct |
130 ms |
29244 KB |
Output is correct |
5 |
Correct |
442 ms |
73464 KB |
Output is correct |
6 |
Correct |
455 ms |
58460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
301 ms |
57328 KB |
Output is correct |
3 |
Correct |
331 ms |
64972 KB |
Output is correct |
4 |
Correct |
184 ms |
49704 KB |
Output is correct |
5 |
Correct |
210 ms |
55580 KB |
Output is correct |
6 |
Correct |
6 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
129 ms |
29352 KB |
Output is correct |
11 |
Correct |
119 ms |
29272 KB |
Output is correct |
12 |
Correct |
210 ms |
49776 KB |
Output is correct |
13 |
Correct |
248 ms |
55688 KB |
Output is correct |
14 |
Correct |
202 ms |
42588 KB |
Output is correct |
15 |
Correct |
239 ms |
45932 KB |
Output is correct |
16 |
Correct |
210 ms |
42584 KB |
Output is correct |
17 |
Correct |
216 ms |
45864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
29276 KB |
Output is correct |
2 |
Correct |
125 ms |
29268 KB |
Output is correct |
3 |
Correct |
151 ms |
28476 KB |
Output is correct |
4 |
Correct |
142 ms |
30216 KB |
Output is correct |
5 |
Correct |
188 ms |
31724 KB |
Output is correct |
6 |
Correct |
167 ms |
31720 KB |
Output is correct |
7 |
Correct |
163 ms |
31728 KB |
Output is correct |
8 |
Correct |
161 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7304 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7636 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7508 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
7 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7304 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7636 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7508 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
7 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
7 ms |
7592 KB |
Output is correct |
17 |
Correct |
51 ms |
11776 KB |
Output is correct |
18 |
Correct |
57 ms |
11988 KB |
Output is correct |
19 |
Correct |
48 ms |
11860 KB |
Output is correct |
20 |
Correct |
56 ms |
11860 KB |
Output is correct |
21 |
Correct |
46 ms |
11896 KB |
Output is correct |
22 |
Correct |
96 ms |
16332 KB |
Output is correct |
23 |
Correct |
13 ms |
8276 KB |
Output is correct |
24 |
Correct |
39 ms |
10304 KB |
Output is correct |
25 |
Correct |
5 ms |
7472 KB |
Output is correct |
26 |
Correct |
13 ms |
8236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7304 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7636 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7508 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
7 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
7 ms |
7592 KB |
Output is correct |
17 |
Correct |
51 ms |
11776 KB |
Output is correct |
18 |
Correct |
57 ms |
11988 KB |
Output is correct |
19 |
Correct |
48 ms |
11860 KB |
Output is correct |
20 |
Correct |
56 ms |
11860 KB |
Output is correct |
21 |
Correct |
46 ms |
11896 KB |
Output is correct |
22 |
Correct |
96 ms |
16332 KB |
Output is correct |
23 |
Correct |
13 ms |
8276 KB |
Output is correct |
24 |
Correct |
39 ms |
10304 KB |
Output is correct |
25 |
Correct |
5 ms |
7472 KB |
Output is correct |
26 |
Correct |
13 ms |
8236 KB |
Output is correct |
27 |
Correct |
9 ms |
8148 KB |
Output is correct |
28 |
Correct |
255 ms |
29076 KB |
Output is correct |
29 |
Correct |
334 ms |
37652 KB |
Output is correct |
30 |
Correct |
324 ms |
36828 KB |
Output is correct |
31 |
Correct |
312 ms |
36828 KB |
Output is correct |
32 |
Correct |
331 ms |
37708 KB |
Output is correct |
33 |
Correct |
325 ms |
36864 KB |
Output is correct |
34 |
Correct |
333 ms |
36836 KB |
Output is correct |
35 |
Correct |
126 ms |
19216 KB |
Output is correct |
36 |
Correct |
348 ms |
37688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
29276 KB |
Output is correct |
2 |
Correct |
125 ms |
29268 KB |
Output is correct |
3 |
Correct |
151 ms |
28476 KB |
Output is correct |
4 |
Correct |
142 ms |
30216 KB |
Output is correct |
5 |
Correct |
188 ms |
31724 KB |
Output is correct |
6 |
Correct |
167 ms |
31720 KB |
Output is correct |
7 |
Correct |
163 ms |
31728 KB |
Output is correct |
8 |
Correct |
161 ms |
31724 KB |
Output is correct |
9 |
Correct |
224 ms |
38408 KB |
Output is correct |
10 |
Correct |
148 ms |
24016 KB |
Output is correct |
11 |
Correct |
296 ms |
40856 KB |
Output is correct |
12 |
Correct |
5 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
6 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
5 ms |
7252 KB |
Output is correct |
17 |
Correct |
6 ms |
7252 KB |
Output is correct |
18 |
Correct |
127 ms |
29276 KB |
Output is correct |
19 |
Correct |
124 ms |
29284 KB |
Output is correct |
20 |
Correct |
129 ms |
29272 KB |
Output is correct |
21 |
Correct |
125 ms |
29272 KB |
Output is correct |
22 |
Correct |
260 ms |
38468 KB |
Output is correct |
23 |
Correct |
306 ms |
48108 KB |
Output is correct |
24 |
Correct |
300 ms |
48612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
49708 KB |
Output is correct |
2 |
Correct |
215 ms |
55680 KB |
Output is correct |
3 |
Correct |
123 ms |
29220 KB |
Output is correct |
4 |
Correct |
130 ms |
29244 KB |
Output is correct |
5 |
Correct |
442 ms |
73464 KB |
Output is correct |
6 |
Correct |
455 ms |
58460 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
301 ms |
57328 KB |
Output is correct |
9 |
Correct |
331 ms |
64972 KB |
Output is correct |
10 |
Correct |
184 ms |
49704 KB |
Output is correct |
11 |
Correct |
210 ms |
55580 KB |
Output is correct |
12 |
Correct |
6 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
129 ms |
29352 KB |
Output is correct |
17 |
Correct |
119 ms |
29272 KB |
Output is correct |
18 |
Correct |
210 ms |
49776 KB |
Output is correct |
19 |
Correct |
248 ms |
55688 KB |
Output is correct |
20 |
Correct |
202 ms |
42588 KB |
Output is correct |
21 |
Correct |
239 ms |
45932 KB |
Output is correct |
22 |
Correct |
210 ms |
42584 KB |
Output is correct |
23 |
Correct |
216 ms |
45864 KB |
Output is correct |
24 |
Correct |
124 ms |
29276 KB |
Output is correct |
25 |
Correct |
125 ms |
29268 KB |
Output is correct |
26 |
Correct |
151 ms |
28476 KB |
Output is correct |
27 |
Correct |
142 ms |
30216 KB |
Output is correct |
28 |
Correct |
188 ms |
31724 KB |
Output is correct |
29 |
Correct |
167 ms |
31720 KB |
Output is correct |
30 |
Correct |
163 ms |
31728 KB |
Output is correct |
31 |
Correct |
161 ms |
31724 KB |
Output is correct |
32 |
Correct |
5 ms |
7252 KB |
Output is correct |
33 |
Correct |
5 ms |
7252 KB |
Output is correct |
34 |
Correct |
5 ms |
7252 KB |
Output is correct |
35 |
Correct |
4 ms |
7252 KB |
Output is correct |
36 |
Correct |
4 ms |
7252 KB |
Output is correct |
37 |
Correct |
4 ms |
7252 KB |
Output is correct |
38 |
Correct |
4 ms |
7252 KB |
Output is correct |
39 |
Correct |
6 ms |
7304 KB |
Output is correct |
40 |
Correct |
5 ms |
7380 KB |
Output is correct |
41 |
Correct |
5 ms |
7636 KB |
Output is correct |
42 |
Correct |
5 ms |
7380 KB |
Output is correct |
43 |
Correct |
5 ms |
7508 KB |
Output is correct |
44 |
Correct |
5 ms |
7380 KB |
Output is correct |
45 |
Correct |
7 ms |
7380 KB |
Output is correct |
46 |
Correct |
4 ms |
7380 KB |
Output is correct |
47 |
Correct |
7 ms |
7592 KB |
Output is correct |
48 |
Correct |
51 ms |
11776 KB |
Output is correct |
49 |
Correct |
57 ms |
11988 KB |
Output is correct |
50 |
Correct |
48 ms |
11860 KB |
Output is correct |
51 |
Correct |
56 ms |
11860 KB |
Output is correct |
52 |
Correct |
46 ms |
11896 KB |
Output is correct |
53 |
Correct |
96 ms |
16332 KB |
Output is correct |
54 |
Correct |
13 ms |
8276 KB |
Output is correct |
55 |
Correct |
39 ms |
10304 KB |
Output is correct |
56 |
Correct |
5 ms |
7472 KB |
Output is correct |
57 |
Correct |
13 ms |
8236 KB |
Output is correct |
58 |
Correct |
9 ms |
8148 KB |
Output is correct |
59 |
Correct |
255 ms |
29076 KB |
Output is correct |
60 |
Correct |
334 ms |
37652 KB |
Output is correct |
61 |
Correct |
324 ms |
36828 KB |
Output is correct |
62 |
Correct |
312 ms |
36828 KB |
Output is correct |
63 |
Correct |
331 ms |
37708 KB |
Output is correct |
64 |
Correct |
325 ms |
36864 KB |
Output is correct |
65 |
Correct |
333 ms |
36836 KB |
Output is correct |
66 |
Correct |
126 ms |
19216 KB |
Output is correct |
67 |
Correct |
348 ms |
37688 KB |
Output is correct |
68 |
Correct |
224 ms |
38408 KB |
Output is correct |
69 |
Correct |
148 ms |
24016 KB |
Output is correct |
70 |
Correct |
296 ms |
40856 KB |
Output is correct |
71 |
Correct |
5 ms |
7252 KB |
Output is correct |
72 |
Correct |
4 ms |
7252 KB |
Output is correct |
73 |
Correct |
6 ms |
7252 KB |
Output is correct |
74 |
Correct |
4 ms |
7252 KB |
Output is correct |
75 |
Correct |
5 ms |
7252 KB |
Output is correct |
76 |
Correct |
6 ms |
7252 KB |
Output is correct |
77 |
Correct |
127 ms |
29276 KB |
Output is correct |
78 |
Correct |
124 ms |
29284 KB |
Output is correct |
79 |
Correct |
129 ms |
29272 KB |
Output is correct |
80 |
Correct |
125 ms |
29272 KB |
Output is correct |
81 |
Correct |
260 ms |
38468 KB |
Output is correct |
82 |
Correct |
306 ms |
48108 KB |
Output is correct |
83 |
Correct |
300 ms |
48612 KB |
Output is correct |
84 |
Correct |
451 ms |
59632 KB |
Output is correct |
85 |
Correct |
464 ms |
61788 KB |
Output is correct |
86 |
Correct |
403 ms |
55620 KB |
Output is correct |
87 |
Correct |
424 ms |
57984 KB |
Output is correct |
88 |
Correct |
5 ms |
7252 KB |
Output is correct |
89 |
Correct |
424 ms |
57988 KB |
Output is correct |
90 |
Correct |
440 ms |
57180 KB |
Output is correct |
91 |
Correct |
421 ms |
56948 KB |
Output is correct |