#include "towers.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int ind = -1;
bool ch = true;
vector <int> h;
const int N = 100100, M = 20;
int mx[N][M], Left[N], Right[N];
void init(int n, vector<int> a) {
ch = true;
ind = -1;
h = a;
for (int i = 0; i < n - 1; ++i)
{
if (a[i] > a[i + 1]) {
if (ind == -1) ind = i;
}
else {
if (ind != -1) ch = false;
}
}
for (int i = 0; i < n; i++) {
mx[i][0] = a[i];
}
for (int j = 1; j < 20; j++) {
for (int i = n - 1; i >= 0; i--) {
mx[i][j] = max(mx[i][j - 1], mx[min(n, i + (1 << (j - 1)))][j - 1]);
}
}
}
int findmaxinrange(int L, int R) {
int u = log2(R - L + 1);
return max(mx[L][u], mx[R - (1 << u) + 1][u]);
}
int max_towers(int L, int R, int D) {
if (ch) {
if (R < ind || L > ind) return 1;
if (h[ind] >= (max(h[L], h[R]) + D)) return 2;
return 1;
}
int n = (int)h.size();
vector <pair<int, int>> v;
set <int> ms;
for (int i = 0; i < n; i++) {
int l = 0, r = i - 1;
Left[i] = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (findmaxinrange(mid, i - 1) >= h[i] + D) {
Left[i] = mid;
l = mid + 1;
}
else r = mid - 1;
}
Right[i] = n;
l = i + 1, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (findmaxinrange(i + 1, mid) >= h[i] + D) {
Right[i] = mid;
r = mid - 1;
}
else l = mid + 1;
}
v.push_back({ h[i], i });
}
int ans = 0;
sort(v.begin(), v.end());
for (auto it : v) {
int ind = it.second;
if (ind > R || ind < L) continue;
auto it = ms.lower_bound(ind);
if (it != ms.begin()) {
it--;
if (*it > Left[ind]) continue;
}
it = ms.lower_bound(ind);
if (it != ms.end() && *it < Right[ind]) {
continue;
}
ms.insert(ind);
++ans;
}
return ans;
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:76:8: error: redeclaration of 'auto it'
76 | auto it = ms.lower_bound(ind);
| ^~
towers.cpp:73:12: note: 'std::pair<int, int> it' previously declared here
73 | for (auto it : v) {
| ^~