This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define Mp make_pair
#define pb push_back
#define endl '\n'
#define sep ' '
const int maxn = 3e5 + 7;
int n, m;
vector<pll> arr[maxn]; vector<ll> dp[2][maxn];
vector<ll> sm[maxn]; ll mx1[maxn], mx2[maxn];
void calx(int i, int T1) {
ll mx = 0;
fill(mx1, mx1 + len(arr[i + 1]), 0);
fill(mx2, mx2 + len(arr[i + 1]), 0);
for (int j = 0; j < len(arr[i]); j++) {
ll x = dp[T1][i][j]; mx = max(mx, x);
int j1 = lower_bound(all(arr[i + 1]), (pll) Mp(arr[i][j].F + 1, -1)) - arr[i + 1].begin();
int j2 = j1 - 1;
if (j1 < len(arr[i + 1])) {
if (T1 == 0) {
mx1[j1] = max(mx1[j1], x + sm[i][j]);
}
}
if (j2 >= 0) {
int r = lower_bound(all(arr[i + 1]), (pll) Mp(arr[i][j].F, -1)) - arr[i + 1].begin();
mx2[j2] = max(mx2[j2], x - sm[i + 1][r]);
}
}
for (int j = len(arr[i + 1]) - 2; j >= 0; j--) {
mx2[j] = max(mx2[j], mx2[j + 1]);
}
for (int j = 1; j < len(arr[i + 1]); j++) {
mx1[j] = max(mx1[j], mx1[j - 1]);
}
for (int j = 0; j < len(arr[i + 1]); j++) {
int r = lower_bound(all(arr[i]), (pll) Mp(arr[i + 1][j].F, -1)) - arr[i].begin();
mx1[j] -= sm[i][r]; mx2[j] += sm[i + 1][j];
}
for (int j = 0; j < len(arr[i + 1]); j++) {
for (int T2 : {0, 1}) {
dp[T2][i + 1][j] = max(dp[T2][i + 1][j], mx);
dp[T2][i + 1][j] = max(dp[T2][i + 1][j], mx1[j]);
if (T2 == 1) dp[T2][i + 1][j] = max(dp[T2][i + 1][j], mx2[j]);
}
}
}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N; m = M;
for (int i = 0; i < m; i++) {
arr[X[i]].pb(Mp(Y[i], W[i]));
}
for (int i = 0; i < n; i++) {
arr[i].pb(Mp(n, 0));
sort(all(arr[i])); sm[i].resize(len(arr[i]));
for (int j = len(arr[i]) - 2; j >= 0; j--) sm[i][j] = sm[i][j + 1] + arr[i][j].S;
dp[0][i].resize(len(arr[i])); dp[1][i].resize(len(arr[i]));
}
ll ans = 0;
for (int i = 0; i < n; i++) {
if (i + 1 < n) {
calx(i, 0); calx(i, 1);
}
for (int T : {0, 1}) {
for (ll x : dp[T][i]) ans = max(ans, x);
}
}
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... |