This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
return 1 + query(l, r + 1, d);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |