이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "meetings.h"
#include <bits/stdc++.h>
using namespace std;
template<class C>constexpr int sz(const C&c){return int(c.size());}
using ll=long long;constexpr const char nl='\n',sp=' ';
template <class T, class F> struct SparseTable {
int N, lg; vector<vector<T>> ST; F op;
void build() { for (int i = 0; i < lg - 1; i++) for (int j = 0; j < N; j++) ST[i + 1][j] = op(ST[i][j], ST[i][min(j + (1 << i), N - 1)]); }
template <class It> SparseTable(It st, It en, F op) : N(en - st), lg(32 - __builtin_clz(N)), ST(lg, vector<T>(st, en)), op(op) { build(); }
template <class It> SparseTable(It st, It en) : SparseTable(st, en, F()) {}
// 0-indexed, inclusive
T query(int l, int r) { int i = 31 - __builtin_clz(r - l + 1); return op(ST[i][l], ST[i][r - (1 << i) + 1]); }
};
template <class It, class F> auto makeSparseTable(It st, It en, F op) {
return SparseTable<typename std::iterator_traits<It>::value_type, F>(st, en, op);
}
struct Line {
ll m, b;
int l, r;
Line(ll m, ll b, int l, int r) : m(m), b(b), l(l), r(r) {}
};
const ll INF = 0x3f3f3f3f3f3f3f3f;
vector<ll> minimum_costs(vector<int> H, vector<int> L, vector<int> R) {
int N = sz(H),Q = sz(L);
vector<int> P(N, 0);
iota(P.begin(), P.end(), 0);
auto ST = makeSparseTable(P.begin(), P.end(), [&] (const int &i, const int &j) { return H[i] > H[j] ? i : j; });
vector<ll> ans(Q, INF);
vector<vector<int>> queries(N);
for (int i = 0; i < Q; i++) {
if (L[i] == R[i]) ans[i] = H[L[i]];
else queries[ST.query(L[i], R[i])].push_back(i);
}
vector<Line> lLines(N, Line(0, INF, N, -1)), rLines(N, Line(0, INF, N, -1));
auto query = [&] (const vector<Line> &lines, ll x) {
ll mn = INF;
for (auto &&line : lines) if (line.l <= x && x <= line.r) mn = min(mn, line.m * x + line.b);
return mn;
};
function<void(int, int)> solve = [&] (int l, int r) {
if (l > r) return;
int m = ST.query(l, r);
ll hm = H[m];
solve(l, m - 1);
solve(m + 1, r);
for (int i : queries[m]) ans[i] = min(query(lLines, L[i]) + (R[i] - m + 1) * hm, query(rLines, R[i]) + (m - L[i] + 1) * hm);
ll lDelta = (r - m + 1) * hm, rDelta = (m - l + 1) * hm;
for (int i = l; i <= m - 1; i++) lLines[i].b += lDelta;
for (int i = m + 1; i <= r; i++) rLines[i].b += rDelta;
lLines[m] = Line(-hm, (m < r ? query(lLines, m + 1) : 0) + (m + 1) * hm, l, m);
rLines[m] = Line(hm, (l < m ? query(rLines, m - 1) : 0) - (m - 1) * hm, m, r);
};
solve(0, N - 1);
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... |