#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;
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 (get(i + 1, R[i] - 1) - d >= h[i]) {
left = i;
break;
}
}
}
for(int i = r; i >= l; i--) {
if (R[i] > r) {
if (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 |
3501 ms |
9816 KB |
Output is correct |
2 |
Execution timed out |
4026 ms |
10552 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 |
2 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6608 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6488 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6488 KB |
Output is correct |
24 |
Correct |
1 ms |
6488 KB |
Output is correct |
25 |
Correct |
1 ms |
6488 KB |
Output is correct |
26 |
Correct |
3 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
1 ms |
6488 KB |
Output is correct |
29 |
Correct |
1 ms |
6488 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6488 KB |
Output is correct |
33 |
Correct |
2 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6484 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6608 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6488 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6488 KB |
Output is correct |
24 |
Correct |
1 ms |
6488 KB |
Output is correct |
25 |
Correct |
1 ms |
6488 KB |
Output is correct |
26 |
Correct |
3 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
1 ms |
6488 KB |
Output is correct |
29 |
Correct |
1 ms |
6488 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6488 KB |
Output is correct |
33 |
Correct |
2 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6484 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
10 ms |
9660 KB |
Output is correct |
37 |
Correct |
14 ms |
10312 KB |
Output is correct |
38 |
Correct |
18 ms |
10304 KB |
Output is correct |
39 |
Correct |
13 ms |
10312 KB |
Output is correct |
40 |
Correct |
13 ms |
10480 KB |
Output is correct |
41 |
Correct |
13 ms |
10312 KB |
Output is correct |
42 |
Correct |
13 ms |
10308 KB |
Output is correct |
43 |
Correct |
13 ms |
10800 KB |
Output is correct |
44 |
Correct |
12 ms |
10584 KB |
Output is correct |
45 |
Correct |
12 ms |
10572 KB |
Output is correct |
46 |
Correct |
16 ms |
10564 KB |
Output is correct |
47 |
Correct |
14 ms |
10312 KB |
Output is correct |
48 |
Correct |
13 ms |
10312 KB |
Output is correct |
49 |
Correct |
13 ms |
10348 KB |
Output is correct |
50 |
Correct |
13 ms |
10568 KB |
Output is correct |
51 |
Correct |
18 ms |
10584 KB |
Output is correct |
52 |
Correct |
19 ms |
10308 KB |
Output is correct |
53 |
Correct |
14 ms |
10312 KB |
Output is correct |
54 |
Correct |
13 ms |
10312 KB |
Output is correct |
55 |
Correct |
13 ms |
10564 KB |
Output is correct |
56 |
Correct |
13 ms |
10564 KB |
Output is correct |
57 |
Correct |
14 ms |
10300 KB |
Output is correct |
58 |
Correct |
15 ms |
10308 KB |
Output is correct |
59 |
Correct |
15 ms |
10316 KB |
Output is correct |
60 |
Correct |
13 ms |
10320 KB |
Output is correct |
61 |
Correct |
13 ms |
10320 KB |
Output is correct |
62 |
Correct |
14 ms |
10312 KB |
Output is correct |
63 |
Correct |
13 ms |
10312 KB |
Output is correct |
64 |
Correct |
12 ms |
10576 KB |
Output is correct |
65 |
Correct |
13 ms |
10568 KB |
Output is correct |
66 |
Correct |
16 ms |
10568 KB |
Output is correct |
67 |
Correct |
12 ms |
10568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4083 ms |
10316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4035 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 |
2 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6608 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6488 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6488 KB |
Output is correct |
24 |
Correct |
1 ms |
6488 KB |
Output is correct |
25 |
Correct |
1 ms |
6488 KB |
Output is correct |
26 |
Correct |
3 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
1 ms |
6488 KB |
Output is correct |
29 |
Correct |
1 ms |
6488 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6488 KB |
Output is correct |
33 |
Correct |
2 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6484 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
10 ms |
9660 KB |
Output is correct |
37 |
Correct |
14 ms |
10312 KB |
Output is correct |
38 |
Correct |
18 ms |
10304 KB |
Output is correct |
39 |
Correct |
13 ms |
10312 KB |
Output is correct |
40 |
Correct |
13 ms |
10480 KB |
Output is correct |
41 |
Correct |
13 ms |
10312 KB |
Output is correct |
42 |
Correct |
13 ms |
10308 KB |
Output is correct |
43 |
Correct |
13 ms |
10800 KB |
Output is correct |
44 |
Correct |
12 ms |
10584 KB |
Output is correct |
45 |
Correct |
12 ms |
10572 KB |
Output is correct |
46 |
Correct |
16 ms |
10564 KB |
Output is correct |
47 |
Correct |
14 ms |
10312 KB |
Output is correct |
48 |
Correct |
13 ms |
10312 KB |
Output is correct |
49 |
Correct |
13 ms |
10348 KB |
Output is correct |
50 |
Correct |
13 ms |
10568 KB |
Output is correct |
51 |
Correct |
18 ms |
10584 KB |
Output is correct |
52 |
Correct |
19 ms |
10308 KB |
Output is correct |
53 |
Correct |
14 ms |
10312 KB |
Output is correct |
54 |
Correct |
13 ms |
10312 KB |
Output is correct |
55 |
Correct |
13 ms |
10564 KB |
Output is correct |
56 |
Correct |
13 ms |
10564 KB |
Output is correct |
57 |
Correct |
14 ms |
10300 KB |
Output is correct |
58 |
Correct |
15 ms |
10308 KB |
Output is correct |
59 |
Correct |
15 ms |
10316 KB |
Output is correct |
60 |
Correct |
13 ms |
10320 KB |
Output is correct |
61 |
Correct |
13 ms |
10320 KB |
Output is correct |
62 |
Correct |
14 ms |
10312 KB |
Output is correct |
63 |
Correct |
13 ms |
10312 KB |
Output is correct |
64 |
Correct |
12 ms |
10576 KB |
Output is correct |
65 |
Correct |
13 ms |
10568 KB |
Output is correct |
66 |
Correct |
16 ms |
10568 KB |
Output is correct |
67 |
Correct |
12 ms |
10568 KB |
Output is correct |
68 |
Execution timed out |
4083 ms |
10316 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3501 ms |
9816 KB |
Output is correct |
2 |
Execution timed out |
4026 ms |
10552 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |