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 SIZE(x) int(x.size())
#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define dforn(i,n) for(int i=int(n)-1;i>=0;i--)
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
const ll INF = 1e18;
const int MAXN = 1e5;
struct Node {
int pref, suff, maxi, sz;
Node() : pref(0), suff(0), maxi(0), sz(0) {}
Node(int v) : pref(v==1), suff(v==1), maxi(v==1), sz(1) {}
Node(Node left, Node right) {
pref = left.pref + right.pref * (left.pref == left.sz);
suff = right.suff + left.suff * (right.suff == right.sz);
maxi = max({left.maxi, right.maxi, left.suff + right.pref});
sz = left.sz + right.sz;
}
};
Node st[2*MAXN];
int n;
int query(int l, int r) {
Node ansL, ansR;
for(l+=n, r+=n; l<r; l/=2, r/=2) {
if(l&1) ansL=Node(ansL,st[l++]);
if(r&1) ansR=Node(st[--r],ansR);
}
return Node(ansL, ansR).maxi;
}
vll subtask3(vi h, vi l, vi r) {
n=SIZE(h);
forn(i,n) st[i+n]=Node(h[i]);
dforn(i,n) st[i]=Node(st[2*i],st[2*i+1]);
int q=SIZE(l);
vll ans(q);
forn(i,q) {
int maxi=query(l[i],r[i]+1);
ans[i]=(r[i]-l[i]+1LL)*2LL-maxi;
}
return ans;
}
vll minimum_costs(vi h, vi l, vi r) {
int q=SIZE(l), n=SIZE(h);
bool s3=q<=MAXN&&n<=MAXN;
forn(i,n) s3&=h[i]<=2;
if(s3) return subtask3(h,l,r);
vll ans(q,INF);
forn(i,n) {
vi c(n);
c[i]=h[i];
dforn(j,i) c[j]=max(c[j+1],h[j]);
forsn(j,i+1,n) c[j]=max(c[j-1],h[j]);
vll p(n+1);
p[0]=0;
forn(j,n) p[j+1]=p[j]+c[j];
forn(j,q) ans[j]=min(ans[j],p[r[j]+1]-p[l[j]]);
}
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... |