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 INFLL = 1e18;
const int INF = 1e9;
const int MAXN = 1e5;
const int MAXH = 21;
struct Node {
int pref[MAXH], suff[MAXH], cost[MAXH][MAXH], maxH;
Node() {
forn(i,MAXH) {
pref[i]=suff[i]=INF;
forn(j,MAXH) cost[i][j]=INF;
}
maxH=0;
}
Node(int val) {
cost[val][val] = maxH = val;
forn(i,MAXH) pref[i] = suff[i] = max(i,val);
}
Node(Node left, Node right) {
maxH = max(left.maxH, right.maxH);
forn(i,MAXH) {
int &curr = cost[i][maxH];
curr = min(curr, left.cost[i][left.maxH] + right.pref[left.maxH]);
}
forn(i,MAXH) {
int &curr = cost[left.maxH][max(i,right.maxH)];
curr = min(curr, left.cost[left.maxH][i] + right.pref[i]);
}
forn(i,MAXH) {
int &curr = cost[max(i,left.maxH)][right.maxH];
curr = min(curr, left.suff[i] + right.cost[i][right.maxH]);
}
forn(i,MAXH) {
int &curr = cost[maxH][i];
curr = min(curr, left.suff[right.maxH] + right.cost[right.maxH][i]);
}
forn(i,MAXH) {
pref[i] = min(pref[i], left.pref[i] + right.pref[max(i,left.maxH)]);
suff[i] = min(suff[i], left.suff[max(i,right.maxH)] + right.suff[i]);
}
}
};
Node st[2*MAXN];
int n;
ll 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);
}
Node ans=Node(ansL, ansR);
ll res=INFLL;
forn(i,MAXH) res=min({res,(ll)ans.cost[ans.maxH][i],(ll)ans.cost[i][ans.maxH]});
return res;
}
vll subtask4(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) ans[i]=query(l[i],r[i]+1);
return ans;
}
vll minimum_costs(vi h, vi l, vi r) {
int q=SIZE(l), n=SIZE(h);
bool s4=q<=MAXN&&n<=MAXN;
forn(i,n) s4&=h[i]<MAXH;
if(s4) return subtask4(h,l,r);
vll ans(q,INFLL);
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... |