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[21];
vector<pair<int,int>> rngs;
vector<pair<int,int>> rng[21];
struct seg_tree{
vector<int> arr;
int seg[400001];
void buld(){
}
void build(int p,int l,int r){
if(l==r){
seg[p] = arr[l];
return ;
}
int md = (l+r)/2;
build(p*2,l,md);
build(p*2+1,md+1,r);
seg[p] = min(seg[p*2],seg[p*2+1]);
}
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 1e18;
int md = (l+r)/2;
return min(query(p*2,l,md,lq,rq),query(p*2+1,md+1,r,lq,rq));
}
}seg[21];
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);
}
if(IT!=rngs.size()&&rngs[IT]==make_pair(l,r))return dp[IT] = ma;
else return ma;
}
int SOLVE2(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 = lower_bound(val[ind].begin(),val[ind].end(),r+1)-val[ind].begin();
it2--;
int ma = 1e18;
if(it2>it){
int it3 = lower_bound(rng[ind-1].begin(),rng[ind-1].end(),make_pair(l,0LL))-rng[ind-1].begin();
int it4 = lower_bound(rng[ind-1].begin(),rng[ind-1].end(),make_pair(r+1,0LL))-rng[ind-1].begin();
it4--;
if(it4>=0&&rng[ind-1][it4].second>r)it4--;
if(it3<=it4){
ma = min(ma,(r-l+1)*ind+seg[ind-1].query(1,0,seg[ind-1].arr.size()-1,it3,it4));
}
}
{//l
ma = min(ma,SOLVE2(l,val[ind][it]-1)+((r-l+1)-(val[ind][it]-l))*ind);
}
{//r
ma = min(ma,SOLVE2(val[ind][it2]+1,r)+((r-l+1)-(r-val[ind][it2]))*ind);
}
if(IT!=rngs.size()&&rngs[IT]==make_pair(l,r))return dp[IT] = ma;
else return 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 = 0;x<=20;x++){
int la = -1;
for(int i = 0;i<n;i++){
if(H[i]<=x){
if(la==-1)la = i;
}else{
if(la!=-1){
rngs.push_back({la,i-1});
rng[x].push_back({la,i-1});
}else{
rngs.push_back({i,i-1});
rng[x].push_back({i,i-1});
}
la = -1;
}
}
if(la!=-1){
rngs.push_back({la,n-1});
rng[x].push_back({la,n-1});
}else{
rngs.push_back({n,n-1});
rng[x].push_back({n,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;
for(int x = 0;x<=20;x++){
for(auto j:rng[x]){
seg[x].arr.push_back(solve(j.first,j.second)-(j.second-j.first+1)*(x+1));
}
if(seg[x].arr.size()){
seg[x].buld();
seg[x].build(1,0,seg[x].arr.size()-1);
}
}
vector<long long> an;
for(int i = 0;i<q;i++){
an.push_back(SOLVE2(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:38:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if(IT!=rngs.size()&&rngs[IT]==make_pair(l,r)&&dp[IT]!=-1)return dp[IT];
| ~~^~~~~~~~~~~~~
meetings.cpp:43: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]
43 | while(it2<val[ind].size()&&val[ind][it2]<=r)it2++;
| ~~~^~~~~~~~~~~~~~~~
meetings.cpp:55:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | if(IT!=rngs.size()&&rngs[IT]==make_pair(l,r))return dp[IT] = ma;
| ~~^~~~~~~~~~~~~
meetings.cpp: In function 'long long int SOLVE2(long long int, long long int)':
meetings.cpp:61:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if(IT!=rngs.size()&&rngs[IT]==make_pair(l,r)&&dp[IT]!=-1)return dp[IT];
| ~~^~~~~~~~~~~~~
meetings.cpp:83:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | if(IT!=rngs.size()&&rngs[IT]==make_pair(l,r))return dp[IT] = ma;
| ~~^~~~~~~~~~~~~
# | 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... |