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>
#define int long long
using namespace std;
const int INF = 1e18;
int max_weights(signed N, signed M, vector<signed> X, vector<signed> Y,
vector<signed> W) {
vector<vector<pair<int, int>>> onCol(N);
vector<vector<int>> prefSum(N);
for (int i = 0; i < M; ++i)
onCol[X[i]].emplace_back(Y[i] + 1, W[i]);
for (int i = 0; i < N; ++i) {
sort(onCol[i].begin(), onCol[i].end());
prefSum[i].resize(onCol[i].size() + 1);
for (int j = 0; j < (int)onCol[i].size(); ++j)
prefSum[i][j + 1] = prefSum[i][j] + onCol[i][j].second;
}
vector<vector<int>> interesting(N);
for (int i = 0; i < N; ++i) {
interesting[i].push_back(0);
for (auto [y, w] : onCol[i])
interesting[i].push_back(y);
if (i)
for (auto [y, w] : onCol[i - 1])
interesting[i].push_back(y);
if (i + 1 < N)
for (auto [y, w] : onCol[i + 1])
interesting[i].push_back(y);
interesting[i].push_back(N);
sort(interesting[i].begin(), interesting[i].end());
interesting[i].resize(unique(interesting[i].begin(), interesting[i].end()) -
interesting[i].begin());
}
array<vector<pair<int, int>>, 2> oldDp, suffixMax;
for (int i = 0; i < 2; ++i) {
for (int x : interesting[0]) {
oldDp[i].emplace_back(x, 0);
suffixMax[i].emplace_back(x, 0);
}
}
auto eval = [&](int side, int h) {
auto it = upper_bound(oldDp[side].begin(), oldDp[side].end(), pair(h, INF));
assert(it != oldDp[side].begin());
--it;
assert(it->first == h);
return it->second;
};
auto getSum = [&](int i, int h1, int h2) -> int {
if (h1 > h2)
return 0;
int lo = upper_bound(onCol[i].begin(), onCol[i].end(), pair(h1, INF)) -
onCol[i].begin();
int up = upper_bound(onCol[i].begin(), onCol[i].end(), pair(h2, INF)) -
onCol[i].begin();
return prefSum[i][up] - prefSum[i][lo];
};
int sol = 0;
for (int i = 1; i < N; ++i) {
int nbInteressants = interesting[i].size();
vector<pair<int, int>> dp0, dp1;
int curOld = 0;
int cntBefore = interesting[i - 1].size();
vector<int> prevI(cntBefore);
vector<int> prevJ(cntBefore);
int curPos = 0;
int curSum = 0;
for (int jHauteur = 0; jHauteur < cntBefore; ++jHauteur) {
int oldH = interesting[i - 1][jHauteur];
prevI[jHauteur] = oldDp[1][jHauteur].second;
while (curPos < (int)onCol[i].size() and onCol[i][curPos].first <= oldH)
curSum += onCol[i][curPos++].second;
assert(curSum == getSum(i, 0, oldH));
prevI[jHauteur] += curSum;
}
for (int j = cntBefore - 2; j >= 0; --j)
prevI[j] = max(prevI[j], prevI[j + 1]);
curPos = 0, curSum = 0;
for (int jHauteur = 0; jHauteur < cntBefore; ++jHauteur) {
int oldH = interesting[i - 1][jHauteur];
while (curPos < (int)onCol[i - 1].size() and
onCol[i - 1][curPos].first <= oldH)
curSum += onCol[i - 1][curPos++].second;
prevJ[jHauteur] = oldDp[0][jHauteur].second - curSum;
}
for (int jHauteur = 1; jHauteur < cntBefore; ++jHauteur)
prevJ[jHauteur] = max(prevJ[jHauteur - 1], prevJ[jHauteur]);
int curOld2 = 0;
for (int iHauteur = 0; iHauteur < nbInteressants; ++iHauteur) {
int h = interesting[i][iHauteur];
// 1
int v1 = 0;
int v0 = 0;
while (curOld < cntBefore and interesting[i - 1][curOld] <= h)
++curOld;
assert(curOld);
v0 = suffixMax[1][curOld - 1].second;
if (curOld < cntBefore)
v1 = max(v1, prevI[curOld] - getSum(i, 0, h));
while (curOld2 < cntBefore and interesting[i - 1][curOld2] < h)
++curOld2;
if (curOld2)
v0 = max(v0, getSum(i - 1, 0, h) + prevJ[curOld2]);
/*for (int jHauteur = 0; jHauteur < cntBefore; ++jHauteur) {
int oldH = interesting[i - 1][jHauteur];
if (oldH < h)
v0 = max(v0, getSum(i - 1, oldH, h) + eval(0, oldH));
}*/
v1 = max(v1, v0);
sol = max(sol, v1);
dp0.emplace_back(h, v0);
dp1.emplace_back(h, v1);
// cout << i << ' ' << h << ' ' << v0 << ' ' << v1 << endl;
}
oldDp[0] = move(dp0);
oldDp[1] = move(dp1);
suffixMax[0] = oldDp[0];
suffixMax[1] = oldDp[1];
for (int h = nbInteressants - 2; h >= 0; --h)
for (int side = 0; side < 2; ++side)
suffixMax[side][h].second =
max(suffixMax[side][h].second, suffixMax[side][h + 1].second);
}
return sol;
}
Compilation message (stderr)
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:45:8: warning: variable 'eval' set but not used [-Wunused-but-set-variable]
45 | auto eval = [&](int side, int h) {
| ^~~~
# | 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... |