#include "towers.h"
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector<int> H;
const int MX_N = 1e5, LG = 17;
int rmq[1+LG][MX_N];
int p2[MX_N];
int rangeMin(int l, int r) {
int p = p2[r-l+1];
return min(rmq[p][l], rmq[p][r-(1<<p)+1]);
}
void init(int N, vector<int> Hs) {
for (int i = 2; i <= N; ++i) p2[i] = p2[i-1] + !(i & (i-1));
n = N, H = Hs;
// rmq
for (int i = 0; i < n; ++i) rmq[0][i] = H[i];
for (int i = 1, p = 1; i <= LG; ++i, p <<= 1) {
for (int j = 0; j < n; ++j) {
if (j+p >= n) rmq[i][j] = rmq[i-1][j];
else rmq[i][j] = min(rmq[i-1][j], rmq[i-1][j+p]);
}
}
}
vector<pair<int, int>> blocks;
int nxt[1+LG][1+MX_N]; // binary lifting - next block
void init2(int D) {
vector<pair<int, int>> b;
for (int i = 0; i < n; ++i) {
int L, R;
{ // find closest before
int left = -1, right = i;
while (left+1 < right) {
int mid = (left + right) / 2;
if (rangeMin(mid, i-1) <= H[i]-D) left = mid;
else right = mid;
}
L = left;
}
{ // find closest after
int left = i, right = n;
while (left+1 < right) {
int mid = (left + right) / 2;
if (rangeMin(i+1, mid) <= H[i]-D) right = mid;
else left = mid;
}
R = right;
}
if (0 <= L && R < n) b.emplace_back(L, R);
}
// erase blocks with other blocks inside them
sort(b.begin(), b.end());
for (int i = (int)b.size()-1, minEnd = n; i >= 0; --i) {
if (b[i].second < minEnd) minEnd = b[i].second, blocks.push_back(b[i]);
}
reverse(blocks.begin(), blocks.end());
// find next block for each block
for (size_t i = 0, j = 0; i < blocks.size(); ++i) {
while (j < blocks.size() && blocks[j].first < blocks[i].second) ++j;
nxt[0][i] = j;
}
nxt[0][blocks.size()] = blocks.size();
// build binary lifting
for (int i = 1; i <= LG; ++i) {
for (int j = 0; j <= (int)blocks.size(); ++j) nxt[i][j] = nxt[i-1][nxt[i-1][j]];
}
}
bool hadQuery = false;
int max_towers(int L, int R, int D) { // case: constant D
if (!hadQuery) init2(D), hadQuery = true;
// else return 0;
int firstBlock = lower_bound(blocks.begin(), blocks.end(), pair<int, int>{L, 0}) - blocks.begin();
if (firstBlock == (int)blocks.size() || blocks[firstBlock].second > R) return 1;
int moves = 0;
for (int i = firstBlock; i < (int)blocks.size() && blocks[i].second <= R; ++i) {
if (blocks[firstBlock].second <= blocks[i].first) {
firstBlock = i;
++moves;
}
}
// // find furthest descendant of firstBlock that ends before R
// int lastBlock = firstBlock;
// int moves = 0;
// for (int i = LG; i >= 0; --i) {
// int b = nxt[i][lastBlock];
// if (b < (int)blocks.size() && blocks[b].second <= R) lastBlock = b, moves += 1 << i;
// }
return moves + 2; // moves + 1 blocks, so moves + 2 gaps between and around blocks
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
322 ms |
5988 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
720 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
720 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
18 ms |
8472 KB |
Output is correct |
37 |
Correct |
30 ms |
9632 KB |
Output is correct |
38 |
Correct |
34 ms |
11304 KB |
Output is correct |
39 |
Correct |
21 ms |
8904 KB |
Output is correct |
40 |
Correct |
31 ms |
13020 KB |
Output is correct |
41 |
Correct |
25 ms |
11180 KB |
Output is correct |
42 |
Correct |
28 ms |
13752 KB |
Output is correct |
43 |
Correct |
21 ms |
8848 KB |
Output is correct |
44 |
Correct |
21 ms |
8880 KB |
Output is correct |
45 |
Correct |
25 ms |
8912 KB |
Output is correct |
46 |
Correct |
23 ms |
9244 KB |
Output is correct |
47 |
Correct |
31 ms |
13184 KB |
Output is correct |
48 |
Correct |
35 ms |
13744 KB |
Output is correct |
49 |
Correct |
32 ms |
13784 KB |
Output is correct |
50 |
Correct |
20 ms |
8868 KB |
Output is correct |
51 |
Correct |
23 ms |
8868 KB |
Output is correct |
52 |
Correct |
29 ms |
13208 KB |
Output is correct |
53 |
Correct |
30 ms |
13760 KB |
Output is correct |
54 |
Correct |
29 ms |
13740 KB |
Output is correct |
55 |
Correct |
21 ms |
8776 KB |
Output is correct |
56 |
Correct |
24 ms |
9480 KB |
Output is correct |
57 |
Correct |
27 ms |
10032 KB |
Output is correct |
58 |
Correct |
28 ms |
10928 KB |
Output is correct |
59 |
Correct |
29 ms |
11604 KB |
Output is correct |
60 |
Correct |
32 ms |
13728 KB |
Output is correct |
61 |
Correct |
30 ms |
13132 KB |
Output is correct |
62 |
Correct |
27 ms |
11748 KB |
Output is correct |
63 |
Correct |
30 ms |
13200 KB |
Output is correct |
64 |
Correct |
21 ms |
8912 KB |
Output is correct |
65 |
Correct |
23 ms |
8860 KB |
Output is correct |
66 |
Correct |
24 ms |
8992 KB |
Output is correct |
67 |
Correct |
26 ms |
8896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3086 ms |
13240 KB |
Output is correct |
2 |
Execution timed out |
4014 ms |
13244 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
622 ms |
3404 KB |
2nd lines differ - on the 1st token, expected: '7063', found: '7197' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
720 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
18 ms |
8472 KB |
Output is correct |
37 |
Correct |
30 ms |
9632 KB |
Output is correct |
38 |
Correct |
34 ms |
11304 KB |
Output is correct |
39 |
Correct |
21 ms |
8904 KB |
Output is correct |
40 |
Correct |
31 ms |
13020 KB |
Output is correct |
41 |
Correct |
25 ms |
11180 KB |
Output is correct |
42 |
Correct |
28 ms |
13752 KB |
Output is correct |
43 |
Correct |
21 ms |
8848 KB |
Output is correct |
44 |
Correct |
21 ms |
8880 KB |
Output is correct |
45 |
Correct |
25 ms |
8912 KB |
Output is correct |
46 |
Correct |
23 ms |
9244 KB |
Output is correct |
47 |
Correct |
31 ms |
13184 KB |
Output is correct |
48 |
Correct |
35 ms |
13744 KB |
Output is correct |
49 |
Correct |
32 ms |
13784 KB |
Output is correct |
50 |
Correct |
20 ms |
8868 KB |
Output is correct |
51 |
Correct |
23 ms |
8868 KB |
Output is correct |
52 |
Correct |
29 ms |
13208 KB |
Output is correct |
53 |
Correct |
30 ms |
13760 KB |
Output is correct |
54 |
Correct |
29 ms |
13740 KB |
Output is correct |
55 |
Correct |
21 ms |
8776 KB |
Output is correct |
56 |
Correct |
24 ms |
9480 KB |
Output is correct |
57 |
Correct |
27 ms |
10032 KB |
Output is correct |
58 |
Correct |
28 ms |
10928 KB |
Output is correct |
59 |
Correct |
29 ms |
11604 KB |
Output is correct |
60 |
Correct |
32 ms |
13728 KB |
Output is correct |
61 |
Correct |
30 ms |
13132 KB |
Output is correct |
62 |
Correct |
27 ms |
11748 KB |
Output is correct |
63 |
Correct |
30 ms |
13200 KB |
Output is correct |
64 |
Correct |
21 ms |
8912 KB |
Output is correct |
65 |
Correct |
23 ms |
8860 KB |
Output is correct |
66 |
Correct |
24 ms |
8992 KB |
Output is correct |
67 |
Correct |
26 ms |
8896 KB |
Output is correct |
68 |
Correct |
3086 ms |
13240 KB |
Output is correct |
69 |
Execution timed out |
4014 ms |
13244 KB |
Time limit exceeded |
70 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
322 ms |
5988 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |