#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> arr;
int sort_index[101010];
struct maxseg {
int tree[404040];
vector<int> initarr;
void init(int v, int st, int ed) {
if (st == ed) {
tree[v] = initarr[st];
return;
}
int mid = (st + ed) / 2;
init(2 * v, st, mid);
init(2 * v + 1, mid + 1, ed);
tree[v] = max(tree[2 * v], tree[2 * v + 1]);
}
void update(int v, int st, int ed, int idx, int val) {
if (idx < st || idx > ed) return;
if (idx == st && idx == ed) {
tree[v] = val;
return;
}
int mid = (st + ed) / 2;
update(2 * v, st, mid, idx, val);
update(2 * v + 1, mid + 1, ed, idx, val);
tree[v] = max(tree[2 * v], tree[2 * v + 1]);
}
int get(int v, int st, int ed, int gs, int ge) {
if (st > ge || ed < gs) return -1;
if (gs <= st && ed <= ge) return tree[v];
int mid = (st + ed) / 2;
return max(get(2 * v, st, mid, gs, ge), get(2 * v + 1, mid + 1, ed, gs, ge));
}
} max_value, left_index;
struct minseg {
int tree[404040];
vector<int> initarr;
void init(int v, int st, int ed) {
if (st == ed) {
tree[v] = initarr[st];
return;
}
int mid = (st + ed) / 2;
init(2 * v, st, mid);
init(2 * v + 1, mid + 1, ed);
tree[v] = min(tree[2 * v], tree[2 * v + 1]);
}
void update(int v, int st, int ed, int idx, int val) {
if (idx < st || idx > ed) return;
if (idx == st && idx == ed) {
tree[v] = val;
return;
}
int mid = (st + ed) / 2;
update(2 * v, st, mid, idx, val);
update(2 * v + 1, mid + 1, ed, idx, val);
tree[v] = min(tree[2 * v], tree[2 * v + 1]);
}
int get(int v, int st, int ed, int gs, int ge) {
if (st > ge || ed < gs) return n;
if (gs <= st && ed <= ge) return tree[v];
int mid = (st + ed) / 2;
return min(get(2 * v, st, mid, gs, ge), get(2 * v + 1, mid + 1, ed, gs, ge));
}
} right_index;
void init(int N, vector<int> H) {
n = N;
vector< pair<int, int> > temp;
for (int i = 0; i < N; i++) arr.push_back(H[i]);
for (int i = 0; i < N; i++) temp.push_back({H[i], i});
sort(temp.begin(), temp.end());
for (int i = 0; i < N; i++) {
sort_index[i] = temp[i].second;
}
max_value.initarr = arr;
right_index.initarr = vector<int>(n, n);
left_index.initarr = vector<int>(n, -1);
max_value.init(1, 0, n - 1);
}
int max_towers(int L, int R, int D) {
int ans = 0;
left_index.init(1, 0, n - 1);
right_index.init(1, 0, n - 1);
for (int i = 0; i < n; i++) {
if (sort_index[i] < L || sort_index[i] > R) continue;
bool f = true;
int j = right_index.get(1, 0, n - 1, sort_index[i] + 1, R);
//cout << sort_index[i] << " " << j << "\n";
if (j >= 0 && j < n)
if (max_value.get(1, 0, n - 1, sort_index[i] + 1, j - 1) < max(arr[sort_index[i]], arr[j]) + D)
f = false;
j = left_index.get(1, 0, n - 1, L, sort_index[i] - 1);
//cout << j << " " << sort_index[i] << "\n";
if (j >= 0 && j < n)
if (max_value.get(1, 0, n - 1, j + 1, sort_index[i] - 1) < max(arr[sort_index[i]], arr[j]) + D)
f = false;
if (f) {
ans++;
left_index.update(1, 0, n - 1, sort_index[i], sort_index[i]);
right_index.update(1, 0, n - 1, sort_index[i], sort_index[i]);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4030 ms |
3316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
3 ms |
456 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
452 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
3 ms |
456 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
452 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
35 ms |
3384 KB |
Output is correct |
37 |
Correct |
60 ms |
5740 KB |
Output is correct |
38 |
Correct |
38 ms |
5660 KB |
Output is correct |
39 |
Correct |
59 ms |
5740 KB |
Output is correct |
40 |
Correct |
36 ms |
5744 KB |
Output is correct |
41 |
Correct |
123 ms |
5664 KB |
Output is correct |
42 |
Correct |
29 ms |
5668 KB |
Output is correct |
43 |
Correct |
46 ms |
5736 KB |
Output is correct |
44 |
Correct |
30 ms |
5676 KB |
Output is correct |
45 |
Correct |
23 ms |
5788 KB |
Output is correct |
46 |
Correct |
20 ms |
5660 KB |
Output is correct |
47 |
Correct |
59 ms |
5724 KB |
Output is correct |
48 |
Correct |
46 ms |
5744 KB |
Output is correct |
49 |
Correct |
33 ms |
5724 KB |
Output is correct |
50 |
Correct |
40 ms |
5668 KB |
Output is correct |
51 |
Correct |
48 ms |
5668 KB |
Output is correct |
52 |
Correct |
105 ms |
5664 KB |
Output is correct |
53 |
Correct |
130 ms |
5740 KB |
Output is correct |
54 |
Correct |
117 ms |
5740 KB |
Output is correct |
55 |
Correct |
59 ms |
5744 KB |
Output is correct |
56 |
Correct |
68 ms |
5744 KB |
Output is correct |
57 |
Correct |
94 ms |
5572 KB |
Output is correct |
58 |
Correct |
109 ms |
5660 KB |
Output is correct |
59 |
Correct |
101 ms |
5780 KB |
Output is correct |
60 |
Correct |
131 ms |
5744 KB |
Output is correct |
61 |
Correct |
106 ms |
5748 KB |
Output is correct |
62 |
Correct |
104 ms |
5664 KB |
Output is correct |
63 |
Correct |
106 ms |
5748 KB |
Output is correct |
64 |
Correct |
53 ms |
5704 KB |
Output is correct |
65 |
Correct |
56 ms |
5660 KB |
Output is correct |
66 |
Correct |
71 ms |
5656 KB |
Output is correct |
67 |
Correct |
52 ms |
5648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4086 ms |
5724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4011 ms |
1744 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
3 ms |
456 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
452 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
35 ms |
3384 KB |
Output is correct |
37 |
Correct |
60 ms |
5740 KB |
Output is correct |
38 |
Correct |
38 ms |
5660 KB |
Output is correct |
39 |
Correct |
59 ms |
5740 KB |
Output is correct |
40 |
Correct |
36 ms |
5744 KB |
Output is correct |
41 |
Correct |
123 ms |
5664 KB |
Output is correct |
42 |
Correct |
29 ms |
5668 KB |
Output is correct |
43 |
Correct |
46 ms |
5736 KB |
Output is correct |
44 |
Correct |
30 ms |
5676 KB |
Output is correct |
45 |
Correct |
23 ms |
5788 KB |
Output is correct |
46 |
Correct |
20 ms |
5660 KB |
Output is correct |
47 |
Correct |
59 ms |
5724 KB |
Output is correct |
48 |
Correct |
46 ms |
5744 KB |
Output is correct |
49 |
Correct |
33 ms |
5724 KB |
Output is correct |
50 |
Correct |
40 ms |
5668 KB |
Output is correct |
51 |
Correct |
48 ms |
5668 KB |
Output is correct |
52 |
Correct |
105 ms |
5664 KB |
Output is correct |
53 |
Correct |
130 ms |
5740 KB |
Output is correct |
54 |
Correct |
117 ms |
5740 KB |
Output is correct |
55 |
Correct |
59 ms |
5744 KB |
Output is correct |
56 |
Correct |
68 ms |
5744 KB |
Output is correct |
57 |
Correct |
94 ms |
5572 KB |
Output is correct |
58 |
Correct |
109 ms |
5660 KB |
Output is correct |
59 |
Correct |
101 ms |
5780 KB |
Output is correct |
60 |
Correct |
131 ms |
5744 KB |
Output is correct |
61 |
Correct |
106 ms |
5748 KB |
Output is correct |
62 |
Correct |
104 ms |
5664 KB |
Output is correct |
63 |
Correct |
106 ms |
5748 KB |
Output is correct |
64 |
Correct |
53 ms |
5704 KB |
Output is correct |
65 |
Correct |
56 ms |
5660 KB |
Output is correct |
66 |
Correct |
71 ms |
5656 KB |
Output is correct |
67 |
Correct |
52 ms |
5648 KB |
Output is correct |
68 |
Execution timed out |
4086 ms |
5724 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4030 ms |
3316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |