이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of Allah
#include <bits/stdc++.h>
#include "towers.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define Mp make_pair
#define pb push_back
#define endl '\n'
#define sep ' '
const int maxn = 1e5 + 7;
const int maxlg = 20;
const ll oo = 1e9 + 7;
int n;
ll A[maxn]; int L[maxn], R[maxn];
int rmq[maxn][maxlg];
vector<ll> arr; ll sm[maxn];
int GI(ll x) {
return lower_bound(all(arr), x) - arr.begin();
}
ll get_max(int l, int r) {
if (l >= r) return -oo;
int j = __lg(r - l);
return max(rmq[l][j], rmq[r - (1 << j)][j]);
}
void init(int N, vector<int> H) {
n = N;
for (int i = 0; i < n; i++) A[i] = H[i];
for (int i = n - 1; i >= 0; i--) {
rmq[i][0] = A[i];
for (int j = 1; j < maxlg; j++) {
if (i + (1 << j) - 1 >= n) break;
rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 0; i < n; i++) {
for (L[i] = i - 1; L[i] != -1 && A[i] <= A[L[i]]; L[i] = L[L[i]]);
}
for (int i = n - 1; i >= 0; i--) {
for (R[i] = i + 1; R[i] != n && A[i] <= A[R[i]]; R[i] = R[R[i]]);
}
for (int i = 0; i < n; i++) {
ll x = oo;
if (L[i] != -1) x = min(x, get_max(L[i] + 1, i) - A[i]);
if (R[i] != n) x = min(x, get_max(i + 1, R[i]) - A[i]);
if (x <= 0) continue;
arr.pb(x);
}
sort(all(arr)); arr.resize(unique(all(arr)) - arr.begin());
for (int i = 0; i < n; i++) {
ll x = oo;
if (L[i] != -1) x = min(x, get_max(L[i] + 1, i) - A[i]);
if (R[i] != n) x = min(x, get_max(i + 1, R[i]) - A[i]);
if (x <= 0) continue;
int j = GI(x); sm[j]++;
}
for (int j = len(arr) - 2; j >= 0; j--) sm[j] += sm[j + 1];
}
bool check(int i, int j, int D) {
if (j - i + 1 <= 2) return 0;
int mx = get_max(i + 1, j);
return (mx >= max(A[i], A[j]) + D);
}
int max_towers(int L, int R, int D) {
int j = lower_bound(all(arr), D) - arr.begin();
return sm[j];
}
# | 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... |