이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
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 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;
}
inline ll Val(int i) {
return seg[i] + add[i];
}
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);
}
};
const int block = 250;
const int MAX_N = 1e5 + 5;
int lefts[MAX_N], rights[MAX_N];
ll h[MAX_N];
int n;
Seg seg;
void AddL(int i, ll x) {
int v = i;
while (v != -1) {
int nex = lefts[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];
seg.update(v, nex, x * h[v]);
v = nex;
}
}
void Add(int i, ll x) {
AddL(i, x), AddR(i, x);
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);
}
seg = Seg(n);
for (int i = 0; i < n; i++) {
Add(i, 1);
}
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;
}
ans[j] = seg.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;
}
}
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... |