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>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef int ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const ll infinity = 1e9;
const int MAX_N = 1e5 + 5;
int seg[4 * MAX_N], add[4 * MAX_N];
//struct Seg {
int n;
// vi seg, add;
// Seg () {}
// Seg (int m) {
// for (n = 1; n < m; n <<= 1);
// seg.resize(2 * n), add.resize(2 * n);
// }
inline ll Val(int i) {
return seg[i] + add[i];
}
inline void pull(int i) {
seg[i] = min(Val(i << 1), Val(i << 1 | 1));
}
inline void push(int i) {
add[i << 1] += add[i];
add[i << 1 | 1] += add[i];
seg[i] += add[i];
add[i] = 0;
}
void update(int a, int b, ll x, int ind, int l, int r) {
if (a <= l && r <= b) {
add[ind] += x;
return;
}
if (r <= a || b <= l) return;
push(ind);
update(a, b, x, ind << 1, l, (l + r) >> 1);
update(a, b, x, ind << 1 | 1,(l + r) >> 1, r);
pull(ind);
}
inline void update(int a, int b, ll x) {
update(a, b, x, 1, 0, n);
}
ll query(int a, int b, int ind, int l, int r) {
if (a <= l && r <= b) return Val(ind);
if (r <= a || b <= l) return infinity;
push(ind);
return min(query(a, b, ind << 1, l, (l + r) >> 1),
query(a, b, ind << 1 | 1,(l + r) >> 1, r));
}
inline ll query(int a, int b) {
return query(a, b, 1, 0, n);
}
//};
int lefts[MAX_N], rights[MAX_N];
ll h[MAX_N];
//int n;
//Seg seg;
vector<pair<pii, int>> ranges;
void AddL(int i, ll x) {
int v = i;
while (v != -1) {
int nex = lefts[v];
ranges.push_back({{nex + 1, v + 1}, x * h[v]});
// seg.update(nex + 1, v + 1, x * h[v]);
v = nex;
}
}
void AddR(int i, ll x) {
int v = i;
while (v != n) {
int nex = rights[v];
ranges.push_back({{v, nex}, x * h[v]});
// seg.update(v, nex, x * h[v]);
v = nex;
}
}
void Update() {
sort(all(ranges));
int m = ranges.size();
for (int i = 0; i < m;) {
int j = i;
ll s = 0;
while (j < m && ranges[i].x == ranges[j].x) {
s += ranges[j++].y;
}
update(ranges[i].x.x, ranges[i].x.y, s);
i = j;
}
ranges.clear();
}
void Add(int i, ll x) {
AddL(i, x), AddR(i, x);
ranges.push_back({{i, i + 1}, -x * h[i]});
// seg.update(i, i + 1, -x * h[i]);
}
vector<long long> minimum_costs(vector<int> H, vector<int> L, vector<int> R) {
int q = L.size();
n = H.size();
int max_h = 0;
for (int i = 0; i < n; i++) {
h[i] = H[i];
chkmax(max_h, H[i]);
}
vector<long long> ans(q);
stack<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && h[stk.top()] <= h[i]) stk.pop();
lefts[i] = stk.empty() ? -1 : stk.top();
stk.push(i);
}
while (!stk.empty()) stk.pop();
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && h[stk.top()] <= h[i]) stk.pop();
rights[i] = stk.empty() ? n : stk.top();
stk.push(i);
}
for (int i = 0; i < n; i++) {
Add(i, 1);
}
Update();
for (int j = 0; j < q; j++) {
int l = L[j], r = R[j];
int v = l;
while (v != -1) {
int nx = lefts[v];
AddR(v, -(v - nx - (v == l)));
v = nx;
}
v = r;
while (v != n) {
int nx = rights[v];
AddL(v, -(nx - v - (v == r)));
v = nx;
}
Update();
ans[j] = query(l, r + 1);
v = l;
while (v != -1) {
int nx = lefts[v];
AddR(v, v - nx - (v == l));
v = nx;
}
v = r;
while (v != n) {
int nx = rights[v];
AddL(v, nx - v - (v == r));
v = nx;
}
Update();
}
return ans;
}
//10 3
//1 2 2 1 1 1 2 1 1 2
//2 5
//3 7
//4 9
# | 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... |