This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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; piers.reserve(M * 2);
vector<tuple<ll, ll, ll>> fish; fish.reserve(M);
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());
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();
ll ans = 0;
ans = max(ans, st1.query(x1ShorterFirstIdx, x1LongerFirstIdx));
ans = max(ans, st3.query(x2ShorterFirstIdx, x2LongerFirstIdx));
ans = max(ans, max(st2.query(x2LongerFirstIdx, c3FirstIdx), 0LL) - weightSum(cx + 1, cy));
ans = max(ans, st2.query(c3FirstIdx, (ll)piers.size()));
dp[lp][0] = ans;
ans = max(ans, max(st2.query(x1LongerFirstIdx, x2ShorterFirstIdx), 0LL) - weightSum(cx, cy) - weightSum(cx + 1, cy));
dp[lp][1] = ans;
/*
for (ll allowNext = 0; allowNext < 2; allowNext++) {
ll ans = 0;
ans = max(ans, st1.query(x1ShorterFirstIdx, x1LongerFirstIdx));
if (allowNext) {
ans = max(ans, max(st2.query(x1LongerFirstIdx, x2ShorterFirstIdx), 0LL) - weightSum(cx, cy) - weightSum(cx + 1, cy));
}
ans = max(ans, st3.query(x2ShorterFirstIdx, x2LongerFirstIdx));
ans = max(ans, max(st3.query(x2LongerFirstIdx, c3FirstIdx), 0LL) - weightSum(cx + 1, cy));
ans = max(ans, st3.query(c3FirstIdx, (ll)piers.size()));
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));
}
}
return dp[-1][1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |