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;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = long long;
const ll N = 3e5 + 10;
const ll INF = 1e17;
ll n, m;
vector<pair<ll,ll> > fish[N];
ll get(ll x,ll y){
return (upper_bound(all(fish[x]), make_pair(y, INF)) - 1)->second;
}
vector<ll> A[N];
ll max_weights(int nn, int mm, vector<int> X, vector<int> Y,vector<int> W) {
n = nn;
m = mm;
for(ll i =0 ;i<m;i++){
fish[X[i] + 1].pb({Y[i], W[i]});
}
fish[0] = {make_pair(-1, 0)};
for(ll i = 1;i<=n;i++){
fish[i].pb({-1, 0});
sort(all(fish[i]));
for(ll j = 1;j< fish[i].size();j++)fish[i][j].second += fish[i][j-1].second;
}
fish[n + 1] = {make_pair(-1, 0)};
A[0] = {-1};
for(ll i = 1;i<=n;i++){
for(auto u : fish[i-1]){
A[i].pb({u.first});
}
for(auto u : fish[i+1]){
A[i].pb({u.first});
}
sort(all(A[i]));
A[i].erase(unique(all(A[i])), A[i].end());
}
A[n+1] = {-1};
vector<ll>dpu = {0}, dpd = {0};
//assert(0);
for(ll i = 1;i<=n+1;i++){
vector<ll> ndpu(A[i].size());
ll pref = 0;
ll ind = 0;
//cout<<i<<endl;
for(ll j = 0;j < A[i].size();j++){
//cout<<j<<endl;
ll x = A[i][j];
while(ind < A[i-1].size() && A[i-1][ind] <= x){
pref = max(pref, dpu[ind] - get(i-1, A[i-1][ind]));
++ind;
}
ndpu[j] = max(pref + get(i-1, x), dpd[0]);
}
vector<ll> ndpd(A[i].size());
ll suff = 0;
ind = A[i-1].size();
for(ll j = A[i].size() - 1;j >= 0; j--){
ll x = A[i][j];
while(ind && A[i-1][ind - 1] >= x){
--ind;
suff = max(suff, max(dpu[ind], dpd[ind]) + get(i, A[i-1][ind]));
}
ndpd[j] = suff - get(i, x);
}
swap(dpu, ndpu);
swap(dpd, ndpd);
}
return max(dpu[0], dpd[0]);
}
Compilation message (stderr)
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:41:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(ll j = 1;j< fish[i].size();j++)fish[i][j].second += fish[i][j-1].second;
| ~^~~~~~~~~~~~~~~~
fish.cpp:70:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(ll j = 0;j < A[i].size();j++){
| ~~^~~~~~~~~~~~~
fish.cpp:74:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while(ind < A[i-1].size() && A[i-1][ind] <= x){
| ~~~~^~~~~~~~~~~~~~~
# | 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... |