이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "meetings.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct line{
int a,b;
}a[750001];
vector <int32_t> ql,qr,ve[750001],v;
vector <int> h,res,ans;
set <int> s;
int n,q,mx[750001][20],lg[750001],ch[750001];
int f(int l, int r){
int d=lg[r-l+1],x=mx[l][d],y=mx[r-(1<<d)+1][d];
return (h[x]<h[y]?y:x);
}
int calc(int i){
int j=*s.lower_bound(i);
return a[j].a*i+a[j].b;
}
int dnc(int l, int r){
if (l>r)
return 0;
if (l==r){
a[l]={h[l],h[l]*(1-l)};
s.insert(l);
for (int i:ve[l])
if (!ch[i]&&qr[i]<=r){
ans[i]+=calc(qr[i]);
ch[i]=1;
}
return 0;
}
int mid=f(l,r);
int x=dnc(l,mid-1),y=dnc(mid+1,r)+h[mid]*(mid-l+1),c=(mid>l?calc(mid-1):0)+x+h[mid];
v.clear();
v.push_back(mid);
s.insert(mid);
int last=mid;
for (auto it=s.upper_bound(mid);it!=s.end()&&*it<=r;it++){
int i=*it;
if (h[mid]*i+c-h[mid]*mid<=a[i].a*i+a[i].b+y){
v.push_back(i);
last=i;
continue;
}
if (h[mid]*i+c-h[mid]*mid>a[i].a*(last+1)+a[i].b+y)
break;
last=(a[i].b+y-c+h[mid]*mid)/(h[mid]-a[i].a);
break;
}
for (int i:v)
s.erase(i);
s.insert(last);
a[last]={h[mid],c-h[mid]*mid-y};
if (mid-l<r-mid+1){
for (int i=l;i<mid;i++)
a[i].b+=x-y;
for (int i:ve[l])
if (!ch[i]&&qr[i]<=r){
ans[i]+=calc(qr[i])+y;
ch[i]=1;
}
return y;
}
for (int i=mid;i<=r;i++)
a[i].b+=y-x;
for (int i:ve[l])
if (!ch[i]&&qr[i]<=r){
ans[i]+=calc(qr[i])+x;
ch[i]=1;
}
return x;
}
void solve(){
s.clear();
for (int j=1;j<20;j++)
for (int i=0;i+(1<<j)-1<n;i++){
int l=mx[i][j-1],r=mx[i+(1<<(j-1))][j-1];
mx[i][j]=(h[l]<h[r]?r:l);
}
for (int i=0;i<n;i++)
ve[i].clear();
for (int i=0;i<q;i++){
ch[i]=0;
int v=f(ql[i],qr[i]);
ans[i]=h[v]*(v-ql[i]+1);
if (v<qr[i])
ve[v+1].push_back(i);
}
dnc(0,n-1);
for (int i=0;i<q;i++)
res[i]=min(res[i],ans[i]);
}
vector <int> minimum_costs(vector <int32_t> H, vector <int32_t> L, vector <int32_t> R){
ql=L,qr=R,n=H.size(),q=L.size();
for (int i:H)
h.push_back(i);
res.assign(q,1e18);
for (int i=2;i<=n;i++)
lg[i]=lg[i/2]+1;
for (int i=0;i<n;i++)
mx[i][0]=i;
ans.assign(q,0);
solve();
reverse(h.begin(),h.end());
for (int i=0;i<q;i++){
ql[i]=n-ql[i]-1;
qr[i]=n-qr[i]-1;
swap(ql[i],qr[i]);
}
solve();
return res;
}
# | 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... |