#include "towers.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int ind = -1;
bool ch = true;
vector <int> h;
const int N = 100100, M = 20;
int mx[N][M], Left[N], Right[N];
void init(int n, vector<int> a) {
ch = true;
ind = -1;
h = a;
for (int i = 0; i < n - 1; ++i)
{
if (a[i] > a[i + 1]) {
if (ind == -1) ind = i;
}
else {
if (ind != -1) ch = false;
}
}
for (int i = 0; i < n; i++) {
mx[i][0] = a[i];
}
for (int j = 1; j < 20; j++) {
for (int i = n - 1; i >= 0; i--) {
mx[i][j] = max(mx[i][j - 1], mx[min(n, i + (1 << (j - 1)))][j - 1]);
}
}
}
int findmaxinrange(int L, int R) {
int u = log2(R - L + 1);
return max(mx[L][u], mx[R - (1 << u) + 1][u]);
}
int max_towers(int L, int R, int D) {
if (ch) {
if (R < ind || L > ind) return 1;
if (h[ind] >= (max(h[L], h[R]) + D)) return 2;
return 1;
}
int n = (int)h.size();
vector <pair<int, int>> v;
set <int> ms;
for (int i = 0; i < n; i++) {
int l = 0, r = i - 1;
Left[i] = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (findmaxinrange(mid, i - 1) >= h[i] + D) {
Left[i] = mid;
l = mid + 1;
}
else r = mid - 1;
}
Right[i] = n;
l = i + 1, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (findmaxinrange(i + 1, mid) >= h[i] + D) {
Right[i] = mid;
r = mid - 1;
}
else l = mid + 1;
}
v.push_back({ h[i], i });
}
int ans = 0;
sort(v.begin(), v.end());
for (auto q : v) {
int ind = q.second;
if (ind > R || ind < L) continue;
auto it = ms.lower_bound(ind);
if (it != ms.begin()) {
it--;
if (*it > Left[ind]) continue;
}
it = ms.lower_bound(ind);
if (it != ms.end() && *it < Right[ind]) {
continue;
}
ms.insert(ind);
++ans;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
407 ms |
5676 KB |
Output is correct |
2 |
Correct |
908 ms |
9276 KB |
Output is correct |
3 |
Correct |
832 ms |
9328 KB |
Output is correct |
4 |
Correct |
895 ms |
9264 KB |
Output is correct |
5 |
Correct |
744 ms |
9264 KB |
Output is correct |
6 |
Correct |
829 ms |
9296 KB |
Output is correct |
7 |
Correct |
698 ms |
9288 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
428 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
2 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
432 KB |
Output is correct |
21 |
Correct |
3 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
3 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
2 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
464 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
2 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
428 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
2 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
432 KB |
Output is correct |
21 |
Correct |
3 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
3 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
2 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
464 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
2 ms |
464 KB |
Output is correct |
36 |
Correct |
55 ms |
7108 KB |
Output is correct |
37 |
Correct |
91 ms |
10876 KB |
Output is correct |
38 |
Correct |
90 ms |
11028 KB |
Output is correct |
39 |
Correct |
85 ms |
10944 KB |
Output is correct |
40 |
Correct |
96 ms |
10908 KB |
Output is correct |
41 |
Correct |
100 ms |
11336 KB |
Output is correct |
42 |
Correct |
92 ms |
10908 KB |
Output is correct |
43 |
Correct |
33 ms |
9280 KB |
Output is correct |
44 |
Correct |
22 ms |
9280 KB |
Output is correct |
45 |
Correct |
71 ms |
10936 KB |
Output is correct |
46 |
Correct |
76 ms |
10968 KB |
Output is correct |
47 |
Correct |
101 ms |
10916 KB |
Output is correct |
48 |
Correct |
110 ms |
10924 KB |
Output is correct |
49 |
Correct |
89 ms |
11060 KB |
Output is correct |
50 |
Correct |
31 ms |
9256 KB |
Output is correct |
51 |
Correct |
75 ms |
10944 KB |
Output is correct |
52 |
Correct |
110 ms |
12036 KB |
Output is correct |
53 |
Correct |
112 ms |
12784 KB |
Output is correct |
54 |
Correct |
115 ms |
12764 KB |
Output is correct |
55 |
Correct |
21 ms |
9256 KB |
Output is correct |
56 |
Correct |
74 ms |
10908 KB |
Output is correct |
57 |
Correct |
111 ms |
10676 KB |
Output is correct |
58 |
Correct |
115 ms |
11296 KB |
Output is correct |
59 |
Correct |
136 ms |
11552 KB |
Output is correct |
60 |
Correct |
127 ms |
12816 KB |
Output is correct |
61 |
Correct |
137 ms |
12504 KB |
Output is correct |
62 |
Correct |
120 ms |
11932 KB |
Output is correct |
63 |
Correct |
115 ms |
12388 KB |
Output is correct |
64 |
Correct |
23 ms |
9244 KB |
Output is correct |
65 |
Correct |
24 ms |
9384 KB |
Output is correct |
66 |
Correct |
84 ms |
11044 KB |
Output is correct |
67 |
Correct |
82 ms |
10968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4032 ms |
12760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4014 ms |
3500 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
428 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
2 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
432 KB |
Output is correct |
21 |
Correct |
3 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
3 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
2 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
464 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
2 ms |
464 KB |
Output is correct |
36 |
Correct |
55 ms |
7108 KB |
Output is correct |
37 |
Correct |
91 ms |
10876 KB |
Output is correct |
38 |
Correct |
90 ms |
11028 KB |
Output is correct |
39 |
Correct |
85 ms |
10944 KB |
Output is correct |
40 |
Correct |
96 ms |
10908 KB |
Output is correct |
41 |
Correct |
100 ms |
11336 KB |
Output is correct |
42 |
Correct |
92 ms |
10908 KB |
Output is correct |
43 |
Correct |
33 ms |
9280 KB |
Output is correct |
44 |
Correct |
22 ms |
9280 KB |
Output is correct |
45 |
Correct |
71 ms |
10936 KB |
Output is correct |
46 |
Correct |
76 ms |
10968 KB |
Output is correct |
47 |
Correct |
101 ms |
10916 KB |
Output is correct |
48 |
Correct |
110 ms |
10924 KB |
Output is correct |
49 |
Correct |
89 ms |
11060 KB |
Output is correct |
50 |
Correct |
31 ms |
9256 KB |
Output is correct |
51 |
Correct |
75 ms |
10944 KB |
Output is correct |
52 |
Correct |
110 ms |
12036 KB |
Output is correct |
53 |
Correct |
112 ms |
12784 KB |
Output is correct |
54 |
Correct |
115 ms |
12764 KB |
Output is correct |
55 |
Correct |
21 ms |
9256 KB |
Output is correct |
56 |
Correct |
74 ms |
10908 KB |
Output is correct |
57 |
Correct |
111 ms |
10676 KB |
Output is correct |
58 |
Correct |
115 ms |
11296 KB |
Output is correct |
59 |
Correct |
136 ms |
11552 KB |
Output is correct |
60 |
Correct |
127 ms |
12816 KB |
Output is correct |
61 |
Correct |
137 ms |
12504 KB |
Output is correct |
62 |
Correct |
120 ms |
11932 KB |
Output is correct |
63 |
Correct |
115 ms |
12388 KB |
Output is correct |
64 |
Correct |
23 ms |
9244 KB |
Output is correct |
65 |
Correct |
24 ms |
9384 KB |
Output is correct |
66 |
Correct |
84 ms |
11044 KB |
Output is correct |
67 |
Correct |
82 ms |
10968 KB |
Output is correct |
68 |
Execution timed out |
4032 ms |
12760 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
407 ms |
5676 KB |
Output is correct |
2 |
Correct |
908 ms |
9276 KB |
Output is correct |
3 |
Correct |
832 ms |
9328 KB |
Output is correct |
4 |
Correct |
895 ms |
9264 KB |
Output is correct |
5 |
Correct |
744 ms |
9264 KB |
Output is correct |
6 |
Correct |
829 ms |
9296 KB |
Output is correct |
7 |
Correct |
698 ms |
9288 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
3 ms |
428 KB |
Output is correct |
13 |
Correct |
2 ms |
464 KB |
Output is correct |
14 |
Correct |
2 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
208 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
2 ms |
464 KB |
Output is correct |
26 |
Correct |
2 ms |
432 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
2 ms |
432 KB |
Output is correct |
31 |
Correct |
3 ms |
464 KB |
Output is correct |
32 |
Correct |
2 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
464 KB |
Output is correct |
37 |
Correct |
2 ms |
464 KB |
Output is correct |
38 |
Correct |
3 ms |
464 KB |
Output is correct |
39 |
Correct |
2 ms |
464 KB |
Output is correct |
40 |
Correct |
2 ms |
464 KB |
Output is correct |
41 |
Correct |
2 ms |
464 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
464 KB |
Output is correct |
44 |
Correct |
2 ms |
464 KB |
Output is correct |
45 |
Correct |
2 ms |
464 KB |
Output is correct |
46 |
Correct |
55 ms |
7108 KB |
Output is correct |
47 |
Correct |
91 ms |
10876 KB |
Output is correct |
48 |
Correct |
90 ms |
11028 KB |
Output is correct |
49 |
Correct |
85 ms |
10944 KB |
Output is correct |
50 |
Correct |
96 ms |
10908 KB |
Output is correct |
51 |
Correct |
100 ms |
11336 KB |
Output is correct |
52 |
Correct |
92 ms |
10908 KB |
Output is correct |
53 |
Correct |
33 ms |
9280 KB |
Output is correct |
54 |
Correct |
22 ms |
9280 KB |
Output is correct |
55 |
Correct |
71 ms |
10936 KB |
Output is correct |
56 |
Correct |
76 ms |
10968 KB |
Output is correct |
57 |
Correct |
101 ms |
10916 KB |
Output is correct |
58 |
Correct |
110 ms |
10924 KB |
Output is correct |
59 |
Correct |
89 ms |
11060 KB |
Output is correct |
60 |
Correct |
31 ms |
9256 KB |
Output is correct |
61 |
Correct |
75 ms |
10944 KB |
Output is correct |
62 |
Correct |
110 ms |
12036 KB |
Output is correct |
63 |
Correct |
112 ms |
12784 KB |
Output is correct |
64 |
Correct |
115 ms |
12764 KB |
Output is correct |
65 |
Correct |
21 ms |
9256 KB |
Output is correct |
66 |
Correct |
74 ms |
10908 KB |
Output is correct |
67 |
Correct |
111 ms |
10676 KB |
Output is correct |
68 |
Correct |
115 ms |
11296 KB |
Output is correct |
69 |
Correct |
136 ms |
11552 KB |
Output is correct |
70 |
Correct |
127 ms |
12816 KB |
Output is correct |
71 |
Correct |
137 ms |
12504 KB |
Output is correct |
72 |
Correct |
120 ms |
11932 KB |
Output is correct |
73 |
Correct |
115 ms |
12388 KB |
Output is correct |
74 |
Correct |
23 ms |
9244 KB |
Output is correct |
75 |
Correct |
24 ms |
9384 KB |
Output is correct |
76 |
Correct |
84 ms |
11044 KB |
Output is correct |
77 |
Correct |
82 ms |
10968 KB |
Output is correct |
78 |
Execution timed out |
4032 ms |
12760 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |