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>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 1LL << 60;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int n;
struct Line{
ll a = 0, b = 0, l = 1, r = n;
ll operator()(ll x){
return a * x + b;
}
};
ostream& operator<<(ostream& o, Line ln){
return o << '(' << ln.a << ',' << ln.b << ',' << ln.l << ',' << ln.r << ')';
}
ll intersection(Line l1, Line l2){
return iceil(l1.b - l2.b, l2.a - l1.a);
}
struct Node{
Line ln;
int l = -1, r = -1;
};
struct DynamicHull{
vector<Node> v;
int lst = -1;
deque<Line> dq;
int qt = -1;
void print(){
int tmp = lst;
if(tmp == -1){
cerr << "empty\n";
return;
}
while(v[tmp].l != -1) tmp = v[tmp].l;
while(tmp != -1){
cerr << '(' << tmp << ',' << v[tmp].ln << ')' << " ";
tmp = v[tmp].r;
}
cerr << "\n";
}
int newnode(Line ln){
v.eb(Node({ln}));
return (int)v.size() - 1;
}
void insert(Line ln){
//cerr << "insert " << ln << "\n";
//print();
int pre = -1;
int nxt = -1;
bool ok = false;
if(lst == -1) ok = true;
while(lst != -1){
Line now = v[lst].ln;
if(ln.r < now.l) break;
if(now.r < ln.l){
lst = v[lst].r;
continue;
}
if(ln(now.r) > now(now.r)){
lst = v[lst].r;
continue;
}
ok = true;
pre = v[lst].l;
nxt = v[lst].r;
if(ln(now.l) <= now(now.l)){
if(ln.r >= now.r){
if(lst == qt) qt = -1;
if(nxt != -1) v[nxt].l = pre;
if(pre != -1) v[pre].r = nxt;
lst = pre;
}
else{
v[lst].ln.l = ln.r + 1;
lst = pre;
}
continue;
}
int mid = intersection(now, ln);
if(ln.r >= now.r){
v[lst].ln.r = mid - 1;
pre = lst;
break;
}
v[lst].ln.r = mid - 1;
int tmp = newnode(Line({now.a, now.b, ln.r + 1, now.r}));
v[tmp].l = lst;
v[tmp].r = nxt;
if(nxt != -1) v[nxt].l = lst;
v[lst].r = tmp;
pre = lst;
nxt = tmp;
break;
}
if(!ok){
//cerr << "no ";
//print();
return;
}
lst = newnode(ln);
v[lst].l = pre;
v[lst].r = nxt;
if(qt == -1) qt = lst;
if(pre != -1) v[pre].r = lst;
if(nxt != -1) v[nxt].l = lst;
//cerr << "ok ";
//print();
}
ll query(int x){
while(qt != -1 && v[qt].ln.r < x) qt = v[qt].r;
if(qt == -1) return 1LL << 60;
return v[qt].ln(x);
}
};
#define lc 2 * id + 1
#define rc 2 * id + 2
struct SegmentTree{
vector<DynamicHull> st;
//vector<vector<Line>> st;
void init(){
st.resize(4 * n);
}
void modify(int x, Line ln, int L = 1, int R = n, int id = 0){
st[id].insert(ln);
//st[id].eb(ln);
if(L == R){
return;
}
int M = (L + R) / 2;
if(x <= M) modify(x, ln, L, M, lc);
else modify(x, ln, M + 1, R, rc);
}
ll query(int l, int r, int x, int L = 1, int R = n, int id = 0){
if(l <= L && R <= r){
/*ll ans = MAX;
for(Line ln : st[id]){
if(x <= ln.r) ans = min(ans, ln(x));
}*/
return st[id].query(x);
//return ans;
}
int M = (L + R) / 2;
if(r <= M) return query(l, r, x, L, M, lc);
else if(l > M) return query(l, r, x, M + 1, R, rc);
else{
return min(query(l, r, x, L, M, lc), query(l, r, x, M + 1, R, rc));
}
}
};
vector<ll> minimum_costs(vector<int> H, vector<int> L, vector<int> R){
n = H.size();
int q = L.size();
vector<ll> h(n + 1);
for(int i = 0; i < n; i++) h[i + 1] = H[i];
for(int i = 0; i < q; i++) L[i]++, R[i]++;
vector<ll> sum(n + 1), lsum(n + 2), rsum(n + 2);
vector<int> lp(n + 1), rp(n + 1);
vector<vector<int>> qry(n + 1);
vector<ll> ans(q, MAX);
vector<int> lb(q, -1), rb(q, -1);
for(int i = 0; i < q; i++){
qry[R[i]].eb(i);
}
auto init = [&](int v, vector<ll>& ssum, vector<int>& p){
deque<int> dq;
dq.eb(v == 1 ? 0 : n + 1);
ll tmp = 0;
for(int i = v == 1 ? 1 : n; 1 <= i && i <= n; i += v){
while(dq.size() > 1 && h[dq.back()] < h[i]){
int t = dq.back();
dq.pob;
tmp -= abs(t - dq.back()) * h[t];
}
tmp += abs(i - dq.back()) * h[i];
ssum[i] = tmp;
p[i] = dq.back();
dq.eb(i);
}
};
init(1, lsum, lp);
init(-1, rsum, rp);
for(int i = 1; i <= n; i++){
sum[i] = lsum[i] + rsum[i] - h[i];
}
auto solve = [&](){
vector<ll> tans = sum;
deque<int> dq;
SegmentTree st;
st.init();
vector<Line> ln(n + 2);
for(int i = 1; i <= n; i++){
while(!dq.empty() && h[dq.back()] <= h[i]){
tans[i] = min(tans[i], tans[dq.back()]);
dq.pob;
}
dq.eb(i);
int t = rp[i];
ln[i] = Line({h[i], tans[i] - t * h[i] - rsum[t] + h[i], i, t - 1});
st.modify(i, ln[i]);
for(int j : qry[i]){
int l = L[j];
rb[j] = *lower_bound(iter(dq), l);
if(rb[j] == i) continue;
/*for(int k = rb[j] + 1; k <= i; k++){
if(ln[k].r < i) continue;
ans[j] = min(ans[j], ln[k](i) - lsum[rb[j]] + (rb[j] - l + 1) * h[rb[j]]);
}*/
ll tmp = st.query(rb[j] + 1, i, i) - lsum[rb[j]] + (rb[j] - l + 1) * h[rb[j]];
ans[j] = min(ans[j], tmp);
}
}
};
solve();
reverse(h.begin() + 1, h.begin() + n + 1);
reverse(sum.begin() + 1, sum.begin() + n + 1);
reverse(lsum.begin() + 1, lsum.begin() + n + 1);
reverse(rsum.begin() + 1, rsum.begin() + n + 1);
lsum.swap(rsum);
rp.swap(lp);
for(int i = 1; i <= n; i++) rp[i] = n - rp[i] + 1;
reverse(rp.begin() + 1, rp.begin() + n + 1);
fill(iter(qry), vector<int>());
L.swap(R);
for(int i = 0; i < q; i++){
L[i] = n - L[i] + 1;
R[i] = n - R[i] + 1;
qry[R[i]].eb(i);
}
lb.swap(rb);
solve();
lsum.swap(rsum);
reverse(h.begin() + 1, h.begin() + n + 1);
reverse(sum.begin() + 1, sum.begin() + n + 1);
reverse(lsum.begin() + 1, lsum.begin() + n + 1);
reverse(rsum.begin() + 1, rsum.begin() + n + 1);
for(int i = 0; i < q; i++){
L[i] = n - L[i] + 1;
R[i] = n - R[i] + 1;
}
L.swap(R);
lb.swap(rb);
for(int i = 0; i < q; i++) lb[i] = n - lb[i] + 1;
fill(iter(qry), vector<int>());
for(int i = 0; i < q; i++) qry[rb[i]].eb(i);
{
vector<int> st;
for(int i = 1; i <= n; i++){
while(!st.empty() && sum[st.back()] >= sum[i]) st.pob;
st.eb(i);
for(int j : qry[i]){
int l = lb[j];
int id = *lower_bound(iter(st), l);
ll tmp = sum[id] - lsum[l] + h[l] * (l - L[j] + 1) - rsum[i] + h[i] * (R[j] - i + 1);
ans[j] = min(ans[j], tmp);
}
}
}
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... |