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 maxn = (1 << 20) + 4;
const int maxs = 5000 + 4;
struct node {
	int num, res, pre, suf;
};
int n, q;
ll A[maxn]; pii Q[maxn];
int mx[maxs][maxs]; ll dp[maxs][maxs];
vector<ll> res; node t[2 * maxn], ca;
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];
	}
}
node f(node a, node b) {
	node ans;
	ans.num = a.num + b.num;
	ans.res = max({a.res, b.res, a.suf + b.pre});
	if (a.pre == a.num) ans.pre = a.pre + b.pre;
	else ans.pre = a.pre;
	if (b.suf == b.num) ans.suf = b.suf + a.suf;
	else ans.suf = b.suf;
	return ans;
}
void build(int v, int tl, int tr) {
	if (tr - tl == 1) {
		t[v].num = 1;
		t[v].res = t[v].pre = t[v].suf = (A[tl] == 1);
		return ;
	}
	int mid = (tl + tr) / 2;
	build(2 * v + 1, tl, mid); build(2 * v + 2, mid, tr);
	t[v] = f(t[2 * v + 1], t[2 * v + 2]);
}
node get_res(int v, int tl, int tr, int l, int r) {
	l = max(l, tl); r = min(r, tr);
	if (l >= tr || r <= tl) return ca;
	if (l == tl && r == tr) return t[v];
	int mid = (tl + tr) / 2;
	return f(get_res(2 * v + 1, tl, mid, l, r), get_res(2 * v + 2, mid, tr, l, r));
}
void solve2() {
	build(0, 0, n);
	for (int i = 0; i < q; i++) {
		int l = Q[i].F, r = Q[i].S;
		res[i] = 2 * (r - l) - get_res(0, 0, n, l, r).res;
	}
}
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);
	if (n < maxs) solve1();
	else 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... |