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 <bits/stdc++.h>
#include "towers.h"
#define ll long long
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T> bool ckmin(T &a, const T &b ) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b ) { return a < b ? a = b, 1 : 0; }
int n; vector<int> h, h2, pref;
int D;
int test(int i) {
return (i && i+1 < n && ((h[i] > h[i-1] && h[i] > h[i+1])));
}
bool one_peak = 1;
int peak = -1;
void init(int N, std::vector<int> H) {
n = N;
h = H;
h2 = H;
sort(all(h2));
h2.erase(unique(all(h2)), h2.end());
for (int &x : h) x = lower_bound(all(h2), x) - h2.begin();
pref.resize(n);
for (int i = 0; i < n; i++) {
pref[i] = test(i) + (i ? pref[i-1] : 0);
}
peak = max_element(all(h)) - h.begin();
for (int i = peak-1; i >= 0; i--) one_peak &= h[i] < h[i+1];
for (int i = peak+1; i < n; i++) one_peak &= h[i] < h[i-1];
}
struct seg {
int N; vector<int> tree;
seg(int n) : N(1<<(__lg(n)+1)), tree(2*N, -1e9) {}
void update(int pos, int x) {
for (int i = pos + N; i; i >>= 1) ckmax(tree[i], x);
}
int query(int node, int nl, int nr, int ql, int qr) {
if (ql > nr || qr < nl) return -1e9;
if (ql <= nl && nr <= qr) return tree[node];
int mid = (nl+nr)/2;
return max(query(node*2, nl, mid, ql, qr), query(node*2+1, mid+1, nr, ql, qr));
}
int query(int l, int r) {
return query(1, 0, N-1, l, r);
}
};
int Q = 0;
int max_towers(int L, int R, int _D) {
D = _D;
if (one_peak) {
return 1 + (L < peak && R > peak && (max(h[L], h[R]) <= h[peak] - D));
}
if (Q++ == -1) {
const int N = sz(h2);
seg st0(N), st1(N);
for (int i = L; i <= R; i++) {
auto it = lower_bound(all(h2), h2[h[i]] + D);
if (it != h2.end()) {
st1.update(h[i], st0.query(it - h2.begin(), N - 1) + 1);
}
auto it2 = upper_bound(all(h2), h2[h[i]] - D);
if (it2 != h2.begin()) {
it2--;
st0.update(h[i], st1.query(0, it2 - h2.begin()));
}
st1.update(h[i], 1);
}
return st1.query(0, N-1);
}
if (D == 1) {
return pref[R] - (L ? pref[L-1] : 0) + 1 - test(L) - test(R) + (L == R ? test(L) : 0);
}
int ans = 1;
set<int> SL, SR;
for (int i = L+1; i <= R-1; i++) if (h[i] > h[i-1] && h[i] > h[i+1]) {
int l = -1, r = -1;
for (int j = i-1; j >= L; j--) {
if (h2[h[i]] - D >= h2[h[j]]) {
if (SL.count(j)) break;
l = j;
}
}
for (int j = i+1; j <= R; j++) {
if (h2[h[i]] - D >= h2[h[j]]) {
if (SR.count(j)) break;
r = j;
}
}
if (~l && ~r) {
ans++;
SL.insert(l);
SR.insert(r);
}
}
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... |