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;
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W){
vector<pair<int,int>> v[N];
vector<long long>pre[N];
for(int i = 0;i<M;i++){
v[X[i]].push_back({Y[i],W[i]});
}
for(int i = 0;i<N;i++){
v[i].push_back({N,0});
sort(v[i].begin(),v[i].end());
pre[i].assign(v[i].size(),0);
for(int j = 0;j<v[i].size();j++){
pre[i][j] = v[i][j].second + (j?pre[i][j-1]:0);
}
}
vector<pair<int,long long>> smaller = {{-1,0}};
vector<pair<int,long long>> bigger = {{-1,0}};
long long oldmaxi = 0;
long long maxi = 0;
for(int i = 0;i<N;i++){
vector<pair<int,long long>> nwsmaller;
vector<pair<int,long long>> nwbigger;
int nwsz = v[i].size();
int sz = smaller.size();
for(int j = 0;j<nwsz;j++){
nwsmaller.push_back({v[i][j].first-1,-1e18});
nwbigger.push_back({v[i][j].first-1,-1e18});
}
//1. operation
vector<pair<int,long long>> vals;
int pt = -1;
for(int j = 0;j<sz;j++){
while(pt != nwsz - 1 && v[i][pt + 1].first <= smaller[j].first)
pt++;
vals.push_back({smaller[j].first,smaller[j].second + (pt == -1?0:pre[i][pt])});
}
for(int j = sz-2;j>=0;j--){
vals[j].second = max(vals[j].second,vals[j+1].second);
}
pt = 0;
for(int j = 0;j < nwsz;j++){
while(pt != sz && vals[pt].first < nwsmaller[j].first)
pt++;
nwsmaller[j].second = max(nwsmaller[j].second,(pt != sz?vals[pt].second:(long long)-1e18) - (j?pre[i][j-1]:0));
}
//2. operation
pt = -1;
for(int j = 0;j < nwsz;j++){
while(i && pt != sz - 1&& v[i-1][pt+1].first <= nwbigger[j].first)
pt++;
nwbigger[j].second = max(nwbigger[j].second,oldmaxi + (pt != -1?pre[i-1][pt]:0));
}
//3. operation
vals.clear();
for(int j = 0;j<sz;j++){
vals.push_back(smaller[j]);
}
for(int j = 1;j<sz;j++){
vals[j].second = max(vals[j].second,vals[j-1].second);
}
pt = -1;
for(int j = 0;j<nwsz;j++){
while(pt != sz - 1 && vals[pt + 1].first < nwbigger[j].first)
pt++;
nwbigger[j].second = max(nwbigger[j].second,(pt != -1?vals[pt].second:(long long)-1e18));
}
//4. operation
vals.clear();
pt = -1;
for(int j = 0;j<sz;j++){
while(i && pt != sz -1 && v[i-1][pt + 1].first <= bigger[j].first)
pt++;
vals.push_back({bigger[j].first,bigger[j].second - (pt != -1?pre[i-1][pt]:0)});
}
for(int j = 1;j<sz;j++){
vals[j].second = max(vals[j].second,vals[j-1].second);
}
pt = -1;
int pt2 = -1;
for(int j = 0;j<nwsz;j++){
while(pt != sz - 1 && vals[pt + 1].first < nwbigger[j].first)
pt++;
while(i && pt2 != sz - 1&& v[i-1][pt2+1].first <= nwbigger[j].first)
pt2++;
nwbigger[j].second = max(nwbigger[j].second,(pt != -1?vals[pt].second:(long long)-1e18) + (pt2 != -1?pre[i-1][pt2]:0));
}
//5. operation
vals.clear();
pt = -1;
for(int j = 0;j<sz;j++){
while(i && pt != nwsz-1 && v[i][pt + 1].first <= bigger[j].first)
pt++;
vals.push_back({bigger[j].first,bigger[j].second + (pt != -1?pre[i][pt]:0)});
}
for(int j = sz-2;j>=0;j--){
vals[j].second = max(vals[j].second,vals[j+1].second);
}
// cout << "vals" << endl;
// for(auto u:vals){
// cout << u.first << ' ' << u.second << endl;
// }
pt = 0;
pt2 = -1;
for(int j = 0;j<nwsz;j++){
while(pt != sz && vals[pt].first < nwsmaller[j].first)
pt++;
while(pt2 != nwsz -1&& v[i][pt2+1].first <= nwsmaller[j].first)
pt2++;
// cout << "num"<< endl;
// cout << pt << ' ' << pt2 << endl;
nwsmaller[j].second = max(nwsmaller[j].second,(pt != sz?vals[pt].second:(long long)-1e18) - (pt2 != -1?pre[i][pt2]:0));
}
//operations done
oldmaxi = maxi;
smaller = nwsmaller;
bigger = nwbigger;
maxi = -1e18;
for(auto u:smaller){
maxi = max(maxi,u.second);
}
for(auto u:bigger){
maxi = max(maxi,u.second);
}
// cout << "HEY:" << i << endl;
// for(auto u:smaller){
// cout << u.first << ' ' << u.second << endl;
// }
// for(auto u:bigger){
// cout << u.first << ' ' << u.second << endl;
// }
}
return maxi;
}
Compilation message (stderr)
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:15:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int j = 0;j<v[i].size();j++){
| ~^~~~~~~~~~~~
# | 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... |