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>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "jumps.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10;
int n, h[N];
int high[N][20];
int L[N][20], R[N][20];
void init(int N, std::vector<int> H) {
n = N;
h[0] = h[n + 1] = 1e9;
for (int i = 1; i <= n; ++i) h[i] = H[i - 1];
stack <int> stk;
for (int i = 1; i <= n; ++i) {
while (stk.size() && h[stk.top()] < h[i]) {
R[stk.top()][0] = i;
stk.pop();
}
stk.push(i);
}
while (stk.size()) R[stk.top()][0] = n + 1, stk.pop();
for (int i = n; i >= 1; --i) {
while (stk.size() && h[stk.top()] < h[i]) {
L[stk.top()][0] = i;
stk.pop();
}
stk.push(i);
}
while (stk.size()) L[stk.top()][0] = 0, stk.pop();
for (int i = 1; i <= n; ++i) {
high[i][0] = (h[L[i][0]] > h[R[i][0]] ? L[i][0] : R[i][0]);
}
for (int lg = 1; (1 << lg) <= n; ++lg) {
for (int i = 1; i <= n; ++i) {
high[i][lg] = high[high[i][lg - 1]][lg - 1];
L[i][lg] = L[L[i][lg - 1]][lg - 1];
R[i][lg] = R[R[i][lg - 1]][lg - 1];
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A, ++B, ++C, ++D;
int cost = 0;
for (int lg = 19; lg >= 0; --lg)
if (L[B][lg] >= A && R[L[B][lg]][0] <= D) B = L[B][lg];
for (int lg = 19; lg >= 0; --lg)
if (high[B][lg] && high[B][lg] <= n && R[high[B][lg]][0] < C) B = high[B][lg], cost += (1 << lg);
if (high[B][0] && high[B][0] <= n && R[B][0] < C && R[high[B][0]][0] <= D) {
B = high[B][0];
++cost;
}
if (B > D) return -1;
for (int lg = 19; lg >= 0; --lg)
if (R[B][lg] && R[B][lg] <= n && R[B][lg] < C) {
B = R[B][lg];
cost += (1 << lg);
}
++cost;
B = R[B][0];
if (B < C || B > D) return -1;
return cost;
}
//
//int main() {
//// freopen("test.inp", "r", stdin);
//// freopen("test.out", "w", stdout);
// int N, Q;
// assert(2 == scanf("%d %d", &N, &Q));
// std::vector<int> H(N);
// for (int i = 0; i < N; ++i) {
// assert(1 == scanf("%d", &H[i]));
// }
// init(N, H);
//
// for (int i = 0; i < Q; ++i) {
// int A, B, C, D;
// assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
// printf("%d\n", minimum_jumps(A, B, C, D));
// }
// return 0;
//}
//
# | 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... |