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
const int MAXN = 1e5+4;
vector<int> h;
vector<int> lc,rc,a;
int pr[MAXN];
int inited = 0;
int fl[MAXN];
int fr[MAXN];
int sum (int l, int r) {
if (l > r) return 0;
if (l==0) return pr[r];
return pr[r] - pr[l-1];
}
int dfs(int i, int d){
if (i==-1) return 0;
if (lc[i]==-1 && rc[i]==-1) return -a[i];
int lans = dfs(lc[i],d);
int rans = dfs(rc[i],d);
if (lans < 0) {
lc[lc[i]] = -2; rc[lc[i]] = -2;
lc[i] = -1;
}
if (rans < 0) {
lc[rc[i]] = -2; rc[rc[i]] = -2;
rc[i] = -1;
}
if (lans < 0 && a[i] - d >= -lans) lans = 1;
if (rans < 0 && a[i] - d >= -rans) rans = 1;
if (!lans) return rans; if (!rans) return lans;
if (lans < 0 && rans < 0) return max(lans,rans);
if (lans < 0) return rans;
if (rans < 0) return lans;
return lans + rans;
}
signed helper(signed l, signed r, int d){
int sz = r-l+1;
a.assign(sz,0); for (int i=l; i<=r; i++) a[i-l] = h[i];
a.push_back(3e9);
stack<int> st; st.push(sz);
lc.assign(sz+1,-1); rc.assign(sz+1,-1);
for (int i=0; i<sz; i++){
int last = -1;
while (a[i] > a[st.top()]) {
last=st.top();
st.pop();
}
lc[i] = last;
rc[st.top()]=i;
st.push(i);
}
return dfs(sz,d);
}
void init(signed N, std::vector<signed> H){
h.resize(N); for (int i = 0; i < N; i++) h[i] = H[i];
}
void init0(int d) {
helper(0, h.size()-1, d);
pr[0] = (lc[0]==-1 && rc[0]==-1);
for (int i=1; i<h.size(); i++) pr[i] = pr[i-1] + (lc[i]==-1 && rc[i]==-1);
int rs = 1e9;
for (int i=h.size()-1; i>=0; i--) {
if (lc[i]!=-2) rs = i;
fl[i] = rs;
}
rs = -1;
for (int i=0; i<h.size(); i++) {
if (lc[i]!=-2) rs = i;
fr[i] = rs;
}
}
signed max_towers(signed l, signed r, signed d) {
if (!inited) init0(d), inited = 1;
l = fl[l]; r = fr[r];
if (r-l <= 1) return 1;
int s = sum(l+1,r-1);
s += rc[l] == -1;
s += lc[r] == -1;
return s;
}
Compilation message (stderr)
towers.cpp: In function 'long long int dfs(long long int, long long int)':
towers.cpp:35:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
35 | if (!lans) return rans; if (!rans) return lans;
| ^~
towers.cpp:35:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
35 | if (!lans) return rans; if (!rans) return lans;
| ^~
towers.cpp: In function 'void init0(long long int)':
towers.cpp:71:17: 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]
71 | for (int i=1; i<h.size(); i++) pr[i] = pr[i-1] + (lc[i]==-1 && rc[i]==-1);
| ~^~~~~~~~~
towers.cpp:78:17: 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]
78 | for (int i=0; i<h.size(); i++) {
| ~^~~~~~~~~
# | 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... |