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 namespace std;
using ll = long long;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
for(int i=0; i<M; i++) X[i]++, Y[i]++;
map<int, int> mp[N+5];
for(int i=0; i<M; i++) mp[X[i]][Y[i]] = W[i];
ll ans = 0;
vector<pair<int, int> > vec[N+5];
vector<int> pos[N+5], SZ(N+5);
for(int i=1; i<=N; i++) vec[i].push_back({ 0, 0 });
for(int i=0; i<M; i++) vec[X[i]].push_back({ Y[i], W[i] });
for(int i=1; i<=N; i++) {
set<int> st;
for(int j=-1; j<=1; j++) for(auto &x : vec[i-j]) st.insert(x.first);
for(auto &x : st) pos[i].push_back(x);
sort(vec[i].begin(), vec[i].end());
}
vector<vector<ll> > pref(N+5), pmx1(N+5), smx1(N+5), pmx2(N+5), smx2(N+5);
vector<vector<vector<ll> > > dp(N+5);
for(int i=N; i>=1; i--) {
SZ[i] = pos[i].size() - 1;
pref[i].resize(SZ[i] + 10);
pmx1[i].resize(SZ[i] + 10);
smx1[i].resize(SZ[i] + 10);
pmx2[i].resize(SZ[i] + 10);
smx2[i].resize(SZ[i] + 10);
dp[i].resize(SZ[i] + 5, vector<ll>(2));
if(i == 1) {
pmx1[i][0] = dp[i][0][1] - pref[i][0];
for(int j=1; j<=SZ[i]; j++) pmx1[i][j] = max(pmx1[i][j-1], dp[i][j][1] - pref[i][j]);
for(int j=SZ[i]; j>=0; j--) {
int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
smx1[i][j] = pref[i+1][p];
}
for(int j=SZ[i]-1; j>=0; j--) smx1[i][j] = max(smx1[i][j], smx1[i][j+1]);
}
for(int j=1; j<=SZ[i]; j++) pref[i][j] = pref[i][j-1] + mp[i][pos[i][j]];
}
for(int i=2; i<=N; i++) {
int ptr = 0;
for(int j=0; j<=SZ[i]; j++) {
int p = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i][j]) - pos[i-1].begin() - 1;
dp[i][j][1] = max(dp[i][j][1], pmx1[i-1][p] + pref[i-1][p]);
while(ptr + 1 <= SZ[i-1] && pos[i-1][ptr] < pos[i][j]) ptr++;
if(pos[i-1][ptr] >= pos[i][j]) dp[i][j][0] = max(dp[i][j][0], smx1[i-1][ptr] - pref[i][j]);
if(i < 3) continue;
int pa = upper_bound(pos[i-2].begin(), pos[i-2].end(), pos[i][j]) - pos[i-2].begin() - 1;
int pb = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i][j]) - pos[i-1].begin() - 1;
dp[i][j][0] = max(dp[i][j][0], pmx2[i-2][pa] + pref[i-1][pb]);
for(int k=0; k<=SZ[i-2]; k++) {
int p1 = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i][j]) - pos[i-1].begin() - 1;
int p2 = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i-2][k]) - pos[i-1].begin() - 1;
dp[i][j][1] = max(dp[i][j][1], max(dp[i-2][k][0], dp[i-2][k][1]) + max((p1 > -1 ? pref[i-1][p1] : 0), (p2 > -1 ? pref[i-1][p2] : 0)));
}
}
pmx1[i][0] = dp[i][0][1] - pref[i][0];
pmx2[i][0] = max(dp[i][0][0], dp[i][0][1]);
for(int j=1; j<=SZ[i]; j++) {
pmx1[i][j] = max(pmx1[i][j-1], dp[i][j][1] - pref[i][j]);
pmx2[i][j] = max(pmx2[i][j-1], max(dp[i][j][0], dp[i][j][1]));
}
if(i == N) continue;
for(int j=SZ[i]; j>=0; j--) {
int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
smx1[i][j] = max(dp[i][j][0], dp[i][j][1]) + pref[i+1][p];
}
for(int j=SZ[i]-1; j>=0; j--) smx1[i][j] = max(smx1[i][j], smx1[i][j+1]);
}
for(int i=1; i<=N; i++) {
for(int j=0; j<=SZ[i]; j++) {
if(pref[i+1].empty() || j == 0) ans = max({ ans, dp[i][j][0], dp[i][j][1] });
else {
int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
ans = max(ans, max(dp[i][j][0], dp[i][j][1]) + pref[i+1][p]);
}
}
}
return ans;
}
# | 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... |