#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
#include <bits/extc++.h>
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
vector<ordered_set<pair<int, int>>> settree;
vector<int> min_h, max_h, up, down, h;
int n;
void build(int u, int l, int r) {
if (l + 1 == r) {
min_h[u] = max_h[u] = h[l];
} else {
int m = (l + r) / 2;
build(2 * u, l, m);
build(2 * u + 1, m, r);
min_h[u] = min(min_h[2 * u], min_h[2 * u + 1]);
max_h[u] = max(max_h[2 * u], max_h[2 * u + 1]);
up[u] = max({up[2 * u], up[2 * u + 1], max_h[2 * u + 1] - min_h[2 * u]});
down[u] = max({down[2 * u], down[2 * u + 1], max_h[2 * u] - min_h[2 * u + 1]});
}
}
void update(int a, int x, int u = 1, int l = 0, int r = n) {
if (l + 1 < r) {
int m = (l + r) / 2;
if (a < m) update(a, x, 2 * u, l, m);
else update(a, x, 2 * u + 1, m, r);
}
settree[u].insert({x, a});
}
void init(int n, vector<int> h) {
::h = h, ::n = n;
vector<pair<int, int>> points;
for (int i = 0; i < n; ++i) {
points.push_back({h[i], i});
}
sort(points.begin(), points.end());
settree.resize(4 * n);
set<array<int, 3>> ranges = {{INT_MIN, INT_MIN, -1}, {INT_MAX, INT_MAX, -1}};
for (auto [hi, i] : points) {
auto nxt = ranges.upper_bound({i, i, hi});
auto prv = nxt; --prv;
if (nxt->at(0) == i + 1 && prv->at(1) == i - 1) {
update(i, hi - max(nxt->at(2), prv->at(2)));
}
int l = i, r = i, x = hi;
if (nxt->at(0) == i + 1) {
r = nxt->at(1);
x = min(x, nxt->at(2));
ranges.erase(nxt);
}
if (prv->at(1) == i - 1) {
l = prv->at(0);
x = min(x, prv->at(2));
ranges.erase(prv);
}
ranges.insert({l, r, x});
}
max_h.resize(4 * n);
min_h.resize(4 * n);
up.resize(4 * n);
down.resize(4 * n);
build(1, 0, n);
}
int find_first(int a, int d, int &prefix, int u = 1, int l = 0, int r = n) {
if (r <= a) return n;
if (l + 1 == r) {
return max_h[u] - prefix >= d ? l : n;
}
if (l >= a && up[u] < d && max_h[u] - prefix < d) {
prefix = min(prefix, min_h[u]);
return n;
}
int m = (l + r) / 2;
int b = find_first(a, d, prefix, 2 * u, l, m);
return b == n ? find_first(a, d, prefix, 2 * u + 1, m, r) : b;
}
int find_last(int b, int d, int &suffix, int u = 1, int l = 0, int r = n) {
if (l > b) return -1;
if (l + 1 == r) {
return max_h[u] - suffix >= d ? l : -1;
}
if (r <= b + 1 && down[u] < d && max_h[u] - suffix < d) {
suffix = min(suffix, min_h[u]);
return -1;
}
int m = (l + r) / 2;
int a = find_last(b, d, suffix, 2 * u + 1, m, r);
return a == -1 ? find_last(b, d, suffix, 2 * u, l, m) : a;
}
int query(int a, int b, int d, int u = 1, int l = 0, int r = n) {
if (a >= b || b <= l || r <= a) return 0;
if (a <= l && r <= b) return settree[u].size() - settree[u].order_of_key({d, -1});
int m = (l + r) / 2;
return query(a, b, d, 2 * u, l, m) + query(a, b, d, 2 * u + 1, m, r);
}
int max_towers(int l, int r, int d) {
/*
int temp = h[l];
l = find_first(l, d, temp);
temp = h[r];
r = find_last(r, d, temp);
*/
int min_h = h[l];
while (l <= r && h[l] - min_h < d) {
min_h = min(min_h, h[l]);
++l;
}
min_h = h[r];
while (l <= r && h[r] - min_h < d) {
min_h = min(min_h, h[r]);
--r;
}
return 1 + query(l, r + 1, d);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1249 ms |
27536 KB |
Output is correct |
2 |
Execution timed out |
4049 ms |
46136 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
3 ms |
1616 KB |
Output is correct |
3 |
Correct |
3 ms |
1616 KB |
Output is correct |
4 |
Correct |
4 ms |
1872 KB |
Output is correct |
5 |
Correct |
4 ms |
1872 KB |
Output is correct |
6 |
Correct |
4 ms |
1872 KB |
Output is correct |
7 |
Correct |
4 ms |
1872 KB |
Output is correct |
8 |
Correct |
1 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
1 ms |
1104 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
1168 KB |
Output is correct |
14 |
Correct |
2 ms |
1104 KB |
Output is correct |
15 |
Correct |
4 ms |
1616 KB |
Output is correct |
16 |
Correct |
4 ms |
1872 KB |
Output is correct |
17 |
Correct |
4 ms |
1872 KB |
Output is correct |
18 |
Correct |
1 ms |
1104 KB |
Output is correct |
19 |
Correct |
2 ms |
1104 KB |
Output is correct |
20 |
Correct |
4 ms |
1616 KB |
Output is correct |
21 |
Correct |
4 ms |
1872 KB |
Output is correct |
22 |
Correct |
4 ms |
1872 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
1 ms |
1104 KB |
Output is correct |
25 |
Correct |
2 ms |
848 KB |
Output is correct |
26 |
Correct |
4 ms |
1736 KB |
Output is correct |
27 |
Correct |
4 ms |
1616 KB |
Output is correct |
28 |
Correct |
4 ms |
1872 KB |
Output is correct |
29 |
Correct |
4 ms |
1960 KB |
Output is correct |
30 |
Correct |
4 ms |
1872 KB |
Output is correct |
31 |
Correct |
4 ms |
1872 KB |
Output is correct |
32 |
Correct |
2 ms |
1224 KB |
Output is correct |
33 |
Correct |
1 ms |
1104 KB |
Output is correct |
34 |
Correct |
1 ms |
1104 KB |
Output is correct |
35 |
Correct |
2 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
3 ms |
1616 KB |
Output is correct |
3 |
Correct |
3 ms |
1616 KB |
Output is correct |
4 |
Correct |
4 ms |
1872 KB |
Output is correct |
5 |
Correct |
4 ms |
1872 KB |
Output is correct |
6 |
Correct |
4 ms |
1872 KB |
Output is correct |
7 |
Correct |
4 ms |
1872 KB |
Output is correct |
8 |
Correct |
1 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
1 ms |
1104 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
1168 KB |
Output is correct |
14 |
Correct |
2 ms |
1104 KB |
Output is correct |
15 |
Correct |
4 ms |
1616 KB |
Output is correct |
16 |
Correct |
4 ms |
1872 KB |
Output is correct |
17 |
Correct |
4 ms |
1872 KB |
Output is correct |
18 |
Correct |
1 ms |
1104 KB |
Output is correct |
19 |
Correct |
2 ms |
1104 KB |
Output is correct |
20 |
Correct |
4 ms |
1616 KB |
Output is correct |
21 |
Correct |
4 ms |
1872 KB |
Output is correct |
22 |
Correct |
4 ms |
1872 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
1 ms |
1104 KB |
Output is correct |
25 |
Correct |
2 ms |
848 KB |
Output is correct |
26 |
Correct |
4 ms |
1736 KB |
Output is correct |
27 |
Correct |
4 ms |
1616 KB |
Output is correct |
28 |
Correct |
4 ms |
1872 KB |
Output is correct |
29 |
Correct |
4 ms |
1960 KB |
Output is correct |
30 |
Correct |
4 ms |
1872 KB |
Output is correct |
31 |
Correct |
4 ms |
1872 KB |
Output is correct |
32 |
Correct |
2 ms |
1224 KB |
Output is correct |
33 |
Correct |
1 ms |
1104 KB |
Output is correct |
34 |
Correct |
1 ms |
1104 KB |
Output is correct |
35 |
Correct |
2 ms |
1104 KB |
Output is correct |
36 |
Correct |
252 ms |
53116 KB |
Output is correct |
37 |
Correct |
464 ms |
83108 KB |
Output is correct |
38 |
Correct |
451 ms |
82904 KB |
Output is correct |
39 |
Correct |
654 ms |
101436 KB |
Output is correct |
40 |
Correct |
684 ms |
101404 KB |
Output is correct |
41 |
Correct |
757 ms |
101456 KB |
Output is correct |
42 |
Correct |
770 ms |
101460 KB |
Output is correct |
43 |
Correct |
54 ms |
46092 KB |
Output is correct |
44 |
Correct |
51 ms |
46068 KB |
Output is correct |
45 |
Correct |
73 ms |
46140 KB |
Output is correct |
46 |
Correct |
51 ms |
45988 KB |
Output is correct |
47 |
Correct |
494 ms |
82952 KB |
Output is correct |
48 |
Correct |
675 ms |
101436 KB |
Output is correct |
49 |
Correct |
642 ms |
101392 KB |
Output is correct |
50 |
Correct |
50 ms |
45992 KB |
Output is correct |
51 |
Correct |
52 ms |
46004 KB |
Output is correct |
52 |
Correct |
435 ms |
83004 KB |
Output is correct |
53 |
Correct |
637 ms |
101436 KB |
Output is correct |
54 |
Correct |
641 ms |
101388 KB |
Output is correct |
55 |
Correct |
51 ms |
46100 KB |
Output is correct |
56 |
Correct |
50 ms |
46032 KB |
Output is correct |
57 |
Correct |
435 ms |
80052 KB |
Output is correct |
58 |
Correct |
428 ms |
83060 KB |
Output is correct |
59 |
Correct |
436 ms |
83092 KB |
Output is correct |
60 |
Correct |
661 ms |
101476 KB |
Output is correct |
61 |
Correct |
667 ms |
101448 KB |
Output is correct |
62 |
Correct |
663 ms |
101456 KB |
Output is correct |
63 |
Correct |
656 ms |
101452 KB |
Output is correct |
64 |
Correct |
55 ms |
46084 KB |
Output is correct |
65 |
Correct |
55 ms |
45976 KB |
Output is correct |
66 |
Correct |
49 ms |
46108 KB |
Output is correct |
67 |
Correct |
52 ms |
46100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1360 ms |
82252 KB |
Output is correct |
2 |
Correct |
1718 ms |
82952 KB |
Output is correct |
3 |
Correct |
1535 ms |
82996 KB |
Output is correct |
4 |
Correct |
1780 ms |
101476 KB |
Output is correct |
5 |
Correct |
2076 ms |
101408 KB |
Output is correct |
6 |
Correct |
2039 ms |
101380 KB |
Output is correct |
7 |
Correct |
1961 ms |
101440 KB |
Output is correct |
8 |
Execution timed out |
4067 ms |
46092 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
450 ms |
19000 KB |
Output is correct |
2 |
Correct |
1967 ms |
82940 KB |
Output is correct |
3 |
Correct |
1868 ms |
82980 KB |
Output is correct |
4 |
Correct |
2084 ms |
101508 KB |
Output is correct |
5 |
Correct |
2201 ms |
101396 KB |
Output is correct |
6 |
Correct |
1998 ms |
101400 KB |
Output is correct |
7 |
Correct |
2236 ms |
101384 KB |
Output is correct |
8 |
Execution timed out |
4067 ms |
46036 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
3 ms |
1616 KB |
Output is correct |
3 |
Correct |
3 ms |
1616 KB |
Output is correct |
4 |
Correct |
4 ms |
1872 KB |
Output is correct |
5 |
Correct |
4 ms |
1872 KB |
Output is correct |
6 |
Correct |
4 ms |
1872 KB |
Output is correct |
7 |
Correct |
4 ms |
1872 KB |
Output is correct |
8 |
Correct |
1 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
1 ms |
1104 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
1168 KB |
Output is correct |
14 |
Correct |
2 ms |
1104 KB |
Output is correct |
15 |
Correct |
4 ms |
1616 KB |
Output is correct |
16 |
Correct |
4 ms |
1872 KB |
Output is correct |
17 |
Correct |
4 ms |
1872 KB |
Output is correct |
18 |
Correct |
1 ms |
1104 KB |
Output is correct |
19 |
Correct |
2 ms |
1104 KB |
Output is correct |
20 |
Correct |
4 ms |
1616 KB |
Output is correct |
21 |
Correct |
4 ms |
1872 KB |
Output is correct |
22 |
Correct |
4 ms |
1872 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
1 ms |
1104 KB |
Output is correct |
25 |
Correct |
2 ms |
848 KB |
Output is correct |
26 |
Correct |
4 ms |
1736 KB |
Output is correct |
27 |
Correct |
4 ms |
1616 KB |
Output is correct |
28 |
Correct |
4 ms |
1872 KB |
Output is correct |
29 |
Correct |
4 ms |
1960 KB |
Output is correct |
30 |
Correct |
4 ms |
1872 KB |
Output is correct |
31 |
Correct |
4 ms |
1872 KB |
Output is correct |
32 |
Correct |
2 ms |
1224 KB |
Output is correct |
33 |
Correct |
1 ms |
1104 KB |
Output is correct |
34 |
Correct |
1 ms |
1104 KB |
Output is correct |
35 |
Correct |
2 ms |
1104 KB |
Output is correct |
36 |
Correct |
252 ms |
53116 KB |
Output is correct |
37 |
Correct |
464 ms |
83108 KB |
Output is correct |
38 |
Correct |
451 ms |
82904 KB |
Output is correct |
39 |
Correct |
654 ms |
101436 KB |
Output is correct |
40 |
Correct |
684 ms |
101404 KB |
Output is correct |
41 |
Correct |
757 ms |
101456 KB |
Output is correct |
42 |
Correct |
770 ms |
101460 KB |
Output is correct |
43 |
Correct |
54 ms |
46092 KB |
Output is correct |
44 |
Correct |
51 ms |
46068 KB |
Output is correct |
45 |
Correct |
73 ms |
46140 KB |
Output is correct |
46 |
Correct |
51 ms |
45988 KB |
Output is correct |
47 |
Correct |
494 ms |
82952 KB |
Output is correct |
48 |
Correct |
675 ms |
101436 KB |
Output is correct |
49 |
Correct |
642 ms |
101392 KB |
Output is correct |
50 |
Correct |
50 ms |
45992 KB |
Output is correct |
51 |
Correct |
52 ms |
46004 KB |
Output is correct |
52 |
Correct |
435 ms |
83004 KB |
Output is correct |
53 |
Correct |
637 ms |
101436 KB |
Output is correct |
54 |
Correct |
641 ms |
101388 KB |
Output is correct |
55 |
Correct |
51 ms |
46100 KB |
Output is correct |
56 |
Correct |
50 ms |
46032 KB |
Output is correct |
57 |
Correct |
435 ms |
80052 KB |
Output is correct |
58 |
Correct |
428 ms |
83060 KB |
Output is correct |
59 |
Correct |
436 ms |
83092 KB |
Output is correct |
60 |
Correct |
661 ms |
101476 KB |
Output is correct |
61 |
Correct |
667 ms |
101448 KB |
Output is correct |
62 |
Correct |
663 ms |
101456 KB |
Output is correct |
63 |
Correct |
656 ms |
101452 KB |
Output is correct |
64 |
Correct |
55 ms |
46084 KB |
Output is correct |
65 |
Correct |
55 ms |
45976 KB |
Output is correct |
66 |
Correct |
49 ms |
46108 KB |
Output is correct |
67 |
Correct |
52 ms |
46100 KB |
Output is correct |
68 |
Correct |
1360 ms |
82252 KB |
Output is correct |
69 |
Correct |
1718 ms |
82952 KB |
Output is correct |
70 |
Correct |
1535 ms |
82996 KB |
Output is correct |
71 |
Correct |
1780 ms |
101476 KB |
Output is correct |
72 |
Correct |
2076 ms |
101408 KB |
Output is correct |
73 |
Correct |
2039 ms |
101380 KB |
Output is correct |
74 |
Correct |
1961 ms |
101440 KB |
Output is correct |
75 |
Execution timed out |
4067 ms |
46092 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1249 ms |
27536 KB |
Output is correct |
2 |
Execution timed out |
4049 ms |
46136 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |