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 <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int ind = -1;
bool ch = true;
vector <int> h;
const int N = 100100, M = 20;
int mx[N][M], Left[N], Right[N];
void init(int n, vector<int> a) {
ch = true;
ind = -1;
h = a;
for (int i = 0; i < n - 1; ++i)
{
if (a[i] > a[i + 1]) {
if (ind == -1) ind = i;
}
else {
if (ind != -1) ch = false;
}
}
for (int i = 0; i < n; i++) {
mx[i][0] = a[i];
}
for (int j = 1; j < 20; j++) {
for (int i = n - 1; i >= 0; i--) {
mx[i][j] = max(mx[i][j - 1], mx[min(n, i + (1 << (j - 1)))][j - 1]);
}
}
}
int findmaxinrange(int L, int R) {
int u = log2(R - L + 1);
return max(mx[L][u], mx[R - (1 << u) + 1][u]);
}
int max_towers(int L, int R, int D) {
if (ch) {
if (R < ind || L > ind) return 1;
if (h[ind] >= (max(h[L], h[R]) + D)) return 2;
return 1;
}
int n = (int)h.size();
vector <pair<int, int>> v;
set <int> ms;
for (int i = 0; i < n; i++) {
int l = 0, r = i - 1;
Left[i] = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (findmaxinrange(mid, i - 1) >= h[i] + D) {
Left[i] = mid;
l = mid + 1;
}
else r = mid - 1;
}
Right[i] = -1;
l = i + 1, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (findmaxinrange(i + 1, mid) >= h[i] + D) {
Right[i] = mid;
r = mid - 1;
}
else l = mid + 1;
}
v.push_back({h[i], i});
}
int ans = 0;
sort(v.begin(), v.end());
for (auto it : v) {
int ind = it.second;
if (ind > R || ind < L) continue;
if (Left[ind] != -1) {
auto it = ms.lower_bound(ind); --it;
if (it != ms.end() && *it > Left[ind]) {
continue;
}
}
if (Right[ind] != -1) {
auto it = ms.lower_bound(ind);
if (it != ms.end() && *it < Right[ind]) {
continue;
}
}
ms.insert(ind);
++ans;
}
return ans;
}
# | 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... |