#include "towers.h"
#include<bits/stdc++.h>
using namespace std;
#define in insert
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
//#define int long long
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
const int mxn = 1e5 + 10, inf = 2e9 + 10;
int h[mxn], n, lg[mxn], L[mxn], R[mxn];
pair<int, int> p[mxn][20];
pair<int, int> get(int l, int r) {
int k = lg[r - l + 1];
return max(p[l][k], p[r - (1 << k) + 1][k]);
}
void build(int l, int r) {
if(l > r) return;
int pos = get(l, r).ss;
L[pos] = l;
R[pos] = r;
if(l == r) return;
pre[pos] = 1;
build(l, pos - 1);
build(pos + 1, r);
}
void init(int _n, vector<int> H) {
n = _n;
for(int i = 1; i <= n; i++) {
h[i] = H[i - 1];
p[i][0] = {h[i], i};
}
for(int j = 1; j < 20; j++) {
for(int i = 1; i + (1 << j) - 1 <= n; i++) {
p[i][j] = max(p[i][j - 1], p[i + (1 << (j - 1))][j - 1]);
}
}
for(int i = 2; i <= n; i++) {
lg[i] = lg[i / 2] + 1;
}
build(1, n);
for(int i = 1; i <= n; i++) {
pre[i] += pre[i - 1];
}
}
int max_towers(int l, int r, int D) {
l++;
r++;
if(l == r) return 1;
int ans = r - l + 1 - pre[r] + pre[l - 1];
if(pre[l] - pre[l - 1] == 1 && R[l] == l) ans++;
if(pre[r] - pre[r - 1] == 1 && L[r] == r) ans++;
return ans;
}
Compilation message
towers.cpp: In function 'void build(int, int)':
towers.cpp:36:2: error: 'pre' was not declared in this scope
36 | pre[pos] = 1;
| ^~~
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:57:3: error: 'pre' was not declared in this scope
57 | pre[i] += pre[i - 1];
| ^~~
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:65:24: error: 'pre' was not declared in this scope
65 | int ans = r - l + 1 - pre[r] + pre[l - 1];
| ^~~