#include <bits/stdc++.h>
#define ll long long
#define rep(i, m, n) for (int i = (m); i <= (n); i++)
#define REB(i, m, n) for (int i = (m); i >= (n); i--)
#define iter(v, id) for (auto v : id)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
using namespace std;
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
const int N = 1e6 + 7;
const int Mod = 1e9 + 7;
const ll INF = 1e18 + 7;
const int BASE = 137;
int n, Q;
int a[N];
pii qr[N];
namespace sub2 {
int suf[N], pre[N];
ll ssuf[N], spre[N];
vector<ll> solution() {
vector<ll> Ans(Q + 3, INF);
rep (i, 1, n) {
suf[i] = pre[i] = ssuf[i] = spre[i] = a[i];
REB (j, i - 1, 1) suf[j] = max(suf[j + 1], a[j]), ssuf[j] = ssuf[j + 1] + suf[j];
rep (j, i + 1, n) pre[j] = max(pre[j - 1], a[j]), spre[j] = spre[j - 1] + pre[j];
rep (q, 1, Q) {
if (qr[q].fs <= i && i <= qr[q].se) Ans[q] = min(Ans[q], ssuf[qr[q].fs] + spre[qr[q].se] - a[i]);
}
}
// rep (i, 1, Q) {
// cout << Ans[i] <<"\n";
// }
return vector<ll> (Ans.begin() + 1, Ans.begin() + 1 + Q);
}
}
namespace sub3 {
bool check () {
rep (i, 1, n) if (a[i] > 2) return 0;
return 1;
}
int L2[N], R2[N];
int spt[N][25], lg[N];
vector<ll> solution() {///ko long long dc
rep (i, 1, n) {
L2[i] = a[i] == 2 ? i : L2[i - 1];
}
R2[n + 1] = n + 1;
REB (i, n, 1) {
R2[i] = a[i] == 2 ? i : R2[i + 1];
}
rep (i, 1, n) {
spt[i][0] = max(0, R2[i] - L2[i] - 1);
lg[i] = log2(i);
}
for (int j = 1; (1 << j) <= n; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
spt[i][j] = max(spt[i][j - 1], spt[i + (1 << j - 1)][j - 1]);
}
}
auto get = [&] (int L, int R) {
if (L > R) return 0;
int K = lg[R - L + 1];
return max(spt[L][K], spt[R - (1 << K) + 1][K]);
};
vector<ll> Ans(Q + 3, 0);
rep (q, 1, Q) {
int L = qr[q].fs, R = qr[q].se;
Ans[q] = min({get(R2[L], L2[R]) + (R - L + 1 - get(R2[L], L2[R])) * 2,
R - max(L - 1, L2[R]) + (R - L + 1 - (R - max(L - 1, L2[R]))) * 2,
min(R2[L], R + 1) - L + (R - L + 1 - (min(R2[L], R + 1) - L)) * 2});
// cout << Ans[q] <<"\n";
}
return vector<ll> (Ans.begin() + 1, Ans.begin() + 1 + Q);
}
}
vector<ll> minimum_costs (vector<int> H, vector<int> L, vector<int> R) {
n = SZ(H);
Q = SZ(L);
rep (i, 1, n) a[i] = H[i - 1];
rep (i, 1, Q) qr[i].fs = L[i - 1] + 1, qr[i].se = R[i - 1] + 1;
if (n <= 5000 && Q <= 5000) return sub2 :: solution();
else if (sub3 :: check()) return sub3 :: solution();
}
void process() {
cin >> n >> Q;
rep (i, 1, n) cin >> a[i];
rep (i, 1, Q) {
cin >> qr[i].fs >> qr[i].se;
}
if (n <= 5000 && Q <= 5000) sub2 :: solution();
else if (sub3 :: check()) sub3 :: solution();
}
//#define file(name) if(fopen(name".inp","r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
//main () {
//// file("c");
// ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int num_Test = 1;
//// cin >> num_Test;
// while (num_Test--) {
// process();
// }
//}
/*
onepunch +28
*/
Compilation message (stderr)
meetings.cpp: In function 'std::vector<long long int> minimum_costs(std::vector<int>, std::vector<int>, std::vector<int>)':
meetings.cpp:103:1: warning: control reaches end of non-void function [-Wreturn-type]
103 | }
| ^
# | 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... |