在校内OJ过的,这里给个luogu链接:https://www.luogu.org/problem/P5503
灯塔必然是整数,因此对于sqrt(i - j)一样的山头,只有最高的山有效。
于是问题就变成了对每个山头logn次区间最值查询。
静态查询显然ST表。
整体复杂度O(nlogn)
code
#include <bits/stdc++.h>
#define numm ch-48
#define pd putchar(' ')
#define pn putchar('\n')
#define pb push_back
#define fi first
#define se second
#define fre1 freopen("1.txt","r",stdin)
#define fre2 freopen("2.txt","w",stdout)
typedef long long int ll;
typedef long long int LL;
using namespace std;
template<typename T>
void read(T& res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = numm; isdigit(ch = getchar()); res = (res << 1) + (res << 3) + numm);
flag && (res = -res);
}
template<typename T>
void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
//static auto _ = []()
//{
// ios::sync_with_stdio(false);
// cin.tie(0);
// return 0;
//}();
//
const int maxn = 1e5 + 5;
int n;
ll hig[maxn];
ll st[maxn][18];
int lg[maxn];
int power[320];
ll ans[maxn];
ll query(int l, int r) {
int mid = lg[r - l + 1];
return max(st[l][mid], st[r - (1 << mid) + 1][mid]);
}
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(hig[i]);
ans[i] = st[i][0] = hig[i];
}
lg[1] = 0;
for (int i = 2; i <= 1e5; i++) {
lg[i] = lg[i >> 1] + 1;
}
for (int j = 1; j <= lg[n]; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 1; i < 320; i++) {
power[i] = i * i;
}
for (int i = 1; i <= n; i++) {
int t = 1;
while (1) {
if (i + power[t] < n) {
ans[i] = max(ans[i], t + query(i + power[t - 1] + 1, i + power[t]));
t++;
}
else if(i + power[t] <= n) {
ans[i] = max(ans[i], t + query(i + power[t - 1] + 1, n));
break;
}
else {
break;
}
}
t = 1;
while (1) {
if (i - power[t] > 1) {
ans[i] = max(ans[i], t + query(i - power[t], i - power[t - 1] - 1));
t++;
}
else if (i - power[t] == 1) {
ans[i] = max(ans[i], t + query(1, i - power[t - 1] - 1));
break;
}
else {
break;
}
}
write(ans[i] - hig[i]), pn;
}
return 0;
}