이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
//#include "meetings.h"
const int maxn = 5050;
ll pre[maxn],suf[maxn];
std::vector<long long> minimum_costs(std::vector<int> H, std::vector<int> L,
std::vector<int> R) {
int Q = L.size();
std::vector<long long> C(Q);
rep(_,0,Q) {
int l = L[_],r = R[_];
pre[l] = H[l];
stack <pii> s;
s.push({mod,l-1});
s.push({H[l],l});
ll sum;
pre[l] = sum = H[l];
rep(i,l+1,r+1) {
while (s.top().fi<=H[i]) {
int val = s.top().fi,pos = s.top().se;
s.pop();
sum -= 1ll*val*(pos - s.top().se);
}
sum += 1ll*H[i]*(i-s.top().se);
s.push({H[i],i});
pre[i] = sum;
}
while (!s.empty()) s.pop();
suf[r] = H[r];
s.push({mod,r+1});
s.push({H[r],r});
suf[r] = sum = H[r];
for (int i = r-1;i>=l;i--) {
while (s.top().fi<=H[i]) {
int val = s.top().fi,pos = s.top().se;
s.pop();
sum -= 1ll*val*(s.top().se - pos);
}
sum += 1ll*H[i]*(s.top().se-i);
s.push({H[i],i});
suf[i] = sum;
}
ll ans = 1e18;
rep(i,l,r+1) ans = min(ans,pre[i] + suf[i] - (ll)H[i]);
C[_] = ans;
}
return C;
}
# | 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... |