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 "meetings.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using ar2 = array<int,2>;
const int INF = (int)1e9;
const ll LINF = (ll)2e18;
const int mxN = (int)2e5+10;
int n, q;
vi a;
struct Data{
ll pref, suf, ans;
int sz;
Data(){};
Data(int v){ pref=suf=ans=(v==1),sz=1; }
} Bad;
Data seg[mxN*2];
Data comb(Data a, Data b){
Data c; c.sz = a.sz+b.sz;
if(!b.sz) return a;
if(!a.sz) return b;
c.pref = a.pref; c.suf = b.suf;
if(a.pref==a.sz) c.pref+=b.pref;
if(b.suf==b.sz) c.suf+=a.suf;
c.ans = max({a.ans,b.ans,a.suf+b.pref});
return c;
}
void build(int p=0, int l=0, int r=n-1){
if(l==r){ seg[p] = Data(a[l]); return; }
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
build(p+1,l,mid); build(rp,mid+1,r);
seg[p] = comb(seg[p+1],seg[rp]);
}
Data query(int i, int j, int p=0, int l=0, int r=n-1){
if(i>r or j<l or i>j) return Bad;
if(i<=l and r<=j) return seg[p];
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
return comb(query(i,j,p+1,l,mid),query(i,j,rp,mid+1,r));
}
vll minimum_costs(vi A, vi L, vi R) {
n = sz(A); q = sz(L); a = A;
Bad.ans=Bad.pref=Bad.suf=-LINF; Bad.sz=0;
vll ans(q,LINF); build();
for(int i = 0; i < q; i++){
int l = L[i], r = R[i];
ans[i] = (r-l+1)*2-query(l,r).ans;
}
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... |