#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], low[N][20];
int L[N], R[N];
void init(int N, std::vector<int> H) {
n = N;
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()] = i;
stk.pop();
}
stk.push(i);
}
while (stk.size()) R[stk.top()] = n + 1, stk.pop();
for (int i = n; i >= 1; --i) {
while (stk.size() && h[stk.top()] < h[i]) {
L[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
while (stk.size()) L[stk.top()] = 0, stk.pop();
for (int i = 1; i <= n; ++i) {
high[i][0] = (h[L[i]] > h[R[i]] ? L[i] : R[i]);
low[i][0] = (h[L[i]] < h[R[i]] ? L[i] : R[i]);
}
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];
low[i][lg] = low[low[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 (h[high[A][lg]] && h[high[A][lg]] <= h[C])
A = high[A][lg],
cost += (1 << lg);
for (int lg = 19; lg >= 0; --lg)
if (h[low[A][lg]] && h[low[A][lg]] <= h[C])
A = low[A][lg],
cost += (1 << lg);
if (A != 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 |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Incorrect |
145 ms |
28992 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
2 |
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 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
204 ms |
16436 KB |
Output is correct |
5 |
Correct |
569 ms |
35516 KB |
Output is correct |
6 |
Correct |
586 ms |
6056 KB |
Output is correct |
7 |
Correct |
889 ms |
35480 KB |
Output is correct |
8 |
Correct |
522 ms |
12356 KB |
Output is correct |
9 |
Correct |
839 ms |
35392 KB |
Output is correct |
10 |
Correct |
945 ms |
36348 KB |
Output is correct |
11 |
Correct |
843 ms |
36080 KB |
Output is correct |
12 |
Correct |
843 ms |
36232 KB |
Output is correct |
13 |
Correct |
707 ms |
35392 KB |
Output is correct |
14 |
Correct |
695 ms |
35852 KB |
Output is correct |
15 |
Correct |
876 ms |
36296 KB |
Output is correct |
16 |
Correct |
690 ms |
36276 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
3 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
3 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
0 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
17 ms |
592 KB |
Output is correct |
28 |
Correct |
24 ms |
592 KB |
Output is correct |
29 |
Correct |
17 ms |
592 KB |
Output is correct |
30 |
Correct |
23 ms |
592 KB |
Output is correct |
31 |
Correct |
16 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
42 ms |
20596 KB |
Output is correct |
34 |
Correct |
76 ms |
35492 KB |
Output is correct |
35 |
Correct |
75 ms |
36168 KB |
Output is correct |
36 |
Correct |
82 ms |
35520 KB |
Output is correct |
37 |
Correct |
85 ms |
35880 KB |
Output is correct |
38 |
Correct |
71 ms |
36244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
204 ms |
16436 KB |
Output is correct |
5 |
Correct |
569 ms |
35516 KB |
Output is correct |
6 |
Correct |
586 ms |
6056 KB |
Output is correct |
7 |
Correct |
889 ms |
35480 KB |
Output is correct |
8 |
Correct |
522 ms |
12356 KB |
Output is correct |
9 |
Correct |
839 ms |
35392 KB |
Output is correct |
10 |
Correct |
945 ms |
36348 KB |
Output is correct |
11 |
Correct |
843 ms |
36080 KB |
Output is correct |
12 |
Correct |
843 ms |
36232 KB |
Output is correct |
13 |
Correct |
707 ms |
35392 KB |
Output is correct |
14 |
Correct |
695 ms |
35852 KB |
Output is correct |
15 |
Correct |
876 ms |
36296 KB |
Output is correct |
16 |
Correct |
690 ms |
36276 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
3 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
3 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
0 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
17 ms |
592 KB |
Output is correct |
28 |
Correct |
24 ms |
592 KB |
Output is correct |
29 |
Correct |
17 ms |
592 KB |
Output is correct |
30 |
Correct |
23 ms |
592 KB |
Output is correct |
31 |
Correct |
16 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
42 ms |
20596 KB |
Output is correct |
34 |
Correct |
76 ms |
35492 KB |
Output is correct |
35 |
Correct |
75 ms |
36168 KB |
Output is correct |
36 |
Correct |
82 ms |
35520 KB |
Output is correct |
37 |
Correct |
85 ms |
35880 KB |
Output is correct |
38 |
Correct |
71 ms |
36244 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
0 ms |
208 KB |
Output is correct |
41 |
Correct |
0 ms |
208 KB |
Output is correct |
42 |
Correct |
251 ms |
16408 KB |
Output is correct |
43 |
Correct |
813 ms |
35400 KB |
Output is correct |
44 |
Correct |
600 ms |
6096 KB |
Output is correct |
45 |
Correct |
834 ms |
35436 KB |
Output is correct |
46 |
Correct |
520 ms |
12336 KB |
Output is correct |
47 |
Correct |
841 ms |
35400 KB |
Output is correct |
48 |
Correct |
798 ms |
36244 KB |
Output is correct |
49 |
Correct |
982 ms |
36284 KB |
Output is correct |
50 |
Correct |
938 ms |
36120 KB |
Output is correct |
51 |
Correct |
918 ms |
35580 KB |
Output is correct |
52 |
Correct |
977 ms |
35828 KB |
Output is correct |
53 |
Correct |
812 ms |
36388 KB |
Output is correct |
54 |
Correct |
869 ms |
36280 KB |
Output is correct |
55 |
Correct |
0 ms |
208 KB |
Output is correct |
56 |
Incorrect |
118 ms |
35388 KB |
Output isn't correct |
57 |
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 |
Incorrect |
145 ms |
28992 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |