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 <bits/stdc++.h>
#include "fish.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const ll INF = 1e18 + 69;
int n, m;
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){
X[i]++; Y[i]++;
}
vector<int> coord[n + 2];
for(int i = 0; i<m; ++i){
for(int j = X[i] - 1; j <= X[i] + 1; ++j){
coord[j].push_back(Y[i]);
coord[j].push_back(Y[i]-1);
}
}
vector<ll> grid[n+2];
for(int i= 0; i<=n; ++i){
coord[i].push_back(0); coord[i].push_back(n);
remove_dup(coord[i]);
grid[i].resize(coord[i].size());
}
for(int i = 0; i<m; ++i){
int j = lower_bound(ALL(coord[X[i]]), Y[i]) - coord[X[i]].begin();
grid[X[i]][j] += W[i];
}
for(int i = 1; i<=n; ++i){
for(int j = 1; j < (int)grid[i].size(); ++j)
grid[i][j] += grid[i][j-1];
}
vector<array<ll, 2>> dp[n+2];
for(int i = 0; i<=n; ++i){
dp[i].resize(coord[i].size(), {{-INF, -INF}});
}
// // dp[n][m][ascending];
dp[0][0][1] = 0;
for(int i = 1; i<=n; ++i){
maximize(dp[i][0][1], dp[i-1][0][0]);
// increase
int len = coord[i].size();
block_of_code{
int _len = coord[i-1].size();
ll pref[len], sum[len];
fill(pref, pref + len, -INF);
fill(sum, sum + len, -INF);
for(int j = 0; j < _len; ++j){
int idx = lower_bound(ALL(coord[i]), coord[i-1][j]) - coord[i].begin();
ll val = dp[i-1][j][1] - grid[i-1][j];
maximize(pref[idx], val);
maximize(sum[idx], grid[i-1][j]);
}
for(int j = 1; j < len; ++j) {
maximize(pref[j], pref[j-1]);
maximize(sum[j], sum[j-1]);
}
for(int j = 0; j < len; ++j){
maximize(dp[i][j][1], pref[j] + sum[j]);
}
}
// decrease
block_of_code{
int _len = coord[i-1].size();
ll suff[len]; fill(suff, suff + len, -INF);
for(int j = 0; j < _len; ++j){
int idx = upper_bound(ALL(coord[i]), coord[i-1][j]) - coord[i].begin() - 1;
ll val = max(dp[i-1][j][0], dp[i-1][j][1]);
maximize(suff[idx], val);
}
for(int j = 0; j < len; ++j) suff[j] += grid[i][j];
for(int j = len - 1; j >= 1; --j) maximize(suff[j-1], suff[j]);
for(int j = len-1; j >= 0; --j){
maximize(dp[i][j][0], suff[j] - grid[i][j]);
}
}
// jump two
if (i >= 2){
ll pref[len], suff[len], sum[len];
fill(pref, pref + len, -INF);
fill(suff, suff + len, -INF);
fill(sum, sum + len, 0);
int _len = coord[i-2].size();
for(int j = 0; j < _len; ++j){
ll val = max(dp[i-2][j][0], dp[i-2][j][1]);
int idx = lower_bound(ALL(coord[i]), coord[i-2][j]) - coord[i].begin();
maximize(pref[idx], val);
idx = upper_bound(ALL(coord[i]), coord[i-2][j]) - coord[i].begin() - 1;
maximize(suff[idx], val);
}
for(int j = 0; j < (int) coord[i-1].size(); ++j){
int idx = lower_bound(ALL(coord[i]), coord[i-1][j]) - coord[i].begin();
sum[idx] = grid[i-1][j];
}
for(int j = 1; j < len; ++j) {
maximize(sum[j], sum[j-1]);
maximize(pref[j], pref[j-1]);
}
for(int j = len-1; j>=0; --j){
suff[j] += sum[j];
if (j < len - 1) maximize(suff[j], suff[j+1]);
}
for(int j = 0; j<len; ++j){
maximize(dp[i][j][1], max(pref[j] + sum[j], suff[j]));
}
}
// cout << "Dp = " << i << "\n";
// for(auto j: dp[i]) printArr(j);
}
ll ans = 0;
for(int j = 0; j < coord[n].size(); ++j) for(int x = 0; x <= 1; ++x) maximize(ans, dp[n][j][x]);
return ans;
}
// int main(void){
// ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
// clock_t start = clock();
// 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";
// cerr << "Time elapsed: " << clock() - start << " ms\n";
// return 0;
// }
Compilation message (stderr)
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:181:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
181 | for(int j = 0; j < coord[n].size(); ++j) for(int x = 0; x <= 1; ++x) maximize(ans, dp[n][j][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... |