#include "towers.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXLOG = 20;
const int INF = 1e9;
int n;
int h[MAXN];
int perm[MAXN];
int getLog[MAXN];
struct BIT
{
int tree[MAXN];
inline void update(int pos, int val)
{
for (int idx = pos ; idx <= n ; idx += idx & (-idx))
{
tree[idx] += val;
}
}
inline int query(int pos)
{
int ans = 0;
for (int idx = pos ; idx >= 1 ; idx -= idx & (-idx))
{
ans += tree[idx];
}
return ans;
}
inline int find(int k)
{
int pos = 0;
for (int log = MAXLOG - 1 ; log >= 0 ; --log)
{
if (pos + (1 << log) > n) continue;
if (tree[pos + (1 << log)] < k)
{
pos += (1 << log);
k -= tree[pos];
}
}
return pos + 1;
}
};
BIT tree;
struct SparseMAX
{
int sparse[MAXLOG][MAXN];
void build()
{
for (int i = 1 ; i <= n ; ++i)
{
sparse[0][i] = h[i];
}
for (int log = 1 ; (1 << log) <= n ; ++log)
{
for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
{
sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
}
}
for (int i = 1 ; i <= n ; ++i)
{
getLog[i] = getLog[i - 1];
if ((1 << getLog[i] + 1) < i) getLog[i]++;
}
}
inline int findMAX(int l, int r)
{
int log = getLog[r - l + 1];
return std::max(sparse[log][l], sparse[log][r - (1 << log) + 1]);
}
};
SparseMAX sparse;
void init(int N, std::vector <int> H)
{
n = N;
for (int i = 0 ; i < n ; ++i)
{
h[i + 1] = H[i];
}
sparse.build();
}
int max_towers(int l, int r, int d)
{
l++; r++;
std::iota(perm + l, perm + r + 1, l);
std::sort(perm + l, perm + r + 1, [&](int x, int y)
{
return h[x] < h[y];
});
int ans = 0;
for (int i = l ; i <= r ; ++i)
{
bool can = true;
int pos = perm[i];
if (tree.query(pos) != 0)
{
int left = tree.find(tree.query(pos));
if (left == pos - 1 || sparse.findMAX(left + 1, pos - 1) - d < std::max(h[left], h[pos]))
{
can = false;
}
}
if (tree.query(pos) != ans)
{
int right = tree.find(tree.query(pos) + 1);
if (right == pos + 1 || sparse.findMAX(pos + 1, right - 1) - d < std::max(h[pos], h[right]))
{
can = false;
}
}
if (can)
{
ans++;
tree.update(pos, 1);
}
}
return ans;
}
Compilation message
towers.cpp: In member function 'void SparseMAX::build()':
towers.cpp:72:93: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
72 | sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
| ~~~~^~~
towers.cpp:79:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
79 | if ((1 << getLog[i] + 1) < i) getLog[i]++;
| ~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4070 ms |
5024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
436 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
436 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
16 ms |
5200 KB |
Output is correct |
37 |
Correct |
27 ms |
7988 KB |
Output is correct |
38 |
Correct |
20 ms |
7984 KB |
Output is correct |
39 |
Correct |
32 ms |
7968 KB |
Output is correct |
40 |
Correct |
19 ms |
8008 KB |
Output is correct |
41 |
Correct |
46 ms |
8264 KB |
Output is correct |
42 |
Correct |
17 ms |
8016 KB |
Output is correct |
43 |
Correct |
19 ms |
8008 KB |
Output is correct |
44 |
Correct |
21 ms |
7972 KB |
Output is correct |
45 |
Correct |
21 ms |
7992 KB |
Output is correct |
46 |
Correct |
23 ms |
7956 KB |
Output is correct |
47 |
Correct |
27 ms |
7964 KB |
Output is correct |
48 |
Correct |
21 ms |
7968 KB |
Output is correct |
49 |
Correct |
19 ms |
7976 KB |
Output is correct |
50 |
Correct |
16 ms |
7972 KB |
Output is correct |
51 |
Correct |
18 ms |
8008 KB |
Output is correct |
52 |
Correct |
52 ms |
8384 KB |
Output is correct |
53 |
Correct |
51 ms |
8384 KB |
Output is correct |
54 |
Correct |
53 ms |
8480 KB |
Output is correct |
55 |
Correct |
26 ms |
7988 KB |
Output is correct |
56 |
Correct |
24 ms |
8092 KB |
Output is correct |
57 |
Correct |
50 ms |
8172 KB |
Output is correct |
58 |
Correct |
52 ms |
8360 KB |
Output is correct |
59 |
Correct |
52 ms |
8416 KB |
Output is correct |
60 |
Correct |
54 ms |
8420 KB |
Output is correct |
61 |
Correct |
51 ms |
8332 KB |
Output is correct |
62 |
Correct |
51 ms |
8420 KB |
Output is correct |
63 |
Correct |
58 ms |
8416 KB |
Output is correct |
64 |
Correct |
22 ms |
8008 KB |
Output is correct |
65 |
Correct |
29 ms |
7976 KB |
Output is correct |
66 |
Correct |
26 ms |
8088 KB |
Output is correct |
67 |
Correct |
23 ms |
8008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4046 ms |
8504 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4005 ms |
2128 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
436 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
16 ms |
5200 KB |
Output is correct |
37 |
Correct |
27 ms |
7988 KB |
Output is correct |
38 |
Correct |
20 ms |
7984 KB |
Output is correct |
39 |
Correct |
32 ms |
7968 KB |
Output is correct |
40 |
Correct |
19 ms |
8008 KB |
Output is correct |
41 |
Correct |
46 ms |
8264 KB |
Output is correct |
42 |
Correct |
17 ms |
8016 KB |
Output is correct |
43 |
Correct |
19 ms |
8008 KB |
Output is correct |
44 |
Correct |
21 ms |
7972 KB |
Output is correct |
45 |
Correct |
21 ms |
7992 KB |
Output is correct |
46 |
Correct |
23 ms |
7956 KB |
Output is correct |
47 |
Correct |
27 ms |
7964 KB |
Output is correct |
48 |
Correct |
21 ms |
7968 KB |
Output is correct |
49 |
Correct |
19 ms |
7976 KB |
Output is correct |
50 |
Correct |
16 ms |
7972 KB |
Output is correct |
51 |
Correct |
18 ms |
8008 KB |
Output is correct |
52 |
Correct |
52 ms |
8384 KB |
Output is correct |
53 |
Correct |
51 ms |
8384 KB |
Output is correct |
54 |
Correct |
53 ms |
8480 KB |
Output is correct |
55 |
Correct |
26 ms |
7988 KB |
Output is correct |
56 |
Correct |
24 ms |
8092 KB |
Output is correct |
57 |
Correct |
50 ms |
8172 KB |
Output is correct |
58 |
Correct |
52 ms |
8360 KB |
Output is correct |
59 |
Correct |
52 ms |
8416 KB |
Output is correct |
60 |
Correct |
54 ms |
8420 KB |
Output is correct |
61 |
Correct |
51 ms |
8332 KB |
Output is correct |
62 |
Correct |
51 ms |
8420 KB |
Output is correct |
63 |
Correct |
58 ms |
8416 KB |
Output is correct |
64 |
Correct |
22 ms |
8008 KB |
Output is correct |
65 |
Correct |
29 ms |
7976 KB |
Output is correct |
66 |
Correct |
26 ms |
8088 KB |
Output is correct |
67 |
Correct |
23 ms |
8008 KB |
Output is correct |
68 |
Execution timed out |
4046 ms |
8504 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4070 ms |
5024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |