#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int R[100010],L[100010],h[100010],n,s[1<<18];
vector<pii> v;
struct ss {
int mmin,mmax,sl,sr;
} sg[1<<18]; // i<j -> sl = j-i, sr=i-j
ss merge(ss l, ss r) {
if (l.mmin==-1 && r.mmin==-1) return {-1,0,0,0};
if (l.mmin==-1) return r;
if (r.mmin==-1) return l;
ss ans;
ans.mmax=max(l.mmax,r.mmax);
ans.mmin=min(l.mmin,r.mmin);
ans.sl=max({l.sl,r.sl,r.mmax-l.mmin});
ans.sr=max({l.sr,r.sr,l.mmax-r.mmin});
return ans;
}
void build_sg(int l, int r, int idx) {
if (l==r) sg[idx]={h[l],h[l],0,0};
else {
int mid=(l+r)/2;
build_sg(l,mid,2*idx);
build_sg(mid+1,r,2*idx+1);
sg[idx]=merge(sg[2*idx],sg[2*idx+1]);
}
}
ss query_sg(int l, int r, int idx, int x, int y) {
if (x>r || y<l) return {-1,0,0,0};
if (x<=l && r<=y) return sg[idx];
int mid=(l+r)/2;
return merge(query_sg(l,mid,2*idx,x,y),query_sg(mid+1,r,2*idx+1,x,y));
}
struct node {
int mmin,mmax,cnt;
node *l, *r;
node() : mmin(INT_MAX), mmax(-INT_MAX), cnt(0), l(NULL), r(NULL) {}
node(int val) : mmin(val), mmax(val), cnt(1), l(NULL), r(NULL) {}
node(node *L, node *R) : mmin(INT_MAX), mmax(-INT_MAX), cnt(0) , r(R), l(L) {
if (L) {
mmin=min(L->mmin,mmin);
mmax=max(L->mmax,mmax);
cnt+=L->cnt;
}
if (R) {
mmin=min(R->mmin,mmin);
mmax=max(R->mmax,mmax);
cnt+=R->cnt;
}
}
};
typedef pair<int,node*> pin;
vector<pin> arr;
struct N {
int mmin,mmax,cnt;
};
node* build(int l, int r) {
if (l==r) return new node();
int mid=(l+r)/2;
return new node(build(l,mid),build(mid+1,r));
}
node* update(int l, int r, int x, node* pnode) {
if (x>r || x<l) return pnode;
if (l==r) return new node(l);
int mid=(l+r)/2;
return new node(update(l,mid,x,pnode->l),update(mid+1,r,x,pnode->r));
}
N query(int l, int r, int x, int y, node *pnode) {
if (x>r || y<l) return {INT_MAX,-INT_MAX,0};
if (x<=l && r<=y) return {pnode->mmin,pnode->mmax,pnode->cnt};
int mid=(l+r)/2;
N ans,L=query(l,mid,x,y,pnode->l),R=query(mid+1,r,x,y,pnode->r);
ans.mmax=max(L.mmax,R.mmax);
ans.mmin=min(L.mmin,R.mmin);
ans.cnt=L.cnt+R.cnt;
return ans;
}
void build_s(int l, int r, int idx) {
if (l==r) s[idx]=h[l];
else {
int mid=(l+r)/2;
build_s(l,mid,2*idx);
build_s(mid+1,r,2*idx+1);
s[idx]=max(s[2*idx],s[2*idx+1]);
}
}
int query_s(int l, int r, int idx, int x, int y) {
if (x>r || y<l) return 0;
if (x<=l && r<=y) return s[idx];
int mid=(l+r)/2;
return max(query_s(l,mid,2*idx,x,y),query_s(mid+1,r,2*idx+1,x,y));
}
void init(int N, vector<int> H) {
n=N;
for (int i=0; i<n; ++i) h[i+1]=H[i];
L[1]=0;
for (int i=2; i<=n; ++i) {
int idx=i-1;
while (h[idx]>=h[i] && idx!=-1) idx=L[idx];
L[i]=idx;
}
R[n]=n+1;
for (int i=n-1; i>=1; --i) {
int idx=i+1;
while (h[idx]>=h[i] && idx!=-1) idx=R[idx];
R[i]=idx;
}
build_s(1,n,1);
build_sg(1,n,1);
for (int i=1; i<=n; ++i) {
int h1=-1,h2=-1;
if (L[i]+1<=i-1) h1=query_s(1,n,1,L[i]+1,i-1);
if (i+1<=R[i]-1) h2=query_s(1,n,1,i+1,R[i]-1);
if (L[i]==0) h1=INT_MAX;
if (R[i]==n+1) h2=INT_MAX;
if (h1!=-1 && h2!=-1) v.push_back(pii(min(h1,h2)-h[i],i));
}
sort(v.begin(),v.end(),greater<pii>());
arr.push_back(pin(INT_MAX,build(1,n)));
for (int i=0; i<v.size(); ++i) {
node* pnode=update(1,n,v[i].second,arr.back().second);
if (arr.back().first==v[i].first) arr.pop_back();
arr.push_back(pin(v[i].first,pnode));
}
}
int max_towers(int Le, int Ri, int D) {
++Le; ++Ri;
int l=0, r=arr.size()-1;
while (l<r) {
int mid=(l+r+1)/2;
if (arr[mid].first>=D) l=mid;
else r=mid-1;
}
N res=query(1,n,Le,Ri,arr[l].second);
int cnt=res.cnt,mi=res.mmin,ma=res.mmax;
if (cnt==0) return 1;
int LL=-1;
if (mi!=Le && query_s(1,n,1,Le,mi-1)>=h[mi]+D) {
l=Le; r=mi-1;
while (l<r) {
int mid=(l+r+1)/2;
if (query_s(1,n,1,mid,mi-1)>=h[mi]+D) l=mid;
else r=mid-1;
}
LL=l;
}
if (LL!=-1 && query_sg(1,n,1,Le,LL).sl>=D) ++cnt;
int RR=-1;
if (ma!=Ri && query_s(1,n,1,ma+1,Ri)>=h[ma]+D) {
l=ma+1; r=Ri;
while (l<r) {
int mid=(l+r)/2;
if (query_s(1,n,1,ma+1,mid)>=h[ma]+D) r=mid;
else l=mid+1;
}
RR=l;
}
if (RR!=-1 && query_sg(1,n,1,RR,Ri).sr>=D) ++cnt;
return cnt;
}
Compilation message
towers.cpp: In constructor 'node::node(node*, node*)':
towers.cpp:45:15: warning: 'node::r' will be initialized after [-Wreorder]
45 | node *l, *r;
| ^
towers.cpp:45:11: warning: 'node* node::l' [-Wreorder]
45 | node *l, *r;
| ^
towers.cpp:51:5: warning: when initialized here [-Wreorder]
51 | node(node *L, node *R) : mmin(INT_MAX), mmax(-INT_MAX), cnt(0) , r(R), l(L) {
| ^~~~
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:149:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for (int i=0; i<v.size(); ++i) {
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
373 ms |
9520 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 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
976 KB |
Output is correct |
3 |
Correct |
2 ms |
976 KB |
Output is correct |
4 |
Correct |
2 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
2 ms |
1200 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Incorrect |
0 ms |
208 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
976 KB |
Output is correct |
3 |
Correct |
2 ms |
976 KB |
Output is correct |
4 |
Correct |
2 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
2 ms |
1200 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Incorrect |
0 ms |
208 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1002 ms |
45192 KB |
Output is correct |
2 |
Correct |
1115 ms |
45376 KB |
Output is correct |
3 |
Correct |
1164 ms |
45236 KB |
Output is correct |
4 |
Correct |
1172 ms |
59392 KB |
Output is correct |
5 |
Correct |
1258 ms |
59472 KB |
Output is correct |
6 |
Correct |
1060 ms |
59452 KB |
Output is correct |
7 |
Correct |
1087 ms |
59508 KB |
Output is correct |
8 |
Correct |
933 ms |
16724 KB |
Output is correct |
9 |
Correct |
1022 ms |
16700 KB |
Output is correct |
10 |
Correct |
1452 ms |
16684 KB |
Output is correct |
11 |
Correct |
1529 ms |
16672 KB |
Output is correct |
12 |
Incorrect |
877 ms |
16712 KB |
170th lines differ - on the 1st token, expected: '2', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
318 ms |
10320 KB |
Output is correct |
2 |
Correct |
958 ms |
45248 KB |
Output is correct |
3 |
Correct |
1104 ms |
45220 KB |
Output is correct |
4 |
Correct |
880 ms |
59452 KB |
Output is correct |
5 |
Correct |
731 ms |
59396 KB |
Output is correct |
6 |
Correct |
1032 ms |
59420 KB |
Output is correct |
7 |
Correct |
1047 ms |
59496 KB |
Output is correct |
8 |
Correct |
1372 ms |
16712 KB |
Output is correct |
9 |
Correct |
1348 ms |
16684 KB |
Output is correct |
10 |
Correct |
1096 ms |
16728 KB |
Output is correct |
11 |
Correct |
1175 ms |
16688 KB |
Output is correct |
12 |
Correct |
83 ms |
45284 KB |
Output is correct |
13 |
Correct |
101 ms |
59480 KB |
Output is correct |
14 |
Correct |
114 ms |
59460 KB |
Output is correct |
15 |
Correct |
35 ms |
16712 KB |
Output is correct |
16 |
Correct |
39 ms |
16700 KB |
Output is correct |
17 |
Correct |
81 ms |
43848 KB |
Output is correct |
18 |
Correct |
80 ms |
45280 KB |
Output is correct |
19 |
Correct |
81 ms |
45240 KB |
Output is correct |
20 |
Correct |
106 ms |
59392 KB |
Output is correct |
21 |
Correct |
102 ms |
59488 KB |
Output is correct |
22 |
Correct |
102 ms |
59416 KB |
Output is correct |
23 |
Correct |
103 ms |
59440 KB |
Output is correct |
24 |
Correct |
41 ms |
16712 KB |
Output is correct |
25 |
Correct |
34 ms |
16712 KB |
Output is correct |
26 |
Correct |
39 ms |
16716 KB |
Output is correct |
27 |
Correct |
45 ms |
16696 KB |
Output is correct |
28 |
Correct |
2 ms |
976 KB |
Output is correct |
29 |
Correct |
3 ms |
1104 KB |
Output is correct |
30 |
Correct |
2 ms |
1104 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
976 KB |
Output is correct |
35 |
Correct |
2 ms |
976 KB |
Output is correct |
36 |
Correct |
2 ms |
1104 KB |
Output is correct |
37 |
Correct |
2 ms |
1204 KB |
Output is correct |
38 |
Correct |
2 ms |
1104 KB |
Output is correct |
39 |
Correct |
2 ms |
1104 KB |
Output is correct |
40 |
Correct |
1 ms |
592 KB |
Output is correct |
41 |
Correct |
1 ms |
592 KB |
Output is correct |
42 |
Correct |
1 ms |
592 KB |
Output is correct |
43 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
976 KB |
Output is correct |
3 |
Correct |
2 ms |
976 KB |
Output is correct |
4 |
Correct |
2 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
2 ms |
1200 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Incorrect |
0 ms |
208 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
373 ms |
9520 KB |
12th lines differ - on the 1st token, expected: '2', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |