#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, nxt->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);
return 1 + query(l, r + 1, d);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
467 ms |
27584 KB |
Output is correct |
2 |
Correct |
843 ms |
45988 KB |
Output is correct |
3 |
Correct |
932 ms |
45988 KB |
Output is correct |
4 |
Correct |
1106 ms |
45976 KB |
Output is correct |
5 |
Correct |
932 ms |
45972 KB |
Output is correct |
6 |
Correct |
1012 ms |
46012 KB |
Output is correct |
7 |
Correct |
904 ms |
45972 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
2 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1616 KB |
1st lines differ - on the 1st token, expected: '292', found: '285' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1616 KB |
1st lines differ - on the 1st token, expected: '292', found: '285' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1385 ms |
82276 KB |
4th lines differ - on the 1st token, expected: '13956', found: '13955' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
458 ms |
18984 KB |
1st lines differ - on the 1st token, expected: '7197', found: '7126' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1616 KB |
1st lines differ - on the 1st token, expected: '292', found: '285' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
467 ms |
27584 KB |
Output is correct |
2 |
Correct |
843 ms |
45988 KB |
Output is correct |
3 |
Correct |
932 ms |
45988 KB |
Output is correct |
4 |
Correct |
1106 ms |
45976 KB |
Output is correct |
5 |
Correct |
932 ms |
45972 KB |
Output is correct |
6 |
Correct |
1012 ms |
46012 KB |
Output is correct |
7 |
Correct |
904 ms |
45972 KB |
Output is correct |
8 |
Correct |
0 ms |
208 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 |
464 KB |
Output is correct |
12 |
Incorrect |
3 ms |
1616 KB |
1st lines differ - on the 1st token, expected: '292', found: '285' |
13 |
Halted |
0 ms |
0 KB |
- |