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];
int logg[100001];
int table[100001][17];
int n ,q;
map<int,vector<int>> val;
map<pair<int,int>,int> dp;
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 sz = (r-l+1);
int ind = max(table[l][logg[sz]],table[r-(1<<logg[sz])+1][logg[sz]]);
int it = lower_bound(val[ind].begin(),val[ind].end(),l)-val[ind].begin();
int it2 = it;
while(it2<val[ind].size()&&val[ind][it2]<=r)it2++;
it2--;
int ma = 1e18;
{//l
ma = min(ma,solve(l,val[ind][it]-1)+((r-l+1)-(val[ind][it]-l))*ind);
}
{//r
ma = min(ma,solve(val[ind][it2]+1,r)+((r-l+1)-(r-val[ind][it2]))*ind);
}
for(int i = it;i<it2;i++){
ma = min(ma,solve(val[ind][i]+1,val[ind][i+1]-1)+((r-l+1)-(val[ind][i+1]-val[ind][i]-1))*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++){
table[i][0] = H[i];
arr[i] = H[i];
val[arr[i]].push_back(i);
}
for(int j = 1;j<17;j++){
for(int i = 0;i<n-(1<<j)+1;i++){
table[i][j] = max(table[i][j-1],table[i+(1<<(j-1))][j-1]);
}
}
logg[1] = 0;
for(int i = 2;i<=n;i++)logg[i] = logg[i/2]+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:18: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]
18 | while(it2<val[ind].size()&&val[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... |