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 <vector>
#include <algorithm>
#include <set>
using namespace std;
int n;
vector<int> H;
vector<pair<int, int>> byH;
const int MX_N = 1e5, LG = 17;
int rmq[1+LG][MX_N];
int p2[MX_N];
int rangeMax(int l, int r) {
int p = p2[r-l+1];
return max(rmq[p][l], rmq[p][r-(1<<p)+1]);
}
bool connected(int x, int y, int d) {
if (x == y) return true;
if (x > y) swap(x, y);
if (x+1 == y) return false;
return rangeMax(x+1, y-1) - d >= max(H[x], H[y]);
}
void init(int N, vector<int> Hs) {
for (int i = 2; i <= N; ++i) p2[i] = p2[i-1] + !(i & (i-1));
n = N, H = Hs;
for (int i = 0; i < n; ++i) byH.emplace_back(H[i], i);
sort(byH.begin(), byH.end());
// rmq
for (int i = 0; i < n; ++i) rmq[0][i] = H[i];
for (int i = 1, p = 1; i <= LG; ++i, p <<= 1) {
for (int j = 0; j < n; ++j) {
if (j+p >= n) rmq[i][j] = rmq[i-1][j];
else rmq[i][j] = max(rmq[i-1][j], rmq[i-1][j+p]);
}
}
}
int max_towers(int L, int R, int D) {
set<int> included;
for (auto [h, i] : byH) {
if (i < L || i > R) continue;
set<int>::iterator it = included.lower_bound(i);
if (it != included.end() && !connected(i, *it, D)) continue;
if (it != included.begin() && !connected(*prev(it), i, D)) continue;
included.insert(i);
}
return included.size();
}
# | 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... |