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 MAX 500005
#define INF (ll)(1e16)
using namespace std;
typedef long long ll;
int N, M;
ll up[MAX], down[MAX], sum[MAX], in[MAX], out[MAX];
vector<ll> X, Y, W;
vector<pair<int, ll> > pos[MAX];
long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y, std::vector<int> w) {
N = n, M = m; X.resize(M); Y.resize(N + M + 1); W.resize(M);
for(ll i = 0; i < M; ++i) X[i] = x[i] + 1, Y[i] = y[i] + 1, W[i] = w[i];
for(ll i = 0; i < M; ++i) pos[X[i]].push_back({Y[i], W[i]});
for(ll i = 1; i <= N; ++i) pos[i].push_back({N + 1, 0});
for(ll i = 1; i <= N; ++i) sort(pos[i].begin(), pos[i].end());
ll cnt = 0;
for(ll i = 1; i <= N; ++i) {
sum[++cnt] = pos[i][0].second; in[i] = cnt; Y[cnt] = pos[i][0].first;
for(ll j = 1; j < (ll)pos[i].size(); ++j) {
++cnt;
sum[cnt] = sum[cnt - 1] + pos[i][j].second;
Y[cnt] = pos[i][j].first;
}
out[i] = cnt;
}
for(ll i = 0; i <= N; ++i) up[i] = down[i] = -INF;
up[0] = down[0] = 0; Y[0] = -1;
for(ll x = 1; x <= N; ++x) {
ll k = in[x - 1], mx = -INF, p = -1;
for(ll i = in[x]; i <= out[x]; ++i) {
while(k <= out[x - 1] && Y[k] < Y[i]) {
mx = max(mx, up[k] - (k == in[x - 1] ? 0 : sum[k - 1]));
p = k; ++k;
}
up[i] = max(up[i], mx + (p == -1 ? 0 : sum[p]));
}
k = out[x - 1], mx = -INF, p = out[x];
for(ll i = out[x]; i >= in[x]; --i) {
while(k >= in[x - 1] && Y[k] > Y[i]) {
while(p >= in[x] && Y[k] <= Y[p]) --p;
mx = max(mx, max(down[k], up[k]) + sum[p]);
--k;
}
down[i] = max(down[i], mx - (i == in[x] ? 0 : sum[i - 1]));
if(k >= in[x - 1] && Y[k] == Y[i]) down[i] = max(down[i], max(down[k], up[k]));
}
if(x < 2) continue;
k = in[x - 2], mx = -INF, p = in[x - 1]; ll p2 = -1;
for(ll i = in[x]; i <= out[x]; ++i) {
while(k <= out[x - 2] && Y[k] <= Y[i]) {
mx = max(mx, max(up[k], down[k]));
++k;
}
while(p <= out[x - 1] && Y[p] < Y[i]) p2 = p++;
up[i] = max(up[i], mx + (p2 == -1 ? 0 : sum[p2]));
}
k = out[x - 2], mx = -INF, p = out[x - 1];
for(ll i = out[x]; i >= in[x]; --i) {
while(k >= in[x - 2] && Y[k] > Y[i]) {
while(p >= in[x - 1] && Y[k] <= Y[p]) --p;
mx = max(mx, max(up[k], down[k]) + (p == in[x - 1] - 1 ? 0 : sum[p]));
--k;
}
up[i] = max(up[i], mx);
}
}
ll ret = 0;
for(ll i = 0; i <= N + M; ++i) ret = max(ret, max(up[i], down[i]));
return ret;
}
# | 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... |