#include "towers.h"
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int h[N], lg[N], sp[20][N], L[N], R[N];
int n;
int get(int l, int r) {
if (l > r) return -1e9;
int k = lg[r - l + 1];
return max(sp[k][l], sp[k][r - (1 << k) + 1]);
}
void init(int N, std::vector<int> H) {
n = N;
for(int i = 0; i < n; i++) h[i] = H[i];
stack<int> st;
for(int i = 0; i < n; i++) {
while (!st.empty() && h[st.top()] >= h[i]) st.pop();
L[i] = st.empty() ? -1 : st.top();
st.push(i);
}
while (!st.empty()) st.pop();
for(int i = n - 1; i >= 0; i--) {
while (!st.empty() && h[st.top()] >= h[i]) st.pop();
R[i] = st.empty() ? n : st.top();
st.push(i);
}
for(int i = 0; i < n; i++) sp[0][i] = h[i];
for(int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;
for(int j = 1; j <= lg[n]; j++) for(int i = 0; i + (1 << j) - 1 < n; i++) {
sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
}
}
int max_towers(int l, int r, int d) {
int ans = 0;
for(int i = l; i <= r; i++) {
if (get(L[i] + 1, i - 1) - d < h[i]) continue;
// cout << i << endl;
if (get(i + 1, R[i] - 1) - d < h[i]) continue;
ans++;
}
int left = -1, right = -1, mn = l;
for(int i = l; i <= r; i++) {
if (L[i] < l) {
if (R[i] > r || get(i + 1, R[i] - 1) - d >= h[i]) {
left = i;
break;
}
}
}
for(int i = r; i >= l; i--) {
if (R[i] > r) {
if (L[i] < l || get(L[i] + 1, i - 1) - d >= h[i]) {
right = i;
break;
}
}
}
for(int i = l + 1; i <= r; i++) if (h[i] < h[mn]) mn = i;
if (left >= 0 && left != mn && get(L[left] - 1, left - 1) - d < h[left]) {
ans++;
}
if (right >= 0 && right != mn && get(right + 1, R[right] - 1) - d < h[right]) {
ans++;
}
if (get(L[mn] + 1, mn - 1) - d < h[mn] || get(mn + 1, R[mn] - 1) - d < h[mn]) ans++;
return ans;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
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;
}
#endif // ngu
Compilation message
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:36:56: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
36 | sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3787 ms |
9904 KB |
Output is correct |
2 |
Execution timed out |
4027 ms |
10564 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6484 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6488 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6484 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6488 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4094 ms |
10316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4024 ms |
6924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6484 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6488 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3787 ms |
9904 KB |
Output is correct |
2 |
Execution timed out |
4027 ms |
10564 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |