#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9+10;
struct func {
int h, i, l, r;
};
int n;
int mx[500000];
int mx2[500000];
int H[500000];
int Sqrt[500000];
int eval(func f, int i) {
return f.h+Sqrt[abs(i-f.i)];
assert(i>=f.i);
return f.h+Sqrt[i-f.i];
}
// returns when right part of func a intersects func b
int intersectRight(func a, func b) {
if (eval(a, n) >= eval(b, n)) return inf;
long long d = b.h-a.h;
if (b.i-a.i-d*d<0) return -1;
long double x = (b.i-a.i)/2.0/d-d/2.0;
long long ans = ceil(x*x)+b.i;
assert(ans < n);
return ans;
}
void solveRight(int mx[]) {
stack<func> s;
for (int i = 0; i < n; i++) {
func f = {H[i], i, 0, n-1};
while (!s.empty()) {
func f2 = s.top();
int intersect = intersectRight(f2, f);
//cout << f2.i << " intersects " << f.i << " at " << intersect << endl;
if (intersect <= f2.l) {
s.pop();
/*
s.top().r = f.l-1;
break;
*/
} else {
if (intersect - 1 <= n-1) s.top().r = intersect - 1;
f.l = intersect;
break;
}
}
if (f.l <= n-1) s.push(f);
//assert(f.l >= i);
}
while (!s.empty()) {
func f = s.top(); s.pop();
//cout << f.i << " is max from " << f.l << " to " << f.r << endl;
for (int i = f.l; i <= f.r; i++) mx[i] = max(mx[i], eval(f, i));
}
for (int i = 0; i < n; i++) {
//cout << "mx[" << i << "] = " << mx[i] << endl;
}
}
int main() {
cin >> n;
Sqrt[0] = 0;
for (int i = 1; i <= 1000; i++) {
for (int j = (i-1)*(i-1)+1; j <= min(i*i, n-1); j++) Sqrt[j] = i;
}
for (int i = 0; i < n; i++) {
cin >> H[i]; mx[i] = H[i];
}
solveRight(mx);
for (int i = 0; i < n/2; i++) {
swap(H[i], H[n-i-1]);
}
solveRight(mx2);
for (int i = 0; i < n; i++) {
mx[i] = max(mx[i], mx2[n-1-i]);
}
for (int i = 0; i < n/2; i++) {
swap(H[i], H[n-i-1]);
}
for (int i = 0; i < n; i++) {
cout << mx[i]-H[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
1800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
1840 KB |
Output is correct |
2 |
Incorrect |
21 ms |
1792 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
2548 KB |
Output is correct |
2 |
Correct |
70 ms |
2552 KB |
Output is correct |
3 |
Incorrect |
62 ms |
3064 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
5240 KB |
Output is correct |
2 |
Correct |
148 ms |
4984 KB |
Output is correct |
3 |
Incorrect |
117 ms |
5864 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
261 ms |
9624 KB |
Output is correct |
2 |
Correct |
249 ms |
7672 KB |
Output is correct |
3 |
Incorrect |
185 ms |
9592 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
13548 KB |
Output is correct |
2 |
Incorrect |
356 ms |
10488 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
361 ms |
11000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |