#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 998244353;
const ll INF = 1e18;
const ld EPS = 1e-12;
#define endl "\n"
#define sp <<" "<<
#define REP(i, a, b) for(ll i = a; i < b; i++)
#define dbg(x) cout << #x << " = " << x << endl
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast_io() ios_base::sync_with_stdio(false); cin.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template <typename Key, typename Value>
using hash_map = unordered_map<Key, Value, custom_hash>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution<int>(a, b)(rng);
// shuffle(all(a), rng);
ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
// this will contain at most O(n? + m)
// time complexity O(n + m log m)
vector<vector<pair<ll, ll>>> fish(n+2);
REP(i, 0, m) {
fish[x[i]+1].push_back({y[i], w[i]});
}
// generate prefix sums to each height
fish[0].push_back({-1, 0});
REP(i, 1, n+1) {
fish[i].push_back({-1, 0});
sort(all(fish[i]));
REP(j, 1, fish[i].size()) {
fish[i][j].second += fish[i][j-1].second;
}
}
fish[n+1].push_back({-1, 0});
// generate indices of possible heights from neighbors
vector<vector<int>> inds(n+2);
inds[0].push_back(-1);
REP(i, 1, n+1) {
for (auto &f : fish[i-1]) {
inds[i].push_back(f.first);
}
for (auto &f : fish[i+1]) {
inds[i].push_back(f.first);
}
sort(all(inds[i]));
inds[i].erase(unique(all(inds[i])), inds[i].end());
}
inds[n+1].push_back(-1);
// prefix sum of less than or equal to j
auto sum = [&](ll i, ll j) {
return (upper_bound(all(fish[i]), mp(j, LONG_LONG_MAX)) - 1)->second;
};
vector<ll> dpu, dpd;
dpu.push_back(0), dpd.push_back(0);
// bitonic
REP(i, 1, n+2) {
int o = inds[i].size(), p = inds[i-1].size();
vector<ll> ndpu(o), ndpd(o);
ll pref = 0, suff = 0;
int pidx = 0, sidx = p;
// increasing on this one
// remove overlap
// add new
REP(j, 0, o) {
while (pidx < p and inds[i-1][pidx] < inds[i][j]) {
pref = max(pref, dpu[pidx] - sum(i-1, inds[i-1][pidx]));
pidx++;
}
ndpu[j] = max(dpd[0], pref + sum(i-1, inds[i][j]));
}
// decreasing on this one
// add new
// remove overlap
for (int j = o-1; j >= 0; j--) {
while (sidx > 0 and inds[i-1][sidx-1] >= inds[i][j]) {
sidx--;
suff = max(suff, max(dpu[sidx], dpd[sidx]) + sum(i, inds[i-1][sidx]));
}
ndpd[j] = suff - sum(i, inds[i][j]);
}
swap(dpu, ndpu), swap(dpd, ndpd);
}
return max(dpu[0], dpd[0]);
}
// signed main() {
// int n, m; cin >> n >> m;
// vector<int> x(m), y(m), w(m);
// REP(i, 0, m) {
// cin >> x[i] >> y[i] >> w[i];
// }
// cout << max_weights(n, m, x, y, w) << endl;
// }
# | 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... |