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 "meetings.h"
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using ll = long long;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
template<class M>
class SparseTable {
using T = typename M::T;
int n, h;
vector<vector<T>> dat;
public:
SparseTable(vector<T> a) {
n = a.size();
h = __lg(n) + 1;
dat.assign(h + 1, vector<T>(n, M::id()));
rep (i, n) dat[0][i] = a[i];
rep (i, h) rep (j, n - (1 << i)) {
dat[i + 1][j] = M::op(dat[i][j], dat[i][j + (1 << i)]);
}
}
T prod(int l, int r) {
assert(0 <= l && l <= r && r <= n);
if (l == r) return M::id();
int i = __lg(r - l);
return M::op(dat[i][l], dat[i][r - (1 << i)]);
}
};
constexpr int inf = 1.01e9;
constexpr ll infl = 1e18;
struct Min {
using T = int;
static T op(T a, T b) { return min(a, b); }
static T id() { return inf; }
};
struct PairMax {
using T = pair<int, int>;
static T op(T a, T b) { return max(a, b); }
static T id() { return {-inf, -1}; }
};
std::vector<long long> minimum_costs(std::vector<int> H, std::vector<int> L, std::vector<int> R) {
SparseTable<PairMax> st([&] {
vector<pair<int, int>> a(H.size());
rep (i, H.size()) a[i] = {H[i], i};
return a;
}());
int Q = L.size();
vector<ll> ans(Q);
rep (i, Q) {
auto dfs = [&](auto& dfs, int l, int r) -> ll {
if (l == r) return 0;
if (l + 1 == r) return H[l];
auto [h, idx] = st.prod(l, r);
ll res = infl;
chmin(res, dfs(dfs, l, idx) + h * (ll)(r - idx));
chmin(res, dfs(dfs, idx + 1, r) + h * (ll)(idx - l + 1));
return res;
};
ans[i] = dfs(dfs, L[i], ++R[i]);
}
return ans;
}
# | 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... |