이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "fish.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define lb lower_bound
#define ub upper_bound
const int MAX=3e5+10;
vector<pair<ll,ll>> y[MAX];
vector<pair<int,int>> vec[MAX];
vector<ll> p[MAX];
vector<ll> dp[MAX][2];
vector<ll> can[MAX];
ll getR(int x,ll r){
if(r<0)return 0;
int R=ub(all(y[x]),make_pair(r+1,-1ll))-y[x].begin()-1;
if(R<0)return 0;
return p[x][R];
}
long long 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]++;
y[X[i]].pb({Y[i],i});
vec[Y[i]].pb({X[i],W[i]});
}
for(int i=1;i<=N;i++){
if(y[i].empty())continue;
sort(all(y[i]));
p[i].pb(W[y[i][0].S]);
for(int j=1;j<sz(y[i]);j++){
p[i].pb(p[i].back()+W[y[i][j].S]);
}
}
can[0].pb(0);
for(int i=1;i<=N;i++){
can[i].pb(0);
if(i>0){
for(auto [x,b]:y[i-1])can[i].pb(x);
}
if(i+1<=N){
for(auto [x,b]:y[i+1])can[i].pb(x);
}
sort(all(can[i]));
can[i].erase(unique(all(can[i])),can[i].end());
}
for(int i=0;i<=N+1;i++){
dp[i][0].resize(sz(can[i]));
dp[i][1].resize(sz(can[i]));
for(int j=0;j<sz(can[i]);j++){
dp[i][0][j]=dp[i][1][j]=0;
}
}
for(int i=1;i<=N;i++){
{
int r=sz(can[i-1]);
ll mx=0;
for(int j=sz(can[i])-1;j>=0;j--){
while(r-1>=0&&can[i-1][r-1]>=can[i][j]){
mx=max(mx,max(dp[i-1][0][r-1],dp[i-1][1][r-1])+getR(i,can[i-1][r-1]));
r--;
}
dp[i][0][j]=mx-getR(i,can[i][j]);
}
}
{
int r=0;
ll mx=0;
for(int j=0;j<sz(can[i]);j++){
while(r<sz(can[i-1])&&can[i-1][r]<=can[i][j]){
mx=max(mx,dp[i-1][1][r]-getR(i-1,can[i-1][r]));
r++;
}
dp[i][1][j]=mx+getR(i-1,can[i][j]);
dp[i][1][j]=max(dp[i][1][j],dp[i-1][0][0]);
}
}
}
ll ans=0;
for(int j=0;j<sz(can[N]);j++)ans=max(ans,max(dp[N][0][j],dp[N][1][j]));
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... |