#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
#define bit(x, i) ((x >> i) & 1)
template<typename T>
using vec = vector<T>;
vec<int> h_;
int n_;
int peak;
void init(int N, std::vector<int> H) {
h_ = H;
n_ = N;
peak = max_element(h_.begin(), h_.end()) - h_.begin();
}
template<typename it>
struct SparseTable {
using T = typename remove_reference<decltype(*declval<it>())>::type;
vector<vector<T>> t;
function<T(T, T)> f;
vector<int> log;
SparseTable() = default;
SparseTable(it first, it last, function<T(T, T)> op) : t(1), f(op) {
int n = distance(first, last);
t.assign(32 - __builtin_clz(n), vector<T>(n));
t[0].assign(first, last);
log.resize(n + 1);
for (int i = 2; i <= n; i++)
log[i] = log[i / 2] + 1;
for (int i = 1; i < t.size(); i++)
for (int j = 0; j < n - (1 << i) + 1; j++)
t[i][j] = f(t[i - 1][j], t[i - 1][j + (1 << (i - 1))]);
}
T get(int l, int r) {
int h = log[r - l + 1];
return f(t[h][l], t[h][r - (1 << h) + 1]);
}
};
const int maxN = 1e5 + 10;
int stree[4 * maxN];
void init() {
memset(stree, -1e9, sizeof stree);
}
void update(int v, int tl, int tr, int pos, int val) {
if (tl == tr) {
stree[v] = val;
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) update(2 * v, tl, tm, pos, val);
else update(2 * v + 1, tm + 1, tr, pos, val);
stree[v] = max(stree[2 * v], stree[2 * v + 1]);
}
int get(int v, int tl, int tr, int l, int r) {
if (l > r) return -1e9;
if (tl == l && tr == r) return stree[v];
int tm = (tl + tr) / 2;
return max(get(2 * v, tl, tm, l, min(r, tm)), get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
int cnt = 0;
int max_towers1(int L, int R, int D) {
if (L < peak and R > peak) {
if (h_[peak] >= max(h_[L], h_[R]) + D) {
return 2;
}
return 1;
}
return 1;
}
int max_towers2(int L, int R, int D) {
vec<int> h;
for (int i = L; i <= R; i++) h.push_back(h_[i]);
int n = h.size();
init();
SparseTable stmax(h.begin(), h.end(), [](int a, int b) { return max(a, b); });
vec<int> dp(n, 0);
multiset<pair<int, int>> ms;
for (int i = n - 1; i >= 0; i--) {
dp[i] = 1;
if (i == n - 1) {
ms.insert({h[i], i});
continue;
}
int tl = i + 1;
int tr = n - 1;
if (stmax.get(tl, tr) < h[i] + D) {
while (!ms.empty()) {
auto [val, idx] = *ms.begin();
if (h[i] < val + D) break;
ms.erase(ms.begin());
update(1, 0, n - 1, idx, dp[idx]);
}
ms.insert({h[i], i});
}
while (tl != tr) {
int tm = (tl + tr) / 2;
if (stmax.get(i + 1, tm) >= h[i] + D) tr = tm;
else tl = tm + 1;
}
dp[i] = max(dp[i], get(1, 0, n - 1, tl + 1, n - 1) + 1);
while (!ms.empty()) {
auto [val, idx] = *ms.begin();
if (h[i] < val + D) break;
ms.erase(ms.begin());
update(1, 0, n - 1, idx, dp[idx]);
}
ms.insert({h[i], i});
// int cur_max = h[i + 1];
// for (int j = i + 2; j < n; j++) {
// if (cur_max >= max(h[i], h[j]) + D) {
// dp[i] = max(dp[i], dp[j] + 1);
// }
// cur_max = max(cur_max, h[j]);
// }
}
return *max_element(dp.begin(), dp.end());
}
int max_towers(int L, int R, int D) {
if (cnt > 1) {
return max_towers1(L, R, D);
} else {
cnt++;
return max_towers2(L, R, D);
}
}
Compilation message
towers.cpp: In instantiation of 'SparseTable<it>::SparseTable(it, it, std::function<typename std::remove_reference<decltype (* declval<it>())>::type(typename std::remove_reference<decltype (* declval<it>())>::type, typename std::remove_reference<decltype (* declval<it>())>::type)>) [with it = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::remove_reference<decltype (* declval<it>())>::type = int]':
towers.cpp:96:81: required from here
towers.cpp:37:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int>, std::allocator<std::vector<int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 1; i < t.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
314 ms |
5484 KB |
Output is correct |
2 |
Correct |
635 ms |
11652 KB |
Output is correct |
3 |
Correct |
658 ms |
6608 KB |
Output is correct |
4 |
Correct |
673 ms |
8560 KB |
Output is correct |
5 |
Correct |
673 ms |
10448 KB |
Output is correct |
6 |
Correct |
584 ms |
7120 KB |
Output is correct |
7 |
Correct |
690 ms |
7712 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
2 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
2 ms |
1880 KB |
Output is correct |
7 |
Correct |
2 ms |
1880 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1880 KB |
Output is correct |
11 |
Correct |
1 ms |
1880 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1880 KB |
Output is correct |
14 |
Correct |
1 ms |
1880 KB |
Output is correct |
15 |
Correct |
1 ms |
1880 KB |
Output is correct |
16 |
Correct |
2 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1880 KB |
Output is correct |
18 |
Correct |
2 ms |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
1880 KB |
Output is correct |
20 |
Correct |
1 ms |
1880 KB |
Output is correct |
21 |
Correct |
2 ms |
1880 KB |
Output is correct |
22 |
Correct |
2 ms |
2136 KB |
Output is correct |
23 |
Correct |
2 ms |
1880 KB |
Output is correct |
24 |
Correct |
1 ms |
2024 KB |
Output is correct |
25 |
Correct |
2 ms |
1880 KB |
Output is correct |
26 |
Correct |
2 ms |
2268 KB |
Output is correct |
27 |
Correct |
2 ms |
2136 KB |
Output is correct |
28 |
Correct |
2 ms |
2136 KB |
Output is correct |
29 |
Correct |
2 ms |
2136 KB |
Output is correct |
30 |
Correct |
2 ms |
2136 KB |
Output is correct |
31 |
Correct |
2 ms |
2136 KB |
Output is correct |
32 |
Correct |
2 ms |
2136 KB |
Output is correct |
33 |
Correct |
2 ms |
2136 KB |
Output is correct |
34 |
Correct |
2 ms |
2136 KB |
Output is correct |
35 |
Correct |
2 ms |
2136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
2 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
2 ms |
1880 KB |
Output is correct |
7 |
Correct |
2 ms |
1880 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1880 KB |
Output is correct |
11 |
Correct |
1 ms |
1880 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1880 KB |
Output is correct |
14 |
Correct |
1 ms |
1880 KB |
Output is correct |
15 |
Correct |
1 ms |
1880 KB |
Output is correct |
16 |
Correct |
2 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1880 KB |
Output is correct |
18 |
Correct |
2 ms |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
1880 KB |
Output is correct |
20 |
Correct |
1 ms |
1880 KB |
Output is correct |
21 |
Correct |
2 ms |
1880 KB |
Output is correct |
22 |
Correct |
2 ms |
2136 KB |
Output is correct |
23 |
Correct |
2 ms |
1880 KB |
Output is correct |
24 |
Correct |
1 ms |
2024 KB |
Output is correct |
25 |
Correct |
2 ms |
1880 KB |
Output is correct |
26 |
Correct |
2 ms |
2268 KB |
Output is correct |
27 |
Correct |
2 ms |
2136 KB |
Output is correct |
28 |
Correct |
2 ms |
2136 KB |
Output is correct |
29 |
Correct |
2 ms |
2136 KB |
Output is correct |
30 |
Correct |
2 ms |
2136 KB |
Output is correct |
31 |
Correct |
2 ms |
2136 KB |
Output is correct |
32 |
Correct |
2 ms |
2136 KB |
Output is correct |
33 |
Correct |
2 ms |
2136 KB |
Output is correct |
34 |
Correct |
2 ms |
2136 KB |
Output is correct |
35 |
Correct |
2 ms |
2136 KB |
Output is correct |
36 |
Correct |
19 ms |
3928 KB |
Output is correct |
37 |
Correct |
32 ms |
7888 KB |
Output is correct |
38 |
Correct |
16 ms |
4048 KB |
Output is correct |
39 |
Correct |
27 ms |
7724 KB |
Output is correct |
40 |
Correct |
24 ms |
3792 KB |
Output is correct |
41 |
Correct |
59 ms |
13256 KB |
Output is correct |
42 |
Correct |
11 ms |
2768 KB |
Output is correct |
43 |
Correct |
37 ms |
9612 KB |
Output is correct |
44 |
Correct |
15 ms |
4844 KB |
Output is correct |
45 |
Correct |
10 ms |
3024 KB |
Output is correct |
46 |
Correct |
9 ms |
2752 KB |
Output is correct |
47 |
Correct |
24 ms |
5328 KB |
Output is correct |
48 |
Correct |
18 ms |
4304 KB |
Output is correct |
49 |
Correct |
12 ms |
3180 KB |
Output is correct |
50 |
Correct |
17 ms |
4364 KB |
Output is correct |
51 |
Correct |
30 ms |
7344 KB |
Output is correct |
52 |
Correct |
49 ms |
10448 KB |
Output is correct |
53 |
Correct |
62 ms |
10448 KB |
Output is correct |
54 |
Correct |
51 ms |
10448 KB |
Output is correct |
55 |
Correct |
43 ms |
10448 KB |
Output is correct |
56 |
Correct |
66 ms |
14792 KB |
Output is correct |
57 |
Correct |
69 ms |
15828 KB |
Output is correct |
58 |
Correct |
81 ms |
14536 KB |
Output is correct |
59 |
Correct |
60 ms |
12744 KB |
Output is correct |
60 |
Correct |
52 ms |
10696 KB |
Output is correct |
61 |
Correct |
58 ms |
12788 KB |
Output is correct |
62 |
Correct |
65 ms |
14344 KB |
Output is correct |
63 |
Correct |
62 ms |
12476 KB |
Output is correct |
64 |
Correct |
57 ms |
17608 KB |
Output is correct |
65 |
Correct |
59 ms |
15816 KB |
Output is correct |
66 |
Correct |
59 ms |
15560 KB |
Output is correct |
67 |
Correct |
64 ms |
15288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
447 ms |
5332 KB |
3rd lines differ - on the 1st token, expected: '14278', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
196 ms |
4088 KB |
3rd lines differ - on the 1st token, expected: '150', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
2 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
2 ms |
1880 KB |
Output is correct |
7 |
Correct |
2 ms |
1880 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1880 KB |
Output is correct |
11 |
Correct |
1 ms |
1880 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1880 KB |
Output is correct |
14 |
Correct |
1 ms |
1880 KB |
Output is correct |
15 |
Correct |
1 ms |
1880 KB |
Output is correct |
16 |
Correct |
2 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1880 KB |
Output is correct |
18 |
Correct |
2 ms |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
1880 KB |
Output is correct |
20 |
Correct |
1 ms |
1880 KB |
Output is correct |
21 |
Correct |
2 ms |
1880 KB |
Output is correct |
22 |
Correct |
2 ms |
2136 KB |
Output is correct |
23 |
Correct |
2 ms |
1880 KB |
Output is correct |
24 |
Correct |
1 ms |
2024 KB |
Output is correct |
25 |
Correct |
2 ms |
1880 KB |
Output is correct |
26 |
Correct |
2 ms |
2268 KB |
Output is correct |
27 |
Correct |
2 ms |
2136 KB |
Output is correct |
28 |
Correct |
2 ms |
2136 KB |
Output is correct |
29 |
Correct |
2 ms |
2136 KB |
Output is correct |
30 |
Correct |
2 ms |
2136 KB |
Output is correct |
31 |
Correct |
2 ms |
2136 KB |
Output is correct |
32 |
Correct |
2 ms |
2136 KB |
Output is correct |
33 |
Correct |
2 ms |
2136 KB |
Output is correct |
34 |
Correct |
2 ms |
2136 KB |
Output is correct |
35 |
Correct |
2 ms |
2136 KB |
Output is correct |
36 |
Correct |
19 ms |
3928 KB |
Output is correct |
37 |
Correct |
32 ms |
7888 KB |
Output is correct |
38 |
Correct |
16 ms |
4048 KB |
Output is correct |
39 |
Correct |
27 ms |
7724 KB |
Output is correct |
40 |
Correct |
24 ms |
3792 KB |
Output is correct |
41 |
Correct |
59 ms |
13256 KB |
Output is correct |
42 |
Correct |
11 ms |
2768 KB |
Output is correct |
43 |
Correct |
37 ms |
9612 KB |
Output is correct |
44 |
Correct |
15 ms |
4844 KB |
Output is correct |
45 |
Correct |
10 ms |
3024 KB |
Output is correct |
46 |
Correct |
9 ms |
2752 KB |
Output is correct |
47 |
Correct |
24 ms |
5328 KB |
Output is correct |
48 |
Correct |
18 ms |
4304 KB |
Output is correct |
49 |
Correct |
12 ms |
3180 KB |
Output is correct |
50 |
Correct |
17 ms |
4364 KB |
Output is correct |
51 |
Correct |
30 ms |
7344 KB |
Output is correct |
52 |
Correct |
49 ms |
10448 KB |
Output is correct |
53 |
Correct |
62 ms |
10448 KB |
Output is correct |
54 |
Correct |
51 ms |
10448 KB |
Output is correct |
55 |
Correct |
43 ms |
10448 KB |
Output is correct |
56 |
Correct |
66 ms |
14792 KB |
Output is correct |
57 |
Correct |
69 ms |
15828 KB |
Output is correct |
58 |
Correct |
81 ms |
14536 KB |
Output is correct |
59 |
Correct |
60 ms |
12744 KB |
Output is correct |
60 |
Correct |
52 ms |
10696 KB |
Output is correct |
61 |
Correct |
58 ms |
12788 KB |
Output is correct |
62 |
Correct |
65 ms |
14344 KB |
Output is correct |
63 |
Correct |
62 ms |
12476 KB |
Output is correct |
64 |
Correct |
57 ms |
17608 KB |
Output is correct |
65 |
Correct |
59 ms |
15816 KB |
Output is correct |
66 |
Correct |
59 ms |
15560 KB |
Output is correct |
67 |
Correct |
64 ms |
15288 KB |
Output is correct |
68 |
Incorrect |
447 ms |
5332 KB |
3rd lines differ - on the 1st token, expected: '14278', found: '2' |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
314 ms |
5484 KB |
Output is correct |
2 |
Correct |
635 ms |
11652 KB |
Output is correct |
3 |
Correct |
658 ms |
6608 KB |
Output is correct |
4 |
Correct |
673 ms |
8560 KB |
Output is correct |
5 |
Correct |
673 ms |
10448 KB |
Output is correct |
6 |
Correct |
584 ms |
7120 KB |
Output is correct |
7 |
Correct |
690 ms |
7712 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1880 KB |
Output is correct |
11 |
Correct |
1 ms |
1880 KB |
Output is correct |
12 |
Correct |
2 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1880 KB |
Output is correct |
14 |
Correct |
1 ms |
1880 KB |
Output is correct |
15 |
Correct |
1 ms |
1880 KB |
Output is correct |
16 |
Correct |
2 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1880 KB |
Output is correct |
18 |
Correct |
1 ms |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
1880 KB |
Output is correct |
20 |
Correct |
1 ms |
1880 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
1 ms |
1880 KB |
Output is correct |
23 |
Correct |
1 ms |
1880 KB |
Output is correct |
24 |
Correct |
1 ms |
1880 KB |
Output is correct |
25 |
Correct |
1 ms |
1880 KB |
Output is correct |
26 |
Correct |
2 ms |
1880 KB |
Output is correct |
27 |
Correct |
2 ms |
1880 KB |
Output is correct |
28 |
Correct |
2 ms |
1880 KB |
Output is correct |
29 |
Correct |
1 ms |
1880 KB |
Output is correct |
30 |
Correct |
1 ms |
1880 KB |
Output is correct |
31 |
Correct |
2 ms |
1880 KB |
Output is correct |
32 |
Correct |
2 ms |
2136 KB |
Output is correct |
33 |
Correct |
2 ms |
1880 KB |
Output is correct |
34 |
Correct |
1 ms |
2024 KB |
Output is correct |
35 |
Correct |
2 ms |
1880 KB |
Output is correct |
36 |
Correct |
2 ms |
2268 KB |
Output is correct |
37 |
Correct |
2 ms |
2136 KB |
Output is correct |
38 |
Correct |
2 ms |
2136 KB |
Output is correct |
39 |
Correct |
2 ms |
2136 KB |
Output is correct |
40 |
Correct |
2 ms |
2136 KB |
Output is correct |
41 |
Correct |
2 ms |
2136 KB |
Output is correct |
42 |
Correct |
2 ms |
2136 KB |
Output is correct |
43 |
Correct |
2 ms |
2136 KB |
Output is correct |
44 |
Correct |
2 ms |
2136 KB |
Output is correct |
45 |
Correct |
2 ms |
2136 KB |
Output is correct |
46 |
Correct |
19 ms |
3928 KB |
Output is correct |
47 |
Correct |
32 ms |
7888 KB |
Output is correct |
48 |
Correct |
16 ms |
4048 KB |
Output is correct |
49 |
Correct |
27 ms |
7724 KB |
Output is correct |
50 |
Correct |
24 ms |
3792 KB |
Output is correct |
51 |
Correct |
59 ms |
13256 KB |
Output is correct |
52 |
Correct |
11 ms |
2768 KB |
Output is correct |
53 |
Correct |
37 ms |
9612 KB |
Output is correct |
54 |
Correct |
15 ms |
4844 KB |
Output is correct |
55 |
Correct |
10 ms |
3024 KB |
Output is correct |
56 |
Correct |
9 ms |
2752 KB |
Output is correct |
57 |
Correct |
24 ms |
5328 KB |
Output is correct |
58 |
Correct |
18 ms |
4304 KB |
Output is correct |
59 |
Correct |
12 ms |
3180 KB |
Output is correct |
60 |
Correct |
17 ms |
4364 KB |
Output is correct |
61 |
Correct |
30 ms |
7344 KB |
Output is correct |
62 |
Correct |
49 ms |
10448 KB |
Output is correct |
63 |
Correct |
62 ms |
10448 KB |
Output is correct |
64 |
Correct |
51 ms |
10448 KB |
Output is correct |
65 |
Correct |
43 ms |
10448 KB |
Output is correct |
66 |
Correct |
66 ms |
14792 KB |
Output is correct |
67 |
Correct |
69 ms |
15828 KB |
Output is correct |
68 |
Correct |
81 ms |
14536 KB |
Output is correct |
69 |
Correct |
60 ms |
12744 KB |
Output is correct |
70 |
Correct |
52 ms |
10696 KB |
Output is correct |
71 |
Correct |
58 ms |
12788 KB |
Output is correct |
72 |
Correct |
65 ms |
14344 KB |
Output is correct |
73 |
Correct |
62 ms |
12476 KB |
Output is correct |
74 |
Correct |
57 ms |
17608 KB |
Output is correct |
75 |
Correct |
59 ms |
15816 KB |
Output is correct |
76 |
Correct |
59 ms |
15560 KB |
Output is correct |
77 |
Correct |
64 ms |
15288 KB |
Output is correct |
78 |
Incorrect |
447 ms |
5332 KB |
3rd lines differ - on the 1st token, expected: '14278', found: '2' |
79 |
Halted |
0 ms |
0 KB |
- |