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>
using namespace std;
const int N = 2e5 + 7;
int n, h[N];
int l[N], r[N], m[N];
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();
for (int i = 0; i < n; ++i) m[i] = h[l[i]] > h[r[i]] ? l[i] : r[i];
}
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 = 0;
for (int i = c; i <= d; ++i) mx = max(mx, h[i]);
int mx1 = -1e9;
for (int i = b + 1; i < c; ++i) mx1 = max(mx1, h[i]);
if (mx1 > mx || h[b] > mx) return -1;
int st = b;
for (int i = a; i <= b; ++i) {
if (h[i] < mx && h[i] > h[st]) st = i;
}
int res = 1;
while (h[st] < mx1) {
++res;
if (h[m[st]] < h[mx]) st = m[st]; else st = r[st];
}
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... |