#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;
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;
// 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 |
333 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 |
0 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 |
844 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 |
644 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 |
0 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 |
844 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 |
644 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 |
22 ms |
8396 KB |
Output is correct |
37 |
Correct |
26 ms |
9576 KB |
Output is correct |
38 |
Correct |
27 ms |
11332 KB |
Output is correct |
39 |
Correct |
22 ms |
8912 KB |
Output is correct |
40 |
Correct |
28 ms |
13068 KB |
Output is correct |
41 |
Correct |
28 ms |
11204 KB |
Output is correct |
42 |
Correct |
33 ms |
13756 KB |
Output is correct |
43 |
Correct |
22 ms |
8912 KB |
Output is correct |
44 |
Correct |
21 ms |
8804 KB |
Output is correct |
45 |
Correct |
20 ms |
8776 KB |
Output is correct |
46 |
Correct |
23 ms |
9152 KB |
Output is correct |
47 |
Correct |
33 ms |
13252 KB |
Output is correct |
48 |
Correct |
27 ms |
13696 KB |
Output is correct |
49 |
Correct |
27 ms |
13796 KB |
Output is correct |
50 |
Correct |
19 ms |
8784 KB |
Output is correct |
51 |
Correct |
25 ms |
8912 KB |
Output is correct |
52 |
Correct |
34 ms |
13200 KB |
Output is correct |
53 |
Correct |
28 ms |
13756 KB |
Output is correct |
54 |
Correct |
28 ms |
13684 KB |
Output is correct |
55 |
Correct |
25 ms |
8912 KB |
Output is correct |
56 |
Correct |
23 ms |
9496 KB |
Output is correct |
57 |
Correct |
30 ms |
10068 KB |
Output is correct |
58 |
Correct |
27 ms |
10944 KB |
Output is correct |
59 |
Correct |
32 ms |
11580 KB |
Output is correct |
60 |
Correct |
30 ms |
13736 KB |
Output is correct |
61 |
Correct |
28 ms |
13124 KB |
Output is correct |
62 |
Correct |
26 ms |
11708 KB |
Output is correct |
63 |
Correct |
28 ms |
13124 KB |
Output is correct |
64 |
Correct |
26 ms |
8872 KB |
Output is correct |
65 |
Correct |
26 ms |
8960 KB |
Output is correct |
66 |
Correct |
23 ms |
8912 KB |
Output is correct |
67 |
Correct |
21 ms |
8912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
629 ms |
13256 KB |
Output is correct |
2 |
Correct |
672 ms |
13244 KB |
Output is correct |
3 |
Correct |
791 ms |
13252 KB |
Output is correct |
4 |
Correct |
690 ms |
13756 KB |
Output is correct |
5 |
Correct |
731 ms |
13764 KB |
Output is correct |
6 |
Correct |
874 ms |
13756 KB |
Output is correct |
7 |
Correct |
754 ms |
13880 KB |
Output is correct |
8 |
Correct |
635 ms |
8784 KB |
Output is correct |
9 |
Correct |
770 ms |
8784 KB |
Output is correct |
10 |
Correct |
651 ms |
8984 KB |
Output is correct |
11 |
Correct |
562 ms |
13896 KB |
Output is correct |
12 |
Correct |
684 ms |
10084 KB |
Output is correct |
13 |
Correct |
707 ms |
8912 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
28 ms |
13244 KB |
Output is correct |
18 |
Correct |
33 ms |
13740 KB |
Output is correct |
19 |
Correct |
27 ms |
13752 KB |
Output is correct |
20 |
Correct |
20 ms |
8912 KB |
Output is correct |
21 |
Correct |
24 ms |
8912 KB |
Output is correct |
22 |
Correct |
30 ms |
13284 KB |
Output is correct |
23 |
Correct |
28 ms |
13736 KB |
Output is correct |
24 |
Correct |
33 ms |
13756 KB |
Output is correct |
25 |
Correct |
20 ms |
8868 KB |
Output is correct |
26 |
Correct |
25 ms |
9668 KB |
Output is correct |
27 |
Correct |
1 ms |
720 KB |
Output is correct |
28 |
Correct |
1 ms |
720 KB |
Output is correct |
29 |
Correct |
1 ms |
720 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 |
720 KB |
Output is correct |
34 |
Correct |
1 ms |
720 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
178 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 |
0 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 |
844 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 |
644 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 |
22 ms |
8396 KB |
Output is correct |
37 |
Correct |
26 ms |
9576 KB |
Output is correct |
38 |
Correct |
27 ms |
11332 KB |
Output is correct |
39 |
Correct |
22 ms |
8912 KB |
Output is correct |
40 |
Correct |
28 ms |
13068 KB |
Output is correct |
41 |
Correct |
28 ms |
11204 KB |
Output is correct |
42 |
Correct |
33 ms |
13756 KB |
Output is correct |
43 |
Correct |
22 ms |
8912 KB |
Output is correct |
44 |
Correct |
21 ms |
8804 KB |
Output is correct |
45 |
Correct |
20 ms |
8776 KB |
Output is correct |
46 |
Correct |
23 ms |
9152 KB |
Output is correct |
47 |
Correct |
33 ms |
13252 KB |
Output is correct |
48 |
Correct |
27 ms |
13696 KB |
Output is correct |
49 |
Correct |
27 ms |
13796 KB |
Output is correct |
50 |
Correct |
19 ms |
8784 KB |
Output is correct |
51 |
Correct |
25 ms |
8912 KB |
Output is correct |
52 |
Correct |
34 ms |
13200 KB |
Output is correct |
53 |
Correct |
28 ms |
13756 KB |
Output is correct |
54 |
Correct |
28 ms |
13684 KB |
Output is correct |
55 |
Correct |
25 ms |
8912 KB |
Output is correct |
56 |
Correct |
23 ms |
9496 KB |
Output is correct |
57 |
Correct |
30 ms |
10068 KB |
Output is correct |
58 |
Correct |
27 ms |
10944 KB |
Output is correct |
59 |
Correct |
32 ms |
11580 KB |
Output is correct |
60 |
Correct |
30 ms |
13736 KB |
Output is correct |
61 |
Correct |
28 ms |
13124 KB |
Output is correct |
62 |
Correct |
26 ms |
11708 KB |
Output is correct |
63 |
Correct |
28 ms |
13124 KB |
Output is correct |
64 |
Correct |
26 ms |
8872 KB |
Output is correct |
65 |
Correct |
26 ms |
8960 KB |
Output is correct |
66 |
Correct |
23 ms |
8912 KB |
Output is correct |
67 |
Correct |
21 ms |
8912 KB |
Output is correct |
68 |
Correct |
629 ms |
13256 KB |
Output is correct |
69 |
Correct |
672 ms |
13244 KB |
Output is correct |
70 |
Correct |
791 ms |
13252 KB |
Output is correct |
71 |
Correct |
690 ms |
13756 KB |
Output is correct |
72 |
Correct |
731 ms |
13764 KB |
Output is correct |
73 |
Correct |
874 ms |
13756 KB |
Output is correct |
74 |
Correct |
754 ms |
13880 KB |
Output is correct |
75 |
Correct |
635 ms |
8784 KB |
Output is correct |
76 |
Correct |
770 ms |
8784 KB |
Output is correct |
77 |
Correct |
651 ms |
8984 KB |
Output is correct |
78 |
Correct |
562 ms |
13896 KB |
Output is correct |
79 |
Correct |
684 ms |
10084 KB |
Output is correct |
80 |
Correct |
707 ms |
8912 KB |
Output is correct |
81 |
Correct |
1 ms |
336 KB |
Output is correct |
82 |
Correct |
1 ms |
592 KB |
Output is correct |
83 |
Correct |
1 ms |
592 KB |
Output is correct |
84 |
Correct |
28 ms |
13244 KB |
Output is correct |
85 |
Correct |
33 ms |
13740 KB |
Output is correct |
86 |
Correct |
27 ms |
13752 KB |
Output is correct |
87 |
Correct |
20 ms |
8912 KB |
Output is correct |
88 |
Correct |
24 ms |
8912 KB |
Output is correct |
89 |
Correct |
30 ms |
13284 KB |
Output is correct |
90 |
Correct |
28 ms |
13736 KB |
Output is correct |
91 |
Correct |
33 ms |
13756 KB |
Output is correct |
92 |
Correct |
20 ms |
8868 KB |
Output is correct |
93 |
Correct |
25 ms |
9668 KB |
Output is correct |
94 |
Correct |
1 ms |
720 KB |
Output is correct |
95 |
Correct |
1 ms |
720 KB |
Output is correct |
96 |
Correct |
1 ms |
720 KB |
Output is correct |
97 |
Correct |
1 ms |
592 KB |
Output is correct |
98 |
Correct |
1 ms |
592 KB |
Output is correct |
99 |
Correct |
1 ms |
592 KB |
Output is correct |
100 |
Correct |
1 ms |
720 KB |
Output is correct |
101 |
Correct |
1 ms |
720 KB |
Output is correct |
102 |
Correct |
1 ms |
592 KB |
Output is correct |
103 |
Correct |
1 ms |
592 KB |
Output is correct |
104 |
Correct |
600 ms |
10992 KB |
Output is correct |
105 |
Correct |
792 ms |
11588 KB |
Output is correct |
106 |
Correct |
700 ms |
10568 KB |
Output is correct |
107 |
Correct |
672 ms |
12216 KB |
Output is correct |
108 |
Correct |
847 ms |
9008 KB |
Output is correct |
109 |
Correct |
631 ms |
11720 KB |
Output is correct |
110 |
Correct |
847 ms |
13760 KB |
Output is correct |
111 |
Correct |
561 ms |
8784 KB |
Output is correct |
112 |
Correct |
853 ms |
8912 KB |
Output is correct |
113 |
Correct |
729 ms |
9008 KB |
Output is correct |
114 |
Correct |
676 ms |
10036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
333 ms |
5988 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |