This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 27.06.2023 11:42:33 *
*************************************/
#include "meetings.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// end of template
const long long INF = 1e18;
vector <long long> minimum_costs(std::vector<int> H, vector <int> L, vector <int> R) {
int N = H.size();
int Q = L.size();
vector <long long> ans;
if (max(N, Q) <= 5000) {
vector <vector <long long>> cost;
REP(i, N) {
vector <long long> pref(N + 1);
pref[i + 1] = H[i];
REPD(j, i) pref[j + 1] = max(1LL * H[j], pref[j + 2]);
FOR(j, i + 1, N) pref[j + 1] = max(1LL * H[j], pref[j]);
partial_sum(ALL(pref), pref.begin());
cost.push_back(pref);
}
REP(i, Q) {
long long res = INF;
FORE(j, L[i], R[i]) minimize(res, cost[j][R[i] + 1] - cost[j][L[i]]);
ans.push_back(res);
}
return ans;
}
int k = *max_element(ALL(H));
if (k > 20) return ans;
stack <int> st;
vector <vector <int>> nxt(N, vector<int>(k + 1, N)), prv(N, vector<int>(k + 1, 0));
vector <long long> cost_left(N), cost_right(N);
vector <int> left(N), right(N);
REP(i, N) {
while (!st.empty() && H[st.top()] <= H[i]) st.pop();
if (st.empty()) {
left[i] = -1;
cost_left[i] = 1LL * H[i] * (i + 1);
}
else {
left[i] = st.top();
cost_left[i] = 1LL * H[i] * (i - left[i]) + cost_left[left[i]];
}
st.push(i);
if (i) prv[i] = prv[i - 1];
prv[i][H[i]] = i;
}
while (!st.empty()) st.pop();
REPD(i, N) {
while (!st.empty() && H[st.top()] <= H[i]) st.pop();
if (st.empty()) {
right[i] = N;
cost_right[i] = 1LL * H[i] * (N - i);
} else {
right[i] = st.top();
cost_right[i] = 1LL * H[i] * (right[i] - i) + cost_right[right[i]];
}
st.push(i);
if (i + 1 < N) nxt[i] = nxt[i + 1];
nxt[i][H[i]] = i;
}
vector <vector <long long>> mi_st;
mi_st.push_back(vector<long long>(N));
REP(i, N) mi_st[0][i] = cost_left[i] + cost_right[i] - H[i];
for (int k = 1; MASK(k) <= N; ++k) {
vector <long long> nxt;
REP(i, N - MASK(k) + 1) nxt.push_back(min(mi_st.back()[i], mi_st.back()[i + MASK(k - 1)]));
mi_st.push_back(nxt);
}
auto get_min = [&] (int l, int r) {
int h = __lg(r - l + 1);
return min(mi_st[h][l], mi_st[h][r - MASK(h) + 1]);
};
REP(q, Q) {
int ma = k;
while (nxt[L[q]][ma] > R[q]) --ma;
long long res = INF;
FORE(km, 1, ma) {
int l = nxt[L[q]][km];
int r = prv[R[q]][ma];
if (left[l] < L[q] && l <= r) {
long long cur = get_min(l, r);
cur -= km * (L[q] - left[l] - 1) + (~left[l] ? cost_left[left[l]] : 0);
cur -= ma * (right[r] - R[q] - 1) + (right[r] < N ? cost_right[right[r]] : 0);
minimize(res, cur);
}
l = nxt[L[q]][ma];
r = prv[R[q]][km];
if (right[r] > R[q] && l <= r) {
long long cur = get_min(l, r);
cur -= ma * (L[q] - left[l] - 1) + (~left[l] ? cost_left[left[l]] : 0);
cur -= km * (right[r] - R[q] - 1) + (right[r] < N ? cost_right[right[r]] : 0);
minimize(res, cur);
}
}
ans.push_back(res);
}
return ans;
}
#ifdef LOCAL
#include <cstdio>
#include <cstdlib>
#include <vector>
#include "meetings.h"
namespace {
int read_int() {
int x;
if (scanf("%d", &x) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return x;
}
} // namespace
int main() {
file("meetings");
int N = read_int();
int Q = read_int();
std::vector<int> H(N);
for (int i = 0; i < N; ++i) {
H[i] = read_int();
}
std::vector<int> L(Q), R(Q);
for (int j = 0; j < Q; ++j) {
L[j] = read_int();
R[j] = read_int();
}
std::vector<long long> C = minimum_costs(H, L, R);
for (size_t j = 0; j < C.size(); ++j) {
printf("%lld\n", C[j]);
}
return 0;
}
#endif
# | 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... |