#include "towers.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10,lg=18,kaf=(1<<lg),inf=1e9+5;
int all[maxn];
vector<int>allp;
struct segmentmin{
pair<int ,int>seg[(1<<(lg+1))];
segmentmin(){
for(int i=2*kaf-1;i>=0;i--){
if(i>=kaf){
seg[i].second=i-kaf;
}else{
seg[i].second=seg[(i<<1)].second;
}
}
}
void ins(int i,int w){
i+=kaf;
seg[i].first=w;
i>>=1;
while(i>0){
seg[i]=min(seg[(i<<1)],seg[(i<<1)^1]);
i>>=1;
}
}
pair<int ,int> pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return make_pair(inf,inf);
}
if(l>=tl&&r<=tr){
return seg[i];
}
int m=(l+r)>>1;
return min(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
}
}segmn;
struct segmentmax{
pair<int ,int>seg[(1<<(lg+1))];
segmentmax(){
for(int i=2*kaf-1;i>=0;i--){
if(i>=kaf){
seg[i].second=i-kaf;
}else{
seg[i].second=seg[(i<<1)].second;
}
}
}
void ins(int i,int w){
i+=kaf;
seg[i].first=w;
i>>=1;
while(i>0){
seg[i]=max(seg[(i<<1)],seg[(i<<1)^1]);
i>>=1;
}
}
pair<int ,int> pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return make_pair(0,0);
}
if(l>=tl&&r<=tr){
return seg[i];
}
int m=(l+r)>>1;
return max(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
}
}segmx;
int n;
void build(int l,int r,int ers=inf){
if(l==r){
return ;
}
int bor=ers;
int ind=segmx.pors(1,0,kaf-1,l,r).second;
int cl=0,cr=0,ret=0,cnt=0;
if(segmn.pors(1,0,kaf-1,l,ind-1).first<=all[ind]){
int e=min(ers,all[ind]-segmn.pors(1,0,kaf-1,l,ind-1).first);
build(l,ind-1,e);
bor=min(bor,e);
cnt++;
}
if(segmn.pors(1,0,kaf-1,ind+1,r).first<=all[ind]){
int e=min(ers,all[ind]-segmn.pors(1,0,kaf-1,ind+1,r).first);
build(ind+1,r,e);
bor=min(bor,e);
cnt++;
}
if(cnt==2){
allp.push_back(bor);
}
}
void init(int N, std::vector<int> H) {
n=N;
for(int i=0;i<n;i++){
all[i]=H[i];
segmx.ins(i,H[i]);
segmn.ins(i,H[i]);
}
build(1,n);
sort(allp.begin(),allp.end());
}
int max_towers(int L, int R, int D) {
int p=lower_bound(allp.begin(),allp.end(),D)-allp.begin();
p=(int)allp.size()-p;
return p;
}
Compilation message
towers.cpp: In function 'void build(int, int, int)':
towers.cpp:79:7: warning: unused variable 'cl' [-Wunused-variable]
79 | int cl=0,cr=0,ret=0,cnt=0;
| ^~
towers.cpp:79:12: warning: unused variable 'cr' [-Wunused-variable]
79 | int cl=0,cr=0,ret=0,cnt=0;
| ^~
towers.cpp:79:17: warning: unused variable 'ret' [-Wunused-variable]
79 | int cl=0,cr=0,ret=0,cnt=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
286 ms |
16728 KB |
12th lines differ - on the 1st token, expected: '2', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
8792 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
8792 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
423 ms |
10064 KB |
1st lines differ - on the 1st token, expected: '11903', found: '33009' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
155 ms |
9048 KB |
1st lines differ - on the 1st token, expected: '7197', found: '7196' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
8792 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
286 ms |
16728 KB |
12th lines differ - on the 1st token, expected: '2', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |