This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second
#define ar array
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)
template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }
const ll INF = 0x3f3f3f3f3f3f3f3f;
template<typename T>
struct Sparse_Table {
int lgflr(int x) {
return 31 - __builtin_clz(x);
}
T f(T x, T y) { // implement !!!
return max(x, y);
}
int N, K;
vector<vector<T>> st;
void init(vector<T> a) {
N = (int) a.size();
K = lgflr(N);
st = vector<vector<T>>(K + 1, vector<T>(N));
swap(st[0], a);
for(int i = 1; i <= K; ++i) {
for(int j = 0; j + (1 << i) <= N; ++j) {
st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
}
T query(int L, int R) {
assert(L >= 0 && R >= 0);
int i = lgflr(R - L + 1);
return f(st[i][L], st[i][R - (1 << i) + 1]);
}
};
vl minimum_costs(vi H, vi L, vi R) {
int N = sz(H);
vpi is;
F0R(i, N) {
if(H[i] == 2) continue;
int j = i;
while(j + 1 < N && H[j + 1] == 1) {
++j;
}
is.eb(i, j);
i = j;
}
vi K(sz(is));
F0R(i, sz(is)) K[i] = is[i].ss - is[i].ff + 1;
Sparse_Table<int> mx;
mx.init(K);
int Q = sz(L);
vl ans(Q);
F0R(q, Q) {
int l = ub(all(is), mp(L[q], 0x3f3f3f3f)) - bg(is) - 1;
if(l >= 0 && l < sz(is) && is[l].ss >= L[q]) ckmax(ans[q], (ll)is[l].ss - L[q] + 1);
++l;
int r = ub(all(is), mp(R[q], 0x3f3f3f3f)) - bg(is) - 1;
if(r >= 0 && r < sz(is) && is[r].ff <= R[q]) ckmax(ans[q], (ll)min(R[q] - is[r].ff + 1, is[r].ss - is[r].ff + 1));
--r;
if(l <= r) ckmax(ans[q], (ll)mx.query(l, r));
ckmin(ans[q], (ll)(R[q] - L[q] + 1));
ans[q] = 2 * (R[q] - L[q] + 1 - ans[q]) + ans[q];
}
return ans;
}
# | 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... |