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 int long long
int arr[100001];
pair<int,int> seg[400001];
int n ,q;
void build(int p,int l,int r){
if(l==r){
seg[p] = {arr[l],l};
return ;
}
int md = (l+r)/2;
build(p*2,l,md);
build(p*2+1,md+1,r);
seg[p] = max(seg[p*2],seg[p*2+1]);
}
pair<int,int> query(int p,int l,int r,int lq,int rq){
if(l>=lq&&r<=rq)return seg[p];
if(r<lq||l>rq)return {0,0};
int md = (l+r)/2;
return max(query(p*2,l,md,lq,rq),query(p*2+1,md+1,r,lq,rq));
}
map<pair<int,int>,int> dp;
vector<int> val[21];
int solve(int l,int r){
if(l>r)return 0;
if(dp.find(make_pair(l,r))!=dp.end())return dp[{l,r}];
int ind = query(1,0,n-1,l,r).second;
int it = lower_bound(val[arr[ind]].begin(),val[arr[ind]].end(),l)-val[arr[ind]].begin();
int it2 = it;
while(it2<val[arr[ind]].size()&&val[arr[ind]][it2]<=r)it2++;
it2--;
int ma = 1e18;
{//l
ma = min(ma,solve(l,val[arr[ind]][it]-1)+((r-l+1)-(val[arr[ind]][it]-l))*arr[ind]);
}
{//r
ma = min(ma,solve(val[arr[ind]][it2]+1,r)+((r-l+1)-(r-val[arr[ind]][it2]))*arr[ind]);
}
for(int i = it;i<it2;i++){
ma = min(ma,solve(val[arr[ind]][i]+1,val[arr[ind]][i+1]-1)+((r-l+1)-(val[arr[ind]][i+1]-val[arr[ind]][i]-1))*arr[ind]);
}
return dp[{l,r}] = ma;
}
vector<long long> minimum_costs(vector<int32_t> H, vector<int32_t> L, vector<int32_t> R){
n = H.size();
q = L.size();
for(int i = 0;i<n;i++){
arr[i] = H[i];
val[arr[i]].push_back(i);
}
build(1,0,n-1);
vector<long long> an;
for(int i = 0;i<q;i++){
an.push_back(solve(L[i],R[i]));
}
return an;
}
Compilation message (stderr)
meetings.cpp: In function 'long long int solve(long long int, long long int)':
meetings.cpp:32:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | while(it2<val[arr[ind]].size()&&val[arr[ind]][it2]<=r)it2++;
| ~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |