#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 110000;
const int LOGN = 20;
const int INF = (int)(1e9+1);
int n, h[N], opt, dp0[N], dp1[N], dp2[N], l[N], r[N], tt[LOGN][N];
void init(int N, vector<int> H) {
n = N;
for (int i = 1; i <= n; i++)
h[i] = H[i - 1];
}
void upd(int &k, int x) {
k = max(k, x);
}
int getmax(int l, int r) {
assert(l <= r); // WTF???
int sz = 31 - __builtin_clz(r - l + 1);
return max(tt[sz][l], tt[sz][r - (1 << sz) + 1]);
}
int max_towers(int L, int R, int D) {
L++; R++;
if (L == R) return 1;
h[L - 1] = h[R + 1] = INF;
for (int i = L - 1; i <= R + 1; i++)
tt[0][i] = h[i];
for (int k = 1; k < LOGN; k++)
for (int i = L - 1; i + (1 << k) - 1 <= R + 1; i++)
tt[k][i] = max(tt[k - 1][i], tt[k - 1][i + (1 << (k - 1))]);
stack<int> st; st.push(L - 1);
for (int i = L; i <= R + 1; i++) {
while (h[st.top()] < h[i]) {
r[st.top()] = i;
st.pop();
}
l[i] = st.top();
st.push(i);
}
for (int i = L - 1; i <= R + 1; i++)
dp0[i] = dp1[i] = dp2[i] = -INF;
for (int i = L; i <= R; i++) {
dp0[i] = 1;
int cl = L - 1, cr = i - 1;
while (cl < cr) {
int mid = (cl + cr) / 2 + 1;
if (getmax(mid, i - 1) >= h[i] + D) cl = mid;
else cr = mid - 1;
}
upd(dp0[i], dp2[cl] + 1);
cl = i + 1, cr = R + 1;
while (cl < cr) {
int mid = (cl + cr) / 2;
if (getmax(i + 1, mid) >= h[i] + D) cr = mid;
else cl = mid + 1;
}
upd(dp1[cl], dp0[i]);
upd(dp2[i], max(dp2[l[i]], dp1[i]));
upd(dp1[r[i]], dp1[i]);
}
int ans = 0;
for (int i = L; i <= R; i++)
ans = max(ans, dp0[i]);
return ans;
}
/*
int main() {
int N, Q;
assert(2 == scanf("%d %d", &N, &Q));
std::vector<int> H(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &H[i]));
}
init(N, H);
for (int i = 0; i < Q; ++i) {
int L, R, D;
assert(3 == scanf("%d %d %d", &L, &R, &D));
printf("%d\n", max_towers(L, R, D));
}
return 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... |