#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 (L[i] >= l && get(L[i] + 1, i - 1) < h[i] + d) continue;
if (R[i] <= r && get(i + 1, R[i] - 1) < h[i] + d) continue;
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 |
1855 ms |
9900 KB |
Output is correct |
2 |
Execution timed out |
4067 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 |
6568 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 |
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 |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
0 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
2 ms |
6684 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 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 |
1 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 |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6488 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6568 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 |
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 |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
0 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
2 ms |
6684 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 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 |
1 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 |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6488 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
36 |
Correct |
11 ms |
9816 KB |
Output is correct |
37 |
Correct |
17 ms |
10316 KB |
Output is correct |
38 |
Correct |
14 ms |
10320 KB |
Output is correct |
39 |
Correct |
16 ms |
10316 KB |
Output is correct |
40 |
Correct |
13 ms |
10316 KB |
Output is correct |
41 |
Correct |
14 ms |
10308 KB |
Output is correct |
42 |
Correct |
17 ms |
10308 KB |
Output is correct |
43 |
Correct |
12 ms |
10564 KB |
Output is correct |
44 |
Correct |
13 ms |
10564 KB |
Output is correct |
45 |
Correct |
12 ms |
10564 KB |
Output is correct |
46 |
Correct |
12 ms |
10564 KB |
Output is correct |
47 |
Correct |
13 ms |
10328 KB |
Output is correct |
48 |
Correct |
13 ms |
10484 KB |
Output is correct |
49 |
Correct |
13 ms |
10328 KB |
Output is correct |
50 |
Correct |
14 ms |
10568 KB |
Output is correct |
51 |
Correct |
12 ms |
10576 KB |
Output is correct |
52 |
Correct |
14 ms |
10312 KB |
Output is correct |
53 |
Correct |
13 ms |
10308 KB |
Output is correct |
54 |
Correct |
15 ms |
10316 KB |
Output is correct |
55 |
Correct |
13 ms |
10572 KB |
Output is correct |
56 |
Correct |
15 ms |
10568 KB |
Output is correct |
57 |
Correct |
16 ms |
10300 KB |
Output is correct |
58 |
Correct |
19 ms |
10312 KB |
Output is correct |
59 |
Correct |
15 ms |
10320 KB |
Output is correct |
60 |
Correct |
13 ms |
10532 KB |
Output is correct |
61 |
Correct |
17 ms |
10308 KB |
Output is correct |
62 |
Correct |
15 ms |
10384 KB |
Output is correct |
63 |
Correct |
13 ms |
10312 KB |
Output is correct |
64 |
Correct |
16 ms |
10564 KB |
Output is correct |
65 |
Correct |
16 ms |
10640 KB |
Output is correct |
66 |
Correct |
12 ms |
10564 KB |
Output is correct |
67 |
Correct |
12 ms |
10568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4016 ms |
10312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4077 ms |
7000 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 |
6568 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 |
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 |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
0 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
2 ms |
6684 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 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 |
1 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 |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6488 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
36 |
Correct |
11 ms |
9816 KB |
Output is correct |
37 |
Correct |
17 ms |
10316 KB |
Output is correct |
38 |
Correct |
14 ms |
10320 KB |
Output is correct |
39 |
Correct |
16 ms |
10316 KB |
Output is correct |
40 |
Correct |
13 ms |
10316 KB |
Output is correct |
41 |
Correct |
14 ms |
10308 KB |
Output is correct |
42 |
Correct |
17 ms |
10308 KB |
Output is correct |
43 |
Correct |
12 ms |
10564 KB |
Output is correct |
44 |
Correct |
13 ms |
10564 KB |
Output is correct |
45 |
Correct |
12 ms |
10564 KB |
Output is correct |
46 |
Correct |
12 ms |
10564 KB |
Output is correct |
47 |
Correct |
13 ms |
10328 KB |
Output is correct |
48 |
Correct |
13 ms |
10484 KB |
Output is correct |
49 |
Correct |
13 ms |
10328 KB |
Output is correct |
50 |
Correct |
14 ms |
10568 KB |
Output is correct |
51 |
Correct |
12 ms |
10576 KB |
Output is correct |
52 |
Correct |
14 ms |
10312 KB |
Output is correct |
53 |
Correct |
13 ms |
10308 KB |
Output is correct |
54 |
Correct |
15 ms |
10316 KB |
Output is correct |
55 |
Correct |
13 ms |
10572 KB |
Output is correct |
56 |
Correct |
15 ms |
10568 KB |
Output is correct |
57 |
Correct |
16 ms |
10300 KB |
Output is correct |
58 |
Correct |
19 ms |
10312 KB |
Output is correct |
59 |
Correct |
15 ms |
10320 KB |
Output is correct |
60 |
Correct |
13 ms |
10532 KB |
Output is correct |
61 |
Correct |
17 ms |
10308 KB |
Output is correct |
62 |
Correct |
15 ms |
10384 KB |
Output is correct |
63 |
Correct |
13 ms |
10312 KB |
Output is correct |
64 |
Correct |
16 ms |
10564 KB |
Output is correct |
65 |
Correct |
16 ms |
10640 KB |
Output is correct |
66 |
Correct |
12 ms |
10564 KB |
Output is correct |
67 |
Correct |
12 ms |
10568 KB |
Output is correct |
68 |
Execution timed out |
4016 ms |
10312 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1855 ms |
9900 KB |
Output is correct |
2 |
Execution timed out |
4067 ms |
10564 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |