#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define arr3 array<int, 3>
const ll inf = 1e18;
struct LCT{
struct line{
int k; ll b;
ll operator ()(int x){
return 1LL * k * x + b;
}
};
vector<line> t;
vector<ll> p;
int n;
LCT(int ns){
n = ns;
t.assign(4 * n, {0, inf});
p.resize(4 * n);
}
void apply(int v, ll& x){
t[v].b += x; p[v] += x;
}
void push1(int& v){
if (!p[v]) return;
int vv = 2 * v;
apply(vv, p[v]);
apply(vv + 1, p[v]);
p[v] = 0;
}
void add(int v, int tl, int tr, int& l, int& r, ll& x){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
apply(v, x);
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push1(v);
add(vv, tl, tm, l, r, x);
add(vv + 1, tm + 1, tr, l, r, x);
}
void add(int l, int r, ll x){
if (l > r) return;
add(1, 1, n, l, r, x);
}
void insert(int v, int tl, int tr, line f){
if (tl == tr){
if (t[v](tl) > f(tl)) t[v] = f;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push1(v);
if (t[v].k > f.k) swap(t[v], f);
if (t[v](tm) > f(tm)){
swap(t[v], f);
insert(vv + 1, tm + 1, tr, f);
}
else {
insert(vv, tl, tm, f);
}
}
void chmin(int v, int tl, int tr, int& l, int& r, line& f){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
insert(v, tl, tr, f);
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push1(v);
chmin(vv, tl, tm, l, r, f);
chmin(vv + 1, tm + 1, tr, l, r, f);
}
void chmin(int l, int r, int k, ll b){
if (l > r) return;
line f = {k, b};
chmin(1, 1, n, l, r, f);
}
ll get(int v, int tl, int tr, int& x){
if (tl == tr) return t[v](x);
int tm = (tl + tr) / 2, vv = 2 * v;
push1(v);
if (x <= tm){
return min(t[v](x), get(vv, tl, tm, x));
}
return min(t[v](x), get(vv + 1, tm + 1, tr, x));
}
ll get(int x){
return get(1, 1, n, x);
}
};
vector<ll> minimum_costs(vector<int> H, vector<int> L, vector<int> R){
int n = (int) H.size(), q = (int) L.size();
vector<int> h(n + 1);
for (int i = 1; i <= n; i++){
h[i] = H[i - 1];
}
L.insert(L.begin(), 0); R.insert(R.begin(), 0);
vector<int> log(n + 1);
for (int i = 2; i <= n; i++) log[i] = log[i / 2] + 1;
const int lg = log[n];
vector<vector<pii>> sp(n + 1, vector<pii>(lg + 1));
for (int i = 1; i <= n; i++) sp[i][0] = {h[i], i};
for (int j = 1; j <= lg; j++){
for (int i = 1; i + (1 << j) <= n + 1; i++){
sp[i][j] = max(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
auto get = [&](int l, int r){
int k = log[r - l + 1];
return max(sp[l][k], sp[r - (1 << k) + 1][k]);
};
vector<int> g[n + 1], nd(n + 1);
int cc = 0;
function<int(int, int)> build1 = [&](int l, int r){
int p = get(l, r).ss, x = -1, y = -1;
if (p != l) x = build1(l, p - 1);
if (p != r) y = build1(p + 1, r);
nd[p] = ++cc;
if (x != -1) g[cc].pb(x);
if (y != -1) g[cc].pb(y);
return cc;
};
build1(1, n);
vector<vector<int>> pw(n + 1, vector<int>(lg + 1));
vector<int> tin(n + 1), tout(n + 1);
int timer = 0;
function<void(int, int)> fill = [&](int v, int pr){
tin[v] = ++timer;
pw[v][0] = pr;
for (int i = 1; i <= lg; i++){
pw[v][i] = pw[pw[v][i - 1]][i - 1];
}
for (int i: g[v]){
fill(i, v);
}
tout[v] = timer;
};
fill(n, n);
auto check = [&](int x, int y){
return (tin[x] <= tin[y] && tout[x] >= tout[y]);
};
auto lca = [&](int x, int y){
if (check(x, y)) return x;
if (check(y, x)) return y;
for (int i = lg; i >= 0; i--){
if (!check(pw[x][i], y)){
x = pw[x][i];
}
}
return pw[x][0];
};
vector<int> qs[n + 1];
for (int i = 1; i <= q; i++){
L[i]++; R[i]++;
qs[lca(nd[L[i]], nd[R[i]])].pb(i);
}
vector<ll> out(q + 1, inf); cc = 0;
LCT T1(n), T2(n);
for (int i = 1; i <= n; i++){
T1.add(i, i, -inf);
T2.add(i, i, -inf);
}
function<void(int, int)> build = [&](int l, int r){
auto [m, p] = get(l, r);
if (l != p) build(l, p - 1);
if (r != p) build(p + 1, r);
for (int i: qs[++cc]){
out[i] = min(out[i], T2.get(L[i]) + 1LL * m * (R[i] - p + 1));
out[i] = min(out[i], T1.get(R[i]) + 1LL * m * (p - L[i] + 1));
}
// T2[i]' = min(T2[i] + m * (r - p + 1), T2[p + 1] + m * (p - i + 1)); (l <= i < p)
T2.add(l, p, 1LL * m * (r - p + 1));
if (r != p) T2.chmin(l, p, -m, T2.get(p + 1) + 1LL * m * (p + 1));
// T1[i]' = min(T1[i] + m * (p - l + 1), T1[p - 1] + m * (i - p + 1)); (p < i <= r)
T1.add(p, r, 1LL * m * (p - l + 1));
if (l != p) T1.chmin(p, r, m, T1.get(p - 1) - 1LL * m * (p - 1));
};
build(1, n);
vector<ll> ret;
for (int i = 1; i <= q; i++) ret.pb(out[i]);
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1884 KB |
Output is correct |
3 |
Correct |
4 ms |
1884 KB |
Output is correct |
4 |
Correct |
3 ms |
1912 KB |
Output is correct |
5 |
Correct |
3 ms |
1884 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
5 ms |
1884 KB |
Output is correct |
8 |
Correct |
3 ms |
2140 KB |
Output is correct |
9 |
Correct |
3 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1884 KB |
Output is correct |
3 |
Correct |
4 ms |
1884 KB |
Output is correct |
4 |
Correct |
3 ms |
1912 KB |
Output is correct |
5 |
Correct |
3 ms |
1884 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
5 ms |
1884 KB |
Output is correct |
8 |
Correct |
3 ms |
2140 KB |
Output is correct |
9 |
Correct |
3 ms |
2140 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
8 ms |
3164 KB |
Output is correct |
12 |
Correct |
7 ms |
3164 KB |
Output is correct |
13 |
Correct |
8 ms |
3164 KB |
Output is correct |
14 |
Correct |
9 ms |
3676 KB |
Output is correct |
15 |
Correct |
7 ms |
3164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
34 ms |
7764 KB |
Output is correct |
3 |
Correct |
233 ms |
70552 KB |
Output is correct |
4 |
Correct |
183 ms |
63044 KB |
Output is correct |
5 |
Correct |
197 ms |
72460 KB |
Output is correct |
6 |
Correct |
184 ms |
73792 KB |
Output is correct |
7 |
Correct |
187 ms |
75448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
34 ms |
7764 KB |
Output is correct |
3 |
Correct |
233 ms |
70552 KB |
Output is correct |
4 |
Correct |
183 ms |
63044 KB |
Output is correct |
5 |
Correct |
197 ms |
72460 KB |
Output is correct |
6 |
Correct |
184 ms |
73792 KB |
Output is correct |
7 |
Correct |
187 ms |
75448 KB |
Output is correct |
8 |
Correct |
204 ms |
62580 KB |
Output is correct |
9 |
Correct |
166 ms |
62528 KB |
Output is correct |
10 |
Correct |
184 ms |
62528 KB |
Output is correct |
11 |
Correct |
184 ms |
61764 KB |
Output is correct |
12 |
Correct |
169 ms |
61764 KB |
Output is correct |
13 |
Correct |
195 ms |
62016 KB |
Output is correct |
14 |
Correct |
240 ms |
69700 KB |
Output is correct |
15 |
Correct |
178 ms |
61248 KB |
Output is correct |
16 |
Correct |
180 ms |
71488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1884 KB |
Output is correct |
3 |
Correct |
4 ms |
1884 KB |
Output is correct |
4 |
Correct |
3 ms |
1912 KB |
Output is correct |
5 |
Correct |
3 ms |
1884 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
5 ms |
1884 KB |
Output is correct |
8 |
Correct |
3 ms |
2140 KB |
Output is correct |
9 |
Correct |
3 ms |
2140 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
8 ms |
3164 KB |
Output is correct |
12 |
Correct |
7 ms |
3164 KB |
Output is correct |
13 |
Correct |
8 ms |
3164 KB |
Output is correct |
14 |
Correct |
9 ms |
3676 KB |
Output is correct |
15 |
Correct |
7 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
34 ms |
7764 KB |
Output is correct |
18 |
Correct |
233 ms |
70552 KB |
Output is correct |
19 |
Correct |
183 ms |
63044 KB |
Output is correct |
20 |
Correct |
197 ms |
72460 KB |
Output is correct |
21 |
Correct |
184 ms |
73792 KB |
Output is correct |
22 |
Correct |
187 ms |
75448 KB |
Output is correct |
23 |
Correct |
204 ms |
62580 KB |
Output is correct |
24 |
Correct |
166 ms |
62528 KB |
Output is correct |
25 |
Correct |
184 ms |
62528 KB |
Output is correct |
26 |
Correct |
184 ms |
61764 KB |
Output is correct |
27 |
Correct |
169 ms |
61764 KB |
Output is correct |
28 |
Correct |
195 ms |
62016 KB |
Output is correct |
29 |
Correct |
240 ms |
69700 KB |
Output is correct |
30 |
Correct |
178 ms |
61248 KB |
Output is correct |
31 |
Correct |
180 ms |
71488 KB |
Output is correct |
32 |
Correct |
1865 ms |
499872 KB |
Output is correct |
33 |
Correct |
1412 ms |
498600 KB |
Output is correct |
34 |
Correct |
1706 ms |
500836 KB |
Output is correct |
35 |
Correct |
2137 ms |
500608 KB |
Output is correct |
36 |
Correct |
1435 ms |
499556 KB |
Output is correct |
37 |
Correct |
1936 ms |
501268 KB |
Output is correct |
38 |
Correct |
3198 ms |
560288 KB |
Output is correct |
39 |
Correct |
3439 ms |
560328 KB |
Output is correct |
40 |
Correct |
2174 ms |
513716 KB |
Output is correct |
41 |
Correct |
1791 ms |
633444 KB |
Output is correct |
42 |
Correct |
1997 ms |
634516 KB |
Output is correct |
43 |
Correct |
1958 ms |
634396 KB |
Output is correct |
44 |
Correct |
3646 ms |
560072 KB |
Output is correct |