이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7, K = 19;
const int INF = 1e9;
int n, h[N];
int L[N], R[N], M[N];
int R1[N][K], M1[N][K];
pair<int, int> t[4 * N];
void build(int v, int l, int r) {
if (r - l == 1) {
t[v] = {h[l], l};
} else {
int m = (l + r) / 2;
build(2 * v, l, m);
build(2 * v + 1, m, r);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
pair<int, int> get(int v, int l, int r, int a, int b) {
if (l >= b || r <= a) return {-INF, -INF};
if (l >= a && r <= b) return t[v];
int m = (l + r) / 2;
return max(get(2 * v, l, m, a, b), get(2 * v + 1, m, r, a, b));
}
int get1(int v, int l, int r, int a, int b, int x) {
if (l >= a || r <= b || t[v].first <= x) return -1;
if (r - l == 1) return l;
int m = (l + r) / 2;
int res = get1(2 * v + 1, m, r, a, b, x);
if (res == -1) res = get1(2 * v, l, m, a, b, x);
return res;
}
void init(int nn, vector<int> hh) {
for (int i = 0; i < nn; i++) h[i + 1] = hh[i] - 1;
h[0] = nn;
h[nn + 1] = nn + 1;
n = nn + 2;
vector<int> st;
for (int i = 0; i < n; ++i) {
while (!st.empty() && h[st.back()] < h[i]) {
R[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
R[n - 1] = n - 1;
st.clear();
for (int i = n - 1; i >= 0; --i) {
while (!st.empty() && h[st.back()] < h[i]) {
L[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
L[0] = 0;
st.clear();
build(1, 0, n);
for (int i = 0; i < n; ++i) M[i] = h[L[i]] > h[R[i]] ? L[i] : R[i];
for (int i = 0; i < n; ++i) {
R1[i][0] = R[i];
M1[i][0] = M[i];
}
for (int i = 1; i < K; ++i) {
for (int j = 0; j < n; ++j) {
R1[j][i] = R1[R1[j][i - 1]][i - 1];
M1[j][i] = M1[M1[j][i - 1]][i - 1];
}
}
}
int minimum_jumps(int aa, int bb, int cc, int dd) {
int a = aa + 1, b = bb + 1, c = cc + 1, d = dd + 1;
int mx = get(1, 0, n, c, d + 1).first;
int mx1 = get(1, 0, n, b + 1, c).first;
if (mx1 > mx || h[b] > mx) return -1;
int ind = get1(1, 0, n, 0, b + 1, mx);
a = max(a, ind + 1);
int st = get(1, 0, n, a, b + 1).second;
int res = 1;
for (int i = K - 1; i >= 0; --i) {
if (h[M1[st][i]] < mx1) {
res += (1 << i);
st = M1[st][i];
}
}
if (h[st] < mx1 && h[M[st]] <= mx) {
st = M[st];
++res;
}
for (int i = K - 1; i >= 0; --i) {
if (h[R1[st][i]] < mx1) {
res += (1 << i);
st = R1[st][i];
}
}
if (h[st] < mx1 && h[R[st]] < mx) {
st = R[st];
++res;
}
return res;
}
#ifdef LOCAL
int main() {
freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int nn, qq;
cin >> nn >> qq;
vector<int> hh(nn);
for (auto &x : hh) cin >> x;
init(nn, hh);
while (qq--) {
int aa, bb, cc, dd;
cin >> aa >> bb >> cc >> dd;
cout << minimum_jumps(aa, bb, cc, dd) << endl;
}
return 0;
}
#endif
# | 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... |