#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_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
vector<int> min_h, max_h, up, down, h;
vector<ordered_set<int>> settree;
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);
}
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);
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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1408 ms |
23804 KB |
Output is correct |
2 |
Execution timed out |
4040 ms |
39716 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1360 KB |
1st lines differ - on the 1st token, expected: '292', found: '286' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1360 KB |
1st lines differ - on the 1st token, expected: '292', found: '286' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1349 ms |
67276 KB |
Output is correct |
2 |
Correct |
1558 ms |
67852 KB |
Output is correct |
3 |
Correct |
1591 ms |
67868 KB |
Output is correct |
4 |
Correct |
1996 ms |
82136 KB |
Output is correct |
5 |
Correct |
1944 ms |
82048 KB |
Output is correct |
6 |
Correct |
1897 ms |
82096 KB |
Output is correct |
7 |
Correct |
1594 ms |
82216 KB |
Output is correct |
8 |
Execution timed out |
4051 ms |
39748 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
417 ms |
15620 KB |
1st lines differ - on the 1st token, expected: '7197', found: '7127' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1360 KB |
1st lines differ - on the 1st token, expected: '292', found: '286' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1408 ms |
23804 KB |
Output is correct |
2 |
Execution timed out |
4040 ms |
39716 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |