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 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 int INF = 1e9;
const int MAXH = 21;
struct Node {
int pref[MAXH], suff[MAXH];
int cost[MAXH][MAXH];
int maxH;
Node() {
forn(i,MAXH) {
pref[i]=suff[i]=INF;
forn(j,MAXH) cost[i][j]=INF;
}
maxH=0;
}
};
Node init(int v) {
Node u;
u.cost[v][v]=u.maxH=v;
forn(i,MAXH) u.pref[i]=u.suff[i]=max(i,v);
return u;
}
void mini(int &curr, int v) {
curr=min(curr,v);
}
Node op(Node left, Node right) {
Node u;
forn(i,MAXH) {
int j=left.maxH;
mini(u.cost[i][max(j,right.maxH)], left.cost[i][j] + right.pref[j]);
}
forn(i,MAXH) {
int j=right.maxH;
mini(u.cost[max(i,left.maxH)][j], left.suff[i] + right.cost[i][j]);
}
forn(j,MAXH) {
int i=left.maxH;
mini(u.cost[i][max(j,right.maxH)], left.cost[i][j] + right.pref[j]);
}
forn(j,MAXH) {
int i=right.maxH;
mini(u.cost[max(i,left.maxH)][j], left.suff[i] + right.cost[i][j]);
}
forn(i,MAXH) {
mini(u.suff[i], left.suff[max(i,right.maxH)] + right.suff[i]);
mini(u.pref[i], left.pref[i] + right.pref[max(i,left.maxH)]);
}
u.maxH = max(left.maxH, right.maxH);
return u;
}
const int SZ = 1<<17;
Node st[2*SZ];
int query(int l, int r) {
Node ansL, ansR;
for(l+=SZ, r+=SZ; l<r; l/=2, r/=2) {
if(l&1) ansL = op(ansL,st[l++]);
if(r&1) ansR = op(st[--r],ansR);
}
Node res=op(ansL,ansR);
int ans=INF;
forn(i,MAXH) {
int j=res.maxH;
mini(ans,res.cost[i][j]);
mini(ans,res.cost[j][i]);
}
return ans;
}
vll minimum_costs(vi h, vi l, vi r) {
int n=SIZE(h);
forn(i,n) st[i+SZ]=init(h[i]);
dforn(i,SZ) st[i]=op(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;
}
# | 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... |