#include "fish.h"
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define all(x) begin(x),end(x)
struct SegTree {
using T = ll; // use pair of value and index to get index from queries
T f(T a, T b) { return max(a, b); }
static constexpr T UNIT = 0; // neutral value for f
vector<T> s; ll n;
SegTree(ll len) : s(2 * len, UNIT), n(len) {}
void set(ll pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(ll lo, ll hi) { // query lo to hi (hi not included)
if (lo >= hi) return UNIT;
T ra = UNIT, rb = UNIT;
for (lo += n, hi += n; lo < hi; lo /= 2, hi /= 2) {
if (lo % 2) ra = f(ra, s[lo++]);
if (hi % 2) rb = f(s[--hi], rb);
}
return f(ra, rb);
}
};
vector<vector<pair<ll, ll>>> fishWeightSum;
ll weightSum(ll x, ll y) {
if (x < 0 || x >= (ll)fishWeightSum.size())
return 0;
ll idx = lower_bound(all(fishWeightSum[x]), make_pair(y, (ll)-1)) - fishWeightSum[x].begin();
if (fishWeightSum[x][idx].first > y)
idx--;
return fishWeightSum[x][idx].second;
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
if (M == 0) return 0;
vector<pair<ll, ll>> piers;
vector<tuple<ll, ll, ll>> fish;
for (ll i = 0; i < M; i++) {
fish.emplace_back(X[i], Y[i], W[i]);
if (X[i] != 0)
piers.emplace_back(X[i] - 1, Y[i]);
if (X[i] != N - 1)
piers.emplace_back(X[i] + 1, Y[i]);
}
sort(all(piers));
piers.erase(unique(all(piers)), piers.end());
sort(all(fish));
fishWeightSum.resize(N);
for (ll i = 0; i < N; i++) {
fishWeightSum[i].emplace_back(-1, 0);
}
for (auto [x, y, w] : fish) {
fishWeightSum[x].emplace_back(y, fishWeightSum[x].back().second + w);
}
for (ll i = 0; i < N; i++) {
fishWeightSum[i].emplace_back(N + 10, fishWeightSum[i].back().second);
}
SegTree st1(piers.size());
SegTree st2(piers.size());
SegTree st3(piers.size());
SegTree st4(piers.size());
SegTree st5(piers.size());
vector<array<ll, 2>> dpv(piers.size() + 1);
array<ll, 2>* dp = &dpv[1];
for (ll lp = (ll)piers.size() - 1; lp >= -1; lp--) {
ll cx = lp == -1 ? -5 : piers[lp].first;
ll cy = lp == -1 ? -5 : piers[lp].second;
ll x1ShorterFirstIdx = lower_bound(all(piers), make_pair(cx + 1, (ll)-1)) - piers.begin();
ll x1LongerFirstIdx = lower_bound(all(piers), make_pair(cx + 1, cy)) - piers.begin();
ll x2ShorterFirstIdx = lower_bound(all(piers), make_pair(cx + 2, (ll)-1)) - piers.begin();
ll x2LongerFirstIdx = lower_bound(all(piers), make_pair(cx + 2, cy)) - piers.begin();
ll c3FirstIdx = lower_bound(all(piers), make_pair(cx + 3, (ll)-1)) - piers.begin();
for (ll allowNext = 0; allowNext < 2; allowNext++) {
ll ans = 0;
ans = max(ans, st1.query(x1ShorterFirstIdx, x1LongerFirstIdx));
if (allowNext) {
//for (ll np = x1ShorterFirstIdx; np < x1LongerFirstIdx; np++) {
// auto [nx, ny] = piers[np];
// ans = max(ans, dp[np][0] + weightSum(nx + 1, ny) - weightSum(nx, ny));
//}
ans = max(ans, max(st2.query(x1LongerFirstIdx, x2ShorterFirstIdx), 0LL) - weightSum(cx, cy) - weightSum(cx + 1, cy));
//for (ll np = x1LongerFirstIdx; np < x2ShorterFirstIdx; np++) {
// auto [nx, ny] = piers[np];
// ans = max(ans, dp[np][1] + weightSum(nx + 1, ny) + weightSum(nx - 1, ny) - weightSum(cx, cy) - weightSum(cx + 1, cy));
//}
}
ans = max(ans, st3.query(x2ShorterFirstIdx, x2LongerFirstIdx));
//for (ll np = x2ShorterFirstIdx; np < x2LongerFirstIdx; np++) {
// auto [nx, ny] = piers[np];
// ans = max(ans, dp[np][1] + weightSum(nx + 1, ny));
//}
ans = max(ans, max(st4.query(x2LongerFirstIdx, c3FirstIdx), 0LL) - weightSum(cx + 1, cy));
//for (ll np = x2LongerFirstIdx; np < c3FirstIdx; np++) {
// auto [nx, ny] = piers[np];
// ans = max(ans, dp[np][1] + weightSum(nx + 1, ny) + weightSum(nx - 1, ny) - weightSum(cx + 1, cy));
//}
ans = max(ans, st5.query(c3FirstIdx, (ll)piers.size()));
//for (ll np = c3FirstIdx; np < (ll)piers.size(); np++) {
// auto [nx, ny] = piers[np];
// ans = max(ans, dp[np][1] + weightSum(nx + 1, ny) + weightSum(nx - 1, ny));
//}
dp[lp][allowNext] = ans;
}
if (lp != -1) {
st1.set(lp, dp[lp][0] + weightSum(cx + 1, cy) - weightSum(cx, cy));
st2.set(lp, dp[lp][1] + weightSum(cx + 1, cy) + weightSum(cx - 1, cy));
st3.set(lp, dp[lp][1] + weightSum(cx + 1, cy));
st4.set(lp, dp[lp][1] + weightSum(cx + 1, cy) + weightSum(cx - 1, cy));
st5.set(lp, dp[lp][1] + weightSum(cx + 1, cy) + weightSum(cx - 1, cy));
}
}
return dp[-1][1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
21300 KB |
Output is correct |
2 |
Correct |
120 ms |
25156 KB |
Output is correct |
3 |
Correct |
12 ms |
7332 KB |
Output is correct |
4 |
Correct |
14 ms |
7360 KB |
Output is correct |
5 |
Correct |
763 ms |
93096 KB |
Output is correct |
6 |
Correct |
817 ms |
94400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
352 ms |
44156 KB |
Output is correct |
3 |
Correct |
422 ms |
53388 KB |
Output is correct |
4 |
Correct |
103 ms |
21336 KB |
Output is correct |
5 |
Correct |
121 ms |
25156 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
224 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
11 ms |
7380 KB |
Output is correct |
11 |
Correct |
11 ms |
7320 KB |
Output is correct |
12 |
Correct |
197 ms |
30416 KB |
Output is correct |
13 |
Correct |
236 ms |
36232 KB |
Output is correct |
14 |
Correct |
150 ms |
25028 KB |
Output is correct |
15 |
Correct |
214 ms |
27844 KB |
Output is correct |
16 |
Correct |
155 ms |
25156 KB |
Output is correct |
17 |
Correct |
170 ms |
27820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
7368 KB |
Output is correct |
2 |
Correct |
13 ms |
7308 KB |
Output is correct |
3 |
Correct |
90 ms |
17956 KB |
Output is correct |
4 |
Correct |
63 ms |
15180 KB |
Output is correct |
5 |
Correct |
143 ms |
27752 KB |
Output is correct |
6 |
Correct |
153 ms |
27812 KB |
Output is correct |
7 |
Correct |
140 ms |
27700 KB |
Output is correct |
8 |
Correct |
141 ms |
27820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
772 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
772 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
104 ms |
10052 KB |
Output is correct |
18 |
Correct |
107 ms |
9920 KB |
Output is correct |
19 |
Correct |
93 ms |
9980 KB |
Output is correct |
20 |
Correct |
85 ms |
9344 KB |
Output is correct |
21 |
Correct |
82 ms |
9260 KB |
Output is correct |
22 |
Correct |
170 ms |
18220 KB |
Output is correct |
23 |
Correct |
24 ms |
2784 KB |
Output is correct |
24 |
Correct |
78 ms |
7948 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
27 ms |
2528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
772 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
104 ms |
10052 KB |
Output is correct |
18 |
Correct |
107 ms |
9920 KB |
Output is correct |
19 |
Correct |
93 ms |
9980 KB |
Output is correct |
20 |
Correct |
85 ms |
9344 KB |
Output is correct |
21 |
Correct |
82 ms |
9260 KB |
Output is correct |
22 |
Correct |
170 ms |
18220 KB |
Output is correct |
23 |
Correct |
24 ms |
2784 KB |
Output is correct |
24 |
Correct |
78 ms |
7948 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
27 ms |
2528 KB |
Output is correct |
27 |
Correct |
6 ms |
1416 KB |
Output is correct |
28 |
Correct |
512 ms |
46508 KB |
Output is correct |
29 |
Correct |
795 ms |
71884 KB |
Output is correct |
30 |
Correct |
948 ms |
84844 KB |
Output is correct |
31 |
Correct |
946 ms |
84400 KB |
Output is correct |
32 |
Correct |
647 ms |
58776 KB |
Output is correct |
33 |
Correct |
955 ms |
85360 KB |
Output is correct |
34 |
Correct |
958 ms |
85376 KB |
Output is correct |
35 |
Correct |
319 ms |
30868 KB |
Output is correct |
36 |
Correct |
969 ms |
82360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
7368 KB |
Output is correct |
2 |
Correct |
13 ms |
7308 KB |
Output is correct |
3 |
Correct |
90 ms |
17956 KB |
Output is correct |
4 |
Correct |
63 ms |
15180 KB |
Output is correct |
5 |
Correct |
143 ms |
27752 KB |
Output is correct |
6 |
Correct |
153 ms |
27812 KB |
Output is correct |
7 |
Correct |
140 ms |
27700 KB |
Output is correct |
8 |
Correct |
141 ms |
27820 KB |
Output is correct |
9 |
Correct |
251 ms |
37176 KB |
Output is correct |
10 |
Correct |
136 ms |
22676 KB |
Output is correct |
11 |
Correct |
295 ms |
44864 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
12 ms |
7260 KB |
Output is correct |
19 |
Correct |
14 ms |
7336 KB |
Output is correct |
20 |
Correct |
12 ms |
7288 KB |
Output is correct |
21 |
Correct |
12 ms |
7252 KB |
Output is correct |
22 |
Correct |
249 ms |
36340 KB |
Output is correct |
23 |
Correct |
444 ms |
58556 KB |
Output is correct |
24 |
Correct |
457 ms |
61356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
21300 KB |
Output is correct |
2 |
Correct |
120 ms |
25156 KB |
Output is correct |
3 |
Correct |
12 ms |
7332 KB |
Output is correct |
4 |
Correct |
14 ms |
7360 KB |
Output is correct |
5 |
Correct |
763 ms |
93096 KB |
Output is correct |
6 |
Correct |
817 ms |
94400 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
352 ms |
44156 KB |
Output is correct |
9 |
Correct |
422 ms |
53388 KB |
Output is correct |
10 |
Correct |
103 ms |
21336 KB |
Output is correct |
11 |
Correct |
121 ms |
25156 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
224 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
11 ms |
7380 KB |
Output is correct |
17 |
Correct |
11 ms |
7320 KB |
Output is correct |
18 |
Correct |
197 ms |
30416 KB |
Output is correct |
19 |
Correct |
236 ms |
36232 KB |
Output is correct |
20 |
Correct |
150 ms |
25028 KB |
Output is correct |
21 |
Correct |
214 ms |
27844 KB |
Output is correct |
22 |
Correct |
155 ms |
25156 KB |
Output is correct |
23 |
Correct |
170 ms |
27820 KB |
Output is correct |
24 |
Correct |
11 ms |
7368 KB |
Output is correct |
25 |
Correct |
13 ms |
7308 KB |
Output is correct |
26 |
Correct |
90 ms |
17956 KB |
Output is correct |
27 |
Correct |
63 ms |
15180 KB |
Output is correct |
28 |
Correct |
143 ms |
27752 KB |
Output is correct |
29 |
Correct |
153 ms |
27812 KB |
Output is correct |
30 |
Correct |
140 ms |
27700 KB |
Output is correct |
31 |
Correct |
141 ms |
27820 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
4 ms |
772 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
4 ms |
852 KB |
Output is correct |
48 |
Correct |
104 ms |
10052 KB |
Output is correct |
49 |
Correct |
107 ms |
9920 KB |
Output is correct |
50 |
Correct |
93 ms |
9980 KB |
Output is correct |
51 |
Correct |
85 ms |
9344 KB |
Output is correct |
52 |
Correct |
82 ms |
9260 KB |
Output is correct |
53 |
Correct |
170 ms |
18220 KB |
Output is correct |
54 |
Correct |
24 ms |
2784 KB |
Output is correct |
55 |
Correct |
78 ms |
7948 KB |
Output is correct |
56 |
Correct |
3 ms |
596 KB |
Output is correct |
57 |
Correct |
27 ms |
2528 KB |
Output is correct |
58 |
Correct |
6 ms |
1416 KB |
Output is correct |
59 |
Correct |
512 ms |
46508 KB |
Output is correct |
60 |
Correct |
795 ms |
71884 KB |
Output is correct |
61 |
Correct |
948 ms |
84844 KB |
Output is correct |
62 |
Correct |
946 ms |
84400 KB |
Output is correct |
63 |
Correct |
647 ms |
58776 KB |
Output is correct |
64 |
Correct |
955 ms |
85360 KB |
Output is correct |
65 |
Correct |
958 ms |
85376 KB |
Output is correct |
66 |
Correct |
319 ms |
30868 KB |
Output is correct |
67 |
Correct |
969 ms |
82360 KB |
Output is correct |
68 |
Correct |
251 ms |
37176 KB |
Output is correct |
69 |
Correct |
136 ms |
22676 KB |
Output is correct |
70 |
Correct |
295 ms |
44864 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
12 ms |
7260 KB |
Output is correct |
78 |
Correct |
14 ms |
7336 KB |
Output is correct |
79 |
Correct |
12 ms |
7288 KB |
Output is correct |
80 |
Correct |
12 ms |
7252 KB |
Output is correct |
81 |
Correct |
249 ms |
36340 KB |
Output is correct |
82 |
Correct |
444 ms |
58556 KB |
Output is correct |
83 |
Correct |
457 ms |
61356 KB |
Output is correct |
84 |
Correct |
983 ms |
88320 KB |
Output is correct |
85 |
Execution timed out |
1065 ms |
88712 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |