#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 + (1 << lg) - 1 <= 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] && R[high[B][0]][0] <= D) {
B = high[B][0];
++cost;
}
assert(R[B][0]);
for (int lg = 19; lg >= 0; --lg)
if (B < C && R[B][lg] && R[B][lg] <= D) {
B = R[B][lg];
cost += (1 << lg);
}
if (B > D || B < C) return -1;
return cost;
}
//
//int main() {
// 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;
//}
//
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Incorrect |
189 ms |
40288 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Runtime error |
2 ms |
420 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Runtime error |
2 ms |
420 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Incorrect |
189 ms |
40288 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |