This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
author: Maksim1744
created: 26.04.2024 01:52:26
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun) fun
#define debugv(var) var
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#define OSTREAM(...) ;
#define OSTREAM0(...) ;
#endif
const ll inf = 1e18;
ll max_weights(int n, int m, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
vector<vector<pair<int, int>>> atx(n);
for (int i = 0; i < m; ++i) {
atx[X[i]].eb(Y[i], W[i]);
}
for (auto& v : atx)
sort(v.begin(), v.end());
vector<vector<ll>> up(n), down(n);
vector<ll> best(n);
for (int i = 0; i < n; ++i) {
up[i].assign(atx[i].size(), 0);
down[i].assign(atx[i].size(), 0);
if (i + 1 < n) {
int ind = 0;
ll prev = 0;
if (i >= 2) prev = max(prev, best[i - 2]);
if (i && !down[i - 1].empty()) prev = max(prev, maxe(down[i - 1]));
for (int j = 0; j < atx[i].size(); ++j) {
if (i) {
while (ind < atx[i - 1].size() && atx[i - 1][ind].first < atx[i][j].first) {
prev = max(prev, up[i - 1][ind]);
++ind;
}
}
if (j) up[i][j] = max(up[i][j], up[i][j - 1] + atx[i][j].second);
up[i][j] = max(up[i][j], prev + atx[i][j].second);
}
}
if (i > 0) {
int ind = (int)atx[i - 1].size() - 1;
ll prev = 0;
if (i >= 2) prev = max(prev, best[i - 2]);
for (int j = (int)atx[i].size() - 1; j >= 0; --j) {
if (i) {
while (ind >= 0 && atx[i - 1][ind].first > atx[i][j].first) {
prev = max(prev, down[i - 1][ind]);
--ind;
}
}
if (j + 1 < down[i].size())
down[i][j] = max(down[i][j], down[i][j + 1] + atx[i][j].second);
down[i][j] = max(down[i][j], prev + atx[i][j].second);
}
}
for (ll a : up[i]) best[i] = max(best[i], a);
for (ll a : down[i]) best[i] = max(best[i], a);
if (i) best[i] = max(best[i], best[i - 1]);
}
return best.back();
}
#ifdef HOUSE
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), w(m);
for (int i = 0; i < m; ++i) {
cin >> x[i] >> y[i] >> w[i];
}
cout << max_weights(n, m, x, y, w) << '\n';
return 0;
}
#endif
Compilation message (stderr)
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int j = 0; j < atx[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~
fish.cpp:75:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while (ind < atx[i - 1].size() && atx[i - 1][ind].first < atx[i][j].first) {
| ~~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if (j + 1 < down[i].size())
| ~~~~~~^~~~~~~~~~~~~~~~
# | 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... |