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>
using namespace std;
#define ll long long
#define t prev(s.end())
#define ff first
#define ss second
#define v vector
int n, q;
v<ll> helper(v<int32_t> h, v<int32_t> l, v<int32_t> r) {
v<v<pair<int, int>>> qq(n);
for (int i = 0; i < q; i++) qq[r[i]].push_back({ l[i], i });
v<ll> c(q);
ll curad = 0;
set <pair<int, pair<ll, ll>>> s;
v<pair<int, pair<ll, int>>> st;
v<v<int>> overtake(n + 1);
auto find_overtake = [&](pair<ll, ll> a, pair<ll, ll>b) -> int {
if (a.ff <= b.ff && a.ss <= b.ss) return 0;
if (b.ff <= a.ff && b.ss <= a.ss) return n;
int div = b.ss - a.ss;
return (int)min((ll)n, (a.ff - b.ff + (div - 1)) / div);
};
for (int i = 0; i < n; i++) {
while (st.size() && st.back().ss.ss < h[i]) st.pop_back();
if (st.size())curad = st.back().ss.ff + (ll)(i - st.back().ff) * h[i];
else curad = (ll)(i + 1) * h[i];
st.push_back({ i, {curad, h[i]} });
ll mn = curad - (ll)h[i] * i;int mni = i;
while (s.size() && t->ss.ss < h[i]) {
ll z = t->ss.ff + t->ss.ss * (i - 1) - (ll)h[i] * (i - 1);
if (z < mn) {
mn = z; mni = t->ff;
}
s.erase(t);
}
s.insert({ mni, {mn, h[i]} });
if (s.size() > 1) {
overtake[max(i, min(n, find_overtake(t->ss, prev(t)->ss)))].push_back(t->ff);
}
for (int k = 0; k < overtake[i].size(); k++) {
auto& x = overtake[i][k];
auto z = s.lower_bound({ x, {(ll)-1e18, (ll)-1e18} });
if (z == s.begin() || z == s.end() || z->ff != x) continue;
auto p = prev(z);
if (p->ss.ff + p->ss.ss * i < z->ss.ff + z->ss.ss * i) continue;
s.erase(p);
if (t == s.begin()) continue;
auto pp = prev(z);
overtake[max(i, min(n, find_overtake(z->ss, pp->ss)))].push_back(z->ff);
}
for (auto& x : qq[i]) {
c[x.ss] = 1e18;
int l = x.ff;
auto owo = lower_bound(st.begin(), st.end(), pair<int, pair<ll, int>>({ l, {(ll)-1e18, (int)-1e9 } }));
auto lll = s.lower_bound({ owo->ff, {(ll)-1e18, (ll)-1e18} });
if (lll == s.end()) continue;
c[x.ss] = lll->ss.ff + lll->ss.ss * i - owo->ss.ff + (ll)(owo->ff - l + 1) * owo->ss.ss;
}
}
return c;
}
v<ll> minimum_costs(v<int32_t> h, v<int32_t> l, v<int32_t> r) {
n = h.size(); q = l.size();
v<ll> a = helper(h, l, r);
reverse(h.begin(), h.end());
for (int i = 0; i < q; i++) { swap(l[i], r[i]); l[i] = n - 1 - l[i]; r[i] = n - 1 - r[i]; }
v<ll> b = helper(h, l, r);
v<ll> c(q); for (int i = 0; i < q;i++) c[i] = min(a[i], b[i]);
for (int i = 0; i < q; i++) {
if (c[i] == 1e18)
c[i] = (ll)(r[i] - l[i] + 1) * h[l[i]];
}
return c;
}
Compilation message (stderr)
meetings.cpp: In function 'std::vector<long long int> helper(std::vector<int>, std::vector<int>, std::vector<int>)':
meetings.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int k = 0; k < overtake[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~
# | 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... |