# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1054910 | Ahmed57 | Meetings (IOI18_meetings) | C++17 | 0 ms | 0 KiB |
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;
int dp[2000001];
vector<int> val[3];
vector<pair<int,int>> rngs;
int solve(int l,int r){
if(l>r)return 0;
int IT = lower_bound(rngs.begin(),rngs.end(),make_pair(l,r))-rngs.begin();
if(IT!=rngs.size()&&rngs[IT]==make_pair(l,r)&&dp[IT]!=-1)return dp[IT];
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 x = 1;x<=20;x++){
int la = -1;
for(int i = 0;i<n;i++){
if(arr[i]<=x){
if(la==-1)la = i;
}else{
if(la!=-1)rngs.push_back({la,i-1});
la = -1;
}
}
if(la!=-1)rngs.push_back({la,n-1});
}
sort(rngs.begin(),rngs.end());
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]);
}
}
memset(dp,-1,sizeof dp);
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;
}