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;
#define ll long long
#define fo(i, n) for(ll i = 0; i<(n); i++)
#define pb push_back
#define F first
#define S second
#define deb(x) cout << #x << " = " << (x) << endl
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
typedef vector<ll> vl;
typedef vector<vl> vvl;
vl v;
ll sufix = 0;
struct Node{
Node *lnode, *rnode;
ll l, r, val = 0, pref = 0, suf = 0;
bool cover = 0;
Node(ll l, ll r) : l(l), r(r){
if(r-l==1) val=pref=suf=cover=v[l];
else{
ll mid =(r+l) / 2;
lnode = new Node(l, mid);
rnode = new Node(mid, r);
val = max(lnode->val, rnode->val);
val = max(val, lnode->suf+rnode->pref);
pref = lnode->pref;
suf = rnode->suf;
cover=lnode->cover&rnode->cover;
if(lnode->cover)pref+=rnode->pref;
if(rnode->cover)suf+=lnode->suf;
}
}
ll query(ll lo, ll hi){
if(r <= lo || hi <= l) return 0;
if(r <= hi && lo <= l){
// deb2(l, r);
ll ans = max(val, sufix+pref);
if(cover)sufix+=suf;
else sufix = suf;
return ans;
}
return max(rnode->query(lo, hi), lnode->query(lo, hi));
}
};
vector<long long> minimum_costs(vector<int> H, vector<int> L, vector<int> R) {
int q = L.size();
ll n = H.size();
vl C;
fo(i, n) v.pb(H[i] == 1);
Node st(0, n+1);
fo(i, q){
sufix = 0;
// deb(i);
// deb(st.query(L[i], R[i]+1));
C.pb((R[i]-L[i]+1)*2-st.query(L[i], R[i]+1));
}
// if(n>3000||q>10){
// }else{
// fo(ind, q){
// ll best = 1e18;
// for(ll meet = L[ind]; meet<=R[ind]; meet++){
// ll hi = H[meet];
// ll current = -hi;
// for(ll i = meet; i>=L[ind]; i--){
// hi = max(hi, (ll)H[i]);
// current+=hi;
// }
// hi = H[meet];
// for(ll i = meet; i<=R[ind]; i++){
// hi = max(hi, (ll)H[i]);
// current+=hi;
// }
// best=min(best, current);
// // deb2(current, meet);
// }
// C.pb(best);
// }
// }
// fo(i, q) deb(C[i]);
return C;
}
# | 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... |