#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
template <typename T, size_t L>
struct max_segtree
{
T t[L << 1];
max_segtree() { fill(t, t + (L << 1), numeric_limits<T>::min()); }
void update(size_t i, T x)
{
i += L;
t[i] = max(t[i], x);
while (i >>= 1)
t[i] = max(t[i << 1], t[i << 1 | 1]);
}
T range_max(size_t i, size_t j)
{
i += L, j += L;
T x = numeric_limits<T>::min();
while (i <= j)
{
if (i & 1)
x = max(x, t[i++]);
if (!(j & 1))
x = max(x, t[j--]);
i >>= 1;
j >>= 1;
}
return x;
}
};
template <typename T, size_t L>
struct min_segtree
{
T t[L << 1];
min_segtree() { fill(t, t + (L << 1), numeric_limits<T>::max()); }
void update(size_t i, T x)
{
i += L;
t[i] = min(t[i], x);
while (i >>= 1)
t[i] = min(t[i << 1], t[i << 1 | 1]);
}
T range_min(size_t i, size_t j)
{
i += L, j += L;
T x = numeric_limits<T>::max();
while (i <= j)
{
if (i & 1)
x = min(x, t[i++]);
if (!(j & 1))
x = min(x, t[j--]);
i >>= 1;
j >>= 1;
}
return x;
}
};
struct node
{
uint32_t l, r, s;
};
constexpr size_t N = 100000, M = 4000000, L = 1 << 17;
node tree[M];
uint32_t roots[N];
vector<int64_t> delta_coords;
size_t left_greater[N], left_smaller[N], right_greater[N], right_smaller[N], num_nodes;
max_segtree<int64_t, L> hmax;
uint32_t incr(size_t i, uint32_t x, size_t k, size_t a = 0, size_t b = L - 1)
{
uint32_t new_node = num_nodes++;
if (a == b)
{
tree[new_node].s = tree[k].s + x;
return new_node;
}
if (i <= (a + b) >> 1)
{
tree[new_node].r = tree[k].r;
tree[new_node].l = incr(i, x, tree[k].l, a, (a + b) >> 1);
}
else
{
tree[new_node].l = tree[k].l;
tree[new_node].r = incr(i, x, tree[k].r, ((a + b) >> 1) + 1, b);
}
tree[new_node].s = tree[tree[new_node].l].s + tree[tree[new_node].r].s;
return new_node;
}
uint32_t range_sum(size_t i, size_t j, size_t k, size_t a = 0, size_t b = L - 1)
{
if (b < i || a > j)
return 0;
if (i <= a && b <= j)
return tree[k].s;
return range_sum(i, j, tree[k].l, a, (a + b) >> 1) +
range_sum(i, j, tree[k].r, ((a + b) >> 1) + 1, b);
}
uint32_t get_root(int64_t delta)
{
return roots[upper_bound(delta_coords.begin(), delta_coords.end(), delta, greater<int64_t>()) - delta_coords.begin() - 1];
}
void init(int n, vector<int> h)
{
{
stack<size_t> s;
for (size_t i = 0; i < n; ++i)
{
while (!s.empty() && h[s.top()] < h[i])
s.pop();
left_greater[i] = !s.empty() ? s.top() : SIZE_MAX;
s.push(i);
}
while (!s.empty())
s.pop();
for (size_t i = 0; i < n; ++i)
{
while (!s.empty() && h[s.top()] > h[i])
s.pop();
left_smaller[i] = !s.empty() ? s.top() : SIZE_MAX;
s.push(i);
}
while (!s.empty())
s.pop();
for (size_t i = n - 1; i < n; --i)
{
while (!s.empty() && h[s.top()] < h[i])
s.pop();
right_greater[i] = !s.empty() ? s.top() : SIZE_MAX;
s.push(i);
}
while (!s.empty())
s.pop();
for (size_t i = n - 1; i < n; --i)
{
while (!s.empty() && h[s.top()] > h[i])
s.pop();
right_smaller[i] = !s.empty() ? s.top() : SIZE_MAX;
s.push(i);
}
}
for (size_t i = 0; i < n; ++i)
hmax.update(i, h[i]);
vector<pair<int64_t, size_t>> delta;
for (size_t i = 0; i < n; ++i)
{
int64_t d = INT64_MAX;
if (left_smaller[i] != SIZE_MAX)
d = min(d, hmax.range_max(left_smaller[i] + 1, i - 1));
if (right_smaller[i] != SIZE_MAX)
d = min(d, hmax.range_max(i + 1, right_smaller[i] - 1));
if (left_smaller[i] == SIZE_MAX && right_smaller[i] == SIZE_MAX)
delta.emplace_back(INT64_MAX, i);
else if (d != INT64_MIN)
delta.emplace_back((d - h[i]), i);
}
sort(delta.begin(), delta.end(), greater<pair<int64_t, size_t>>());
uint32_t curr_root = 1;
for (size_t i = L - 1; i; --i)
tree[i].l = i << 1, tree[i].r = i << 1 | 1;
num_nodes = L << 1;
for (size_t i = 0, k = 0; i < delta.size();)
{
size_t j = i;
while (j < delta.size() && delta[j].first == delta[i].first)
curr_root = incr(delta[j].second, 1, curr_root), ++j;
roots[k] = curr_root;
delta_coords.push_back(delta[i].first);
i = j;
++k;
}
}
int max_towers(int l, int r, int d)
{
return range_sum(l, r, get_root(d));
}
Compilation message
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:131:30: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
131 | for (size_t i = 0; i < n; ++i)
| ~~^~~
towers.cpp:142:30: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
142 | for (size_t i = 0; i < n; ++i)
| ~~^~~
towers.cpp:153:34: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
153 | for (size_t i = n - 1; i < n; --i)
| ~~^~~
towers.cpp:164:34: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
164 | for (size_t i = n - 1; i < n; --i)
| ~~^~~
towers.cpp:173:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
173 | for (size_t i = 0; i < n; ++i)
| ~~^~~
towers.cpp:177:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
177 | for (size_t i = 0; i < n; ++i)
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
297 ms |
6480 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3920 KB |
Output is correct |
2 |
Correct |
2 ms |
4176 KB |
Output is correct |
3 |
Correct |
2 ms |
4176 KB |
Output is correct |
4 |
Correct |
2 ms |
4176 KB |
Output is correct |
5 |
Correct |
2 ms |
4224 KB |
Output is correct |
6 |
Correct |
2 ms |
4176 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4188 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3920 KB |
Output is correct |
2 |
Correct |
2 ms |
4176 KB |
Output is correct |
3 |
Correct |
2 ms |
4176 KB |
Output is correct |
4 |
Correct |
2 ms |
4176 KB |
Output is correct |
5 |
Correct |
2 ms |
4224 KB |
Output is correct |
6 |
Correct |
2 ms |
4176 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4188 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
514 ms |
16188 KB |
1st lines differ - on the 1st token, expected: '11903', found: '11902' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
6816 KB |
Output is correct |
2 |
Correct |
645 ms |
16292 KB |
Output is correct |
3 |
Correct |
622 ms |
16284 KB |
Output is correct |
4 |
Correct |
630 ms |
20272 KB |
Output is correct |
5 |
Correct |
666 ms |
20144 KB |
Output is correct |
6 |
Correct |
568 ms |
20148 KB |
Output is correct |
7 |
Correct |
737 ms |
20152 KB |
Output is correct |
8 |
Correct |
777 ms |
8280 KB |
Output is correct |
9 |
Correct |
673 ms |
8632 KB |
Output is correct |
10 |
Correct |
493 ms |
7980 KB |
Output is correct |
11 |
Correct |
612 ms |
8352 KB |
Output is correct |
12 |
Correct |
33 ms |
16188 KB |
Output is correct |
13 |
Correct |
41 ms |
20156 KB |
Output is correct |
14 |
Correct |
36 ms |
20156 KB |
Output is correct |
15 |
Correct |
17 ms |
8632 KB |
Output is correct |
16 |
Correct |
19 ms |
8112 KB |
Output is correct |
17 |
Correct |
31 ms |
15632 KB |
Output is correct |
18 |
Correct |
33 ms |
16176 KB |
Output is correct |
19 |
Correct |
32 ms |
16244 KB |
Output is correct |
20 |
Correct |
36 ms |
20148 KB |
Output is correct |
21 |
Correct |
36 ms |
20272 KB |
Output is correct |
22 |
Correct |
37 ms |
20224 KB |
Output is correct |
23 |
Correct |
45 ms |
20376 KB |
Output is correct |
24 |
Correct |
16 ms |
8264 KB |
Output is correct |
25 |
Correct |
16 ms |
8636 KB |
Output is correct |
26 |
Correct |
20 ms |
7896 KB |
Output is correct |
27 |
Correct |
22 ms |
8576 KB |
Output is correct |
28 |
Correct |
2 ms |
4176 KB |
Output is correct |
29 |
Correct |
2 ms |
4176 KB |
Output is correct |
30 |
Correct |
2 ms |
4176 KB |
Output is correct |
31 |
Correct |
2 ms |
4004 KB |
Output is correct |
32 |
Correct |
2 ms |
3920 KB |
Output is correct |
33 |
Correct |
2 ms |
4048 KB |
Output is correct |
34 |
Correct |
2 ms |
4176 KB |
Output is correct |
35 |
Correct |
2 ms |
4176 KB |
Output is correct |
36 |
Correct |
2 ms |
4176 KB |
Output is correct |
37 |
Correct |
3 ms |
4176 KB |
Output is correct |
38 |
Correct |
3 ms |
4176 KB |
Output is correct |
39 |
Correct |
2 ms |
4176 KB |
Output is correct |
40 |
Correct |
2 ms |
3920 KB |
Output is correct |
41 |
Correct |
2 ms |
3920 KB |
Output is correct |
42 |
Correct |
2 ms |
3920 KB |
Output is correct |
43 |
Correct |
2 ms |
3920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3920 KB |
Output is correct |
2 |
Correct |
2 ms |
4176 KB |
Output is correct |
3 |
Correct |
2 ms |
4176 KB |
Output is correct |
4 |
Correct |
2 ms |
4176 KB |
Output is correct |
5 |
Correct |
2 ms |
4224 KB |
Output is correct |
6 |
Correct |
2 ms |
4176 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4188 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
297 ms |
6480 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |