This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
#include "meetings.h"
using namespace std;
typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;
#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxlg = 20;
const int maxn = (1 << maxlg) + 4;
const int maxs = 5000 + 4;
struct node {
	int num, res, pre, suf;
};
int n, q; ll Mx;
ll A[maxn]; pii Q[maxn];
int mx[maxs][maxs]; ll dp[maxs][maxs];
vector<ll> res; pll t[2 * maxn], ca;
int M[maxn]; pii Sg[maxlg][maxn];
vector<pair<pii, int>> st;
void solve1() {
	for (int ln = 1; ln <= n; ln++) {
		for (int l = 0, r = ln; r <= n; l++, r++) {
			if (ln == 1) mx[l][r] = l;
			else {
				if (A[l] >= A[mx[l + 1][r]]) mx[l][r] = l;
				else mx[l][r] = mx[l + 1][r];
			}
		}
	}
	
	for (int ln = 0; ln <= n; ln++) {
		for (int l = 0, r = ln; r <= n; l++, r++) {
			if (ln == 0) dp[l][r] = 0;
			else {
				int j = mx[l][r];
				dp[l][r] = min(dp[l][j] + (r - j) * A[j], dp[j + 1][r] + (j + 1 - l) * A[j]);
			}
		}
	}
	for (int i = 0; i < q; i++) {
		int l = Q[i].F, r = Q[i].S;
		res[i] = dp[l][r];
	}
}
void build(int v, int tl, int tr) {
	t[v].S = 0;
	if (tr - tl == 1) {
		t[v].F = 0;
		return ;
	}
	int mid = (tl + tr) / 2;
	build(2 * v + 1, tl, mid); build(2 * v + 2, mid, tr);
	t[v].F = max(t[2 * v + 1].F, t[2 * v + 2].F) + t[v].S;
}
void add_val(int v, int tl, int tr, int l, int r, ll x) {
	if (v == 0) st.pb(Mp(Mp(l, r), x));
	l = max(l, tl); r = min(r, tr);
	if (l >= tr || r <= tl) return ;
	if (l == tl && r == tr) {
		t[v].F += x; t[v].S += x;
		return ;
	}
	int mid = (tl + tr) / 2;
	add_val(2 * v + 1, tl, mid, l, r, x); add_val(2 * v + 2, mid, tr, l, r, x);
	t[v].F = max(t[2 * v + 1].F, t[2 * v + 2].F) + t[v].S;
}
ll get_max(int v, int tl, int tr, int l, int r) {
	l = max(l, tl); r = min(r, tr);
	if (l >= tr || r <= tl) return 0;
	if (l == tl && r == tr) return t[v].F;
	int mid = (tl + tr) / 2;
	return max(get_max(2 * v + 1, tl, mid, l, r), get_max(2 * v + 2, mid, tr, l, r)) + t[v].S;
}
void solve2() {
	build(0, 0, n);
	for (int R = 1; R <= Mx; R++) {
		for (int i = 0; i < n; i++) M[i] = (A[i] <= R);
		
		for (int i = 0; i < n; i++) {
			if (!M[i]) Sg[R][i].F = -1;
			else if (i - 1 >= 0 && M[i - 1]) Sg[R][i].F = Sg[R][i - 1].F;
			else Sg[R][i].F = i;
		}
		for (int i = n - 1; i >= 0; i--) {
			if (!M[i]) Sg[R][i].S = -1;
			else if (i + 1 < n && M[i + 1]) Sg[R][i].S = Sg[R][i + 1].S;
			else Sg[R][i].S = i + 1;
		}
		
		for (int i = 0; i < n; i++) add_val(0, 0, n, i, i + 1, Sg[R][i].S - Sg[R][i].F);
	}
	
	for (int i = 0; i < q; i++) {
		int l = Q[i].F, r = Q[i].S;
		
		st.clear();
		vector<int> vc = {l, r - 1};
		vc.resize(unique(all(vc)) - vc.begin());
		for (int R = 1; R <= Mx; R++) {
			for (int e = 0; e < len(vc); e++) {
				int j = vc[e];
				int lx = Sg[R][j].F, rx = Sg[R][j].S;
				if (lx == -1) continue;
				if (lx <= l && rx >= r && e != 0) continue;
				
				int lp = max(lx, l), rp = min(rx, r);
				add_val(0, 0, n, lp, rp, (rp - lp) - (rx - lx));
			}
		}
		
		res[i] = 1ll * (r - l) * (Mx + 1) - get_max(0, 0, n, l, r);
		for (auto f : st) {
			int lx = f.F.F, rx = f.F.S, x = f.S;
			add_val(0, 0, n, lx, rx, -x);
		}
	}
}
vector<ll> minimum_costs(vector<int> H, vector<int> L, vector<int> R) {
	n = len(H); q = len(L);
	for (int i = 0; i < n; i++) A[i] = H[i];
	for (int i = 0; i < q; i++) Q[i] = Mp(L[i], R[i] + 1);
	
	res.resize(q); Mx = (*max_element(A, A + n)) - 1;
	if (n < maxs) solve1();
	else if (Mx < maxlg) solve2();
	
	return res;
}
| # | 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... |