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;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef pair<int,ii> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
#define maxn 100005
struct node{
int s,e,m,v=0,mn=0,mx=0,mnd=0,mxd=0;
node *l,*r;
node(int _s,int _e){
s=_s;e=_e;m=(s+e)>>1;
if(s!=e)l=new node(s,m),r=new node(m+1,e);
}
void up(int x,int nv){
if(s==x&&e==x){
v=mn=mx=nv;
return;
}
if(x<=m)l->up(x,nv);
else r->up(x,nv);
mn=min(l->mn,r->mn);
mx=max(l->mx,r->mx);
mnd=min(min(l->mnd,r->mnd),r->mn-l->mx);
mxd=max(max(l->mxd,r->mxd),r->mx-l->mn);
}
int qmx(int x,int y){
if(s==x&&e==y)return mx;
if(y<=m)return l->qmx(x,y);
if(x>m)return r->qmx(x,y);
return max(l->qmx(x,m),r->qmx(m+1,y));
}
int qmn(int x,int y){
if(s==x&&e==y)return mn;
if(y<=m)return l->qmn(x,y);
if(x>m)return r->qmn(x,y);
return min(l->qmn(x,m),r->qmn(m+1,y));
}
iii qmxd(int x,int y){
if(s==x&&e==y)return {mxd,{mn,mx}};
if(y<=m)return l->qmxd(x,y);
if(x>m)return r->qmxd(x,y);
iii p1=qmxd(x,m),p2=qmxd(m+1,y);
int mxd=max(max(p1.fi,p2.fi),p2.se.se-p1.se.fi);
int mx=max(p1.se.se,p2.se.se);
int mn=min(p1.se.fi,p2.se.fi);
return {mxd,{mn,mx}};
}
iii qmnd(int x,int y){
if(s==x&&e==y)return {mnd,{mn,mx}};
if(y<=m)return l->qmnd(x,y);
if(x>m)return r->qmnd(x,y);
iii p1=qmnd(x,m),p2=qmnd(m+1,y);
int mnd=min(min(p1.fi,p2.fi),p2.se.fi-p1.se.se);
int mx=max(p1.se.se,p2.se.se);
int mn=min(p1.se.fi,p2.se.fi);
return {mnd,{mn,mx}};
}
int findr(int x,int y,int d){
if(s==x&&e==y){
if(mx<d)return -1;
if(s==e)return s;
if(r->mx>=d)return r->findr(m+1,y,d);
else return l->findr(x,m,d);
}
if(y<=m)return l->findr(x,y,d);
if(x>m)return r->findr(x,y,d);
int tmp=r->findr(m+1,y,d);
if(tmp!=-1)return tmp;
return l->findr(x,m,d);
}
int findl(int x,int y,int d){
if(s==x&&e==y){
if(mx<d)return -1;
if(s==e)return s;
if(l->mx>=d)return l->findl(x,m,d);
else return r->findl(m+1,y,d);
}
if(y<=m)return l->findl(x,y,d);
if(x>m)return r->findl(x,y,d);
int tmp=l->findl(x,m,d);
if(tmp!=-1)return tmp;
return r->findl(m+1,y,d);
}
}*seg;
struct node2{
int s,e,m,v=-1,mn=INF,mx=-1,cnt=0;
node2 *l,*r;
node2(int _s,int _e){
s=_s;e=_e;m=(s+e)>>1;
if(s!=e)l=new node2(s,m),r=new node2(m+1,e);
}
node2(node2 *n){
s=n->s,e=n->e,m=(s+e)>>1;
v=n->v,mn=n->mn,mx=n->mx,cnt=n->cnt;
l=n->l,r=n->r;
}
void up(int x,int nv){
if(s==x&&e==x){
v=mn=mx=nv;
cnt=1;
return;
}
if(x<=m){
l=new node2(l);
l->up(x,nv);
}
else{
r=new node2(r);
r->up(x,nv);
}
mx=max(l->mx,r->mx);
mn=min(l->mn,r->mn);
cnt=l->cnt+r->cnt;
}
int qmn(int x,int y){
if(s==x&&e==y)return mn;
if(y<=m)return l->qmn(x,y);
if(x>m)return r->qmn(x,y);
return min(l->qmn(x,m),r->qmn(m+1,y));
}
int qmx(int x,int y){
if(s==x&&e==y)return mx;
if(y<=m)return l->qmx(x,y);
if(x>m)return r->qmx(x,y);
return max(l->qmx(x,m),r->qmx(m+1,y));
}
int qcnt(int x,int y){
if(s==x&&e==y)return cnt;
if(y<=m)return l->qcnt(x,y);
if(x>m)return r->qcnt(x,y);
return l->qcnt(x,m)+r->qcnt(m+1,y);
}
}*root[maxn];
int ls[maxn],rs[maxn],delta[maxn];
vector<int> h;
vector<ii> d;
void init(int N,vector<int> H){
h=H;
seg=new node(0,N-1);
for(int i=0;i<N;++i)seg->up(i,H[i]);
stack<int> st;
for(int i=0;i<N;++i){
while(!st.empty()&&H[st.top()]>H[i])st.pop();
if(st.empty())ls[i]=-1;
else ls[i]=st.top();
st.push(i);
}
while(!st.empty())st.pop();
for(int i=N-1;i>=0;--i){
while(!st.empty()&&H[st.top()]>H[i])st.pop();
if(st.empty())rs[i]=-1;
else rs[i]=st.top();
st.push(i);
}
for(int i=0;i<N;++i){
int a,b;
if(ls[i]!=-1)a=seg->qmx(ls[i],i);
else a=INF;
if(rs[i]!=-1)b=seg->qmx(i,rs[i]);
else b=INF;
delta[i]=min(a,b)-H[i];
d.pb({delta[i],i});
}
sort(all(d));
root[N]=new node2(0,N-1);
for(int i=N-1;i>=0;--i){
root[i]=new node2(root[i+1]);
int x=d[i].se;
root[i]->up(x,x);
}
}
int max_towers(int L,int R,int D){
int id=lb(d,ii(D,0));
int num=0;
int l=root[id]->qmn(L,R);
int r=root[id]->qmx(L,R);
if(l==INF){
int lo=L,hi=R,mid,res=R;
while(lo<=hi){
mid=(lo+hi)>>1;
if(seg->qmxd(L,mid).fi>=D)res=mid,hi=mid-1;
else lo=mid+1;
}
if(seg->qmnd(res,R).fi<=-D)return 2;
return 1;
}
int ans=root[id]->qcnt(L,R);
int li=seg->findr(L,l,h[l]+D);
if(li!=-1&&seg->qmxd(L,li).fi>=D)++ans;
int ri=seg->findl(r,R,h[r]+D);
if(ri!=-1&&seg->qmnd(ri,R).fi<=-D)++ans;
return ans;
}
Compilation message (stderr)
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:213:6: warning: unused variable 'num' [-Wunused-variable]
213 | int num=0;
| ^~~
# | 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... |