이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |