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 "towers.h"
#include <bits/stdc++.h>
using namespace std;
using mim = pair<pair<int,int>,int>;
const int maxn=1e5+10;
const int logg=18;
int n;
int h[maxn];
multiset<int> res;
mim sparse[logg][maxn];
int klk;
int suff[maxn];
int uniq[maxn];
mim comb(mim &a,mim &b){
mim ret=a;
ret.first=max(ret.first,b.first);
ret.second=min(ret.second,b.second);
return ret;
}
mim qry(int l,int r){
mim ret={{h[l],l},h[l]};
for(int i=logg-1;i>=0;i--){
if(r-(1<<i)+1>=l){
ret=comb(ret,sparse[i][r]);
r-=(1<<i);
}
}
return ret;
}
void resi(int l,int r,multiset<int> &ret,int naj=2e9+10){
if(r<l) return;
auto temp=qry(l,r);
pair<int,int> maxi=temp.first;
int mini=temp.second;
multiset<int> levi; multiset<int> desni;
resi(l,maxi.second-1,levi,maxi.first);
resi(maxi.second+1,r,desni,maxi.first);
if(levi.size()>desni.size()) swap(levi,desni);
for(int i:desni) levi.emplace(i);
if(!levi.empty()){
auto najv=--levi.end();
int d=naj-mini;
if(*najv<d){
levi.erase(najv);
levi.emplace(d);
}
}else{
levi.emplace(naj-mini);
}
swap(ret,levi);
}
void init(int _n, vector<int> _h) {
n=_n;
for(int i=0;i<n;i++) h[i]=_h[i];
for(int i=0;i<logg;i++){
for(int j=0;j<n;j++){
if(i==0) sparse[i][j]={{h[j],j},h[j]};
else if(j-(1<<(i-1))>=0) sparse[i][j]=comb(sparse[i-1][j],sparse[i-1][j-(1<<(i-1))]);
else sparse[i][j]=sparse[i-1][j];
}
}
resi(0,n-1,res);
klk=0;
for(int i:res) uniq[klk++]=i;
klk=unique(uniq,uniq+klk)-uniq;
int tren=0;
for(int i:res){
if(uniq[tren]!=i) tren++;
suff[tren]++;
}
for(int i=klk-2;i>=0;i--) suff[i]+=suff[i+1];
}
int max_towers(int l, int r, int d) {
int koji=lower_bound(uniq,uniq+klk,d)-uniq;
if(koji==klk) return 0;
return suff[koji];
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |