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;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 200005;
int P[MAXN][18], Pi[MAXN][18], Pa[MAXN][18];
int h[MAXN], dist[MAXN], nxt[MAXN], prv[MAXN], nArr;
bool check_sub1;
void init(int _N, vector<int> _H) {
nArr = _N;
check_sub1 = 1;
for (int i = 1; i <= nArr; ++i) {
h[i] = _H[i - 1];
check_sub1 &= (i == 1 || h[i] > h[i - 1]);
}
stack<int> st;
for (int i = 1; i <= nArr; ++i) {
while(sz(st) && h[st.top()] <= h[i])
st.pop();
prv[i] = (sz(st)) ? st.top() : 0;
st.push(i);
}
while(sz(st))
st.pop();
for (int i = nArr; i > 0; --i) {
while(sz(st) && h[st.top()] <= h[i])
st.pop();
nxt[i] = (sz(st)) ? st.top() : nArr + 1;
st.push(i);
}
for (int i = 1; i <= nArr; ++i) {
Pi[i][0] = Pa[i][0] = h[i];
P[i][0] = (h[nxt[i]] >= h[prv[i]]) ? nxt[i] : prv[i];
}
P[0][0] = 0, P[nArr + 1][0] = nArr + 1;
for (int j = 1; (1 << j) <= nArr; ++j) {
for (int i = 0; i <= nArr + 1; ++i) {
P[i][j] = P[P[i][j - 1]][j - 1];
if(i + (1 << j) - 1 <= nArr) {
Pi[i][j] = min(Pi[i][j - 1], Pi[i + (1 << (j - 1))][j - 1]);
Pa[i][j] = max(Pa[i][j - 1], Pa[i + (1 << (j - 1))][j - 1]);
}
}
}
}
int brute(int a, int b, int c, int d) {
for (int i = 1; i <= nArr; ++i)
dist[i] = -1;
queue<int> qu;
for (int i = a; i <= b; ++i) {
qu.push(i);
dist[i] = 0;
}
while(sz(qu)) {
int u(qu.front()); qu.pop();
if(u >= c && u <= d)
return dist[u];
if(prv[u] > 0 && dist[prv[u]] < 0) {
dist[prv[u]] = dist[u] + 1;
qu.push(prv[u]);
}
if(nxt[u] <= nArr && dist[nxt[u]] < 0) {
dist[nxt[u]] = dist[u] + 1;
qu.push(nxt[u]);
}
}
return -1;
}
inline int getMin(int l, int r) {
int log = 31 - __builtin_clz(r - l + 1);
return min(Pi[l][log], Pi[r - (1 << log) + 1][log]);
}
inline int getMax(int l, int r) {
int log = 31 - __builtin_clz(r - l + 1);
return max(Pa[l][log], Pa[r - (1 << log) + 1][log]);
}
int sub5(int a, int c) {
int res(0);
for (int i = 31 - __builtin_clz(nArr); i >= 0; --i) {
if(P[a][i] != 0 && P[a][i] != nArr + 1 && h[P[a][i]] < h[c]) {
a = P[a][i];
res += (1 << i);
}
}
if(h[a] < h[c]) {
a = nxt[a];
assert(a == c);
++res;
}
return res;
}
int minimum_jumps(int a, int b, int c, int d) {
++a, ++b, ++c, ++d;
if(check_sub1)
return c - b;
int minStart = getMin(a, b), maxBetween(-1), maxEnd = getMax(c, d);
if(b + 1 <= c - 1)
maxBetween = getMax(b + 1, c - 1);
if(minStart >= maxEnd || maxBetween >= maxEnd)
return -1;
if(a == b && c == d)
return sub5(a, c);
return brute(a, b, c, d);
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "jumps"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> h;
int n, q;
cin >> n >> q;
h.resize(n);
for (int i = 0; i < n; ++i)
cin >> h[i];
init(n, h);
for (int t = 0; t < q; ++t) {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << "ANSWER " << a << ' ' << b << ' ' << c << ' ' << d << ": " << minimum_jumps(a, b, c, d) << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# | 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... |