#include "jumps.h"
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int best[200005][25], rightt[200005][25], h[200005], n, tallest[2000000], L[200005];
void update(int id, int l, int r, int pos, int val){
if(h[val] > h[tallest[id]]){
tallest[id] = val;
}
if(l == r){
return;
}
int m = l + r >> 1;
if(m >= pos){
update(id * 2, l, m, pos, val);
}
else update(id * 2 + 1, m + 1, r, pos, val);
}
int query(int id, int l, int r, int lef, int rig){
if(lef > rig) return 200004;
if(r < lef || rig < l) return 200004;
if(lef <= l && r <= rig) return tallest[id];
int m = l + r >> 1;
int lll = query(id * 2, l, m, lef, rig);
int rrr = query(id * 2 + 1, m + 1, r, lef, rig);
return (h[lll] > h[rrr] ? lll : rrr);
}
void init(int N, vector<int> H){
n = N;
for(int i = 1; i <= n; i++){
h[i] = H[i - 1];
update(1, 1, n, i, i);
}
stack<int> s, t;
h[n + 1] = 1e9;
s.push(n + 1);
for(int i = n; i >= 1; i--){
while(h[s.top()] < h[i]){
s.pop();
}
rightt[i][0] = s.top();
s.push(i);
}
t.push(0);
h[0] = 1e9;
for(int i = 1; i <= n; i++){
while(h[t.top()] < h[i]){
t.pop();
}
L[i] = t.top();
t.push(i);
}
for(int i = 1; i <= n; i++){
if(h[L[i]] < 1e9 && h[rightt[i][0]] < 1e9){
best[i][0] = (h[L[i]] > h[rightt[i][0]] ? L[i] : rightt[i][0]);
}
else{
if(h[L[i]] < 1e9){
best[i][0] = L[i];
}
else{
if(h[rightt[i][0]] < 1e9){
best[i][0] = rightt[i][0];
}
else best[i][0] = 0;
}
}
}
rightt[n + 1][0] = n + 1;
for(int j = 1; j <= 20; j++){
for(int i = 1; i <= n + 1; i++){
rightt[i][j] = rightt[rightt[i][j - 1]][j - 1];
best[i][j] = best[best[i][j - 1]][j - 1];
}
}
}
int minimum_jumps(int A, int B, int C, int D){
A++, B++, C++, D++;
int x = query(1, 1, n, B + 1, C - 1);
int y = query(1, 1, n, C, D);
// cout << x << ' ' << y << '\n';
if(h[x] > h[y]) return -1;
if(h[B] > h[y]) return -1;
int z = query(1, 1, n, A, B);
if(h[z] > h[y]){
int pos = query(1, 1, n, z + 1, B);
int ans = 0;
for(int i = 20; i >= 0; i--){
if(C > rightt[pos][i]){
ans += (1 << i);
pos = rightt[pos][i];
}
}
return ans + 1;
}
else{
int ans = 0;
int pos = z;
for(int i = 20; i >= 0; i--){
if(h[best[pos][i]] < h[x]){
pos = best[pos][i];
ans += (1 << i);
}
}
// cout << best[pos][0] << ' ' << ans << '\n';
if(h[best[pos][0]] < h[y]){
ans += 2;
}
else{
for(int i = 20; i >= 0; i--){
// cout << pos << '\n';
if(C > rightt[pos][i]){
ans += (1 << i);
pos = rightt[pos][i];
}
}
ans++;
}
return ans;
}
}
Compilation message
jumps.cpp: In function 'void update(int, int, int, int, int)':
jumps.cpp:14:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
14 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int query(int, int, int, int, int)':
jumps.cpp:25:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int m = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Incorrect |
167 ms |
36628 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
208 KB |
Output is correct |
5 |
Incorrect |
2 ms |
208 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
208 KB |
Output is correct |
5 |
Incorrect |
2 ms |
208 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
97 ms |
36364 KB |
Output is correct |
6 |
Correct |
139 ms |
44592 KB |
Output is correct |
7 |
Correct |
60 ms |
22856 KB |
Output is correct |
8 |
Correct |
116 ms |
44592 KB |
Output is correct |
9 |
Correct |
11 ms |
6864 KB |
Output is correct |
10 |
Correct |
127 ms |
44592 KB |
Output is correct |
11 |
Correct |
122 ms |
45344 KB |
Output is correct |
12 |
Incorrect |
116 ms |
45108 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
182 ms |
20668 KB |
Output is correct |
5 |
Correct |
956 ms |
44584 KB |
Output is correct |
6 |
Correct |
641 ms |
7760 KB |
Output is correct |
7 |
Correct |
1113 ms |
44508 KB |
Output is correct |
8 |
Correct |
688 ms |
15780 KB |
Output is correct |
9 |
Correct |
1014 ms |
44668 KB |
Output is correct |
10 |
Correct |
1022 ms |
45376 KB |
Output is correct |
11 |
Correct |
981 ms |
45348 KB |
Output is correct |
12 |
Correct |
1211 ms |
45224 KB |
Output is correct |
13 |
Correct |
1053 ms |
44584 KB |
Output is correct |
14 |
Correct |
1180 ms |
45048 KB |
Output is correct |
15 |
Correct |
1042 ms |
45448 KB |
Output is correct |
16 |
Correct |
1082 ms |
45448 KB |
Output is correct |
17 |
Correct |
0 ms |
208 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
4 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
19 ms |
720 KB |
Output is correct |
28 |
Correct |
17 ms |
720 KB |
Output is correct |
29 |
Correct |
28 ms |
720 KB |
Output is correct |
30 |
Correct |
18 ms |
720 KB |
Output is correct |
31 |
Correct |
11 ms |
720 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
76 ms |
25800 KB |
Output is correct |
34 |
Correct |
115 ms |
44592 KB |
Output is correct |
35 |
Correct |
121 ms |
45348 KB |
Output is correct |
36 |
Correct |
118 ms |
44600 KB |
Output is correct |
37 |
Correct |
120 ms |
44944 KB |
Output is correct |
38 |
Correct |
104 ms |
45460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
182 ms |
20668 KB |
Output is correct |
5 |
Correct |
956 ms |
44584 KB |
Output is correct |
6 |
Correct |
641 ms |
7760 KB |
Output is correct |
7 |
Correct |
1113 ms |
44508 KB |
Output is correct |
8 |
Correct |
688 ms |
15780 KB |
Output is correct |
9 |
Correct |
1014 ms |
44668 KB |
Output is correct |
10 |
Correct |
1022 ms |
45376 KB |
Output is correct |
11 |
Correct |
981 ms |
45348 KB |
Output is correct |
12 |
Correct |
1211 ms |
45224 KB |
Output is correct |
13 |
Correct |
1053 ms |
44584 KB |
Output is correct |
14 |
Correct |
1180 ms |
45048 KB |
Output is correct |
15 |
Correct |
1042 ms |
45448 KB |
Output is correct |
16 |
Correct |
1082 ms |
45448 KB |
Output is correct |
17 |
Correct |
0 ms |
208 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
4 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
19 ms |
720 KB |
Output is correct |
28 |
Correct |
17 ms |
720 KB |
Output is correct |
29 |
Correct |
28 ms |
720 KB |
Output is correct |
30 |
Correct |
18 ms |
720 KB |
Output is correct |
31 |
Correct |
11 ms |
720 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
76 ms |
25800 KB |
Output is correct |
34 |
Correct |
115 ms |
44592 KB |
Output is correct |
35 |
Correct |
121 ms |
45348 KB |
Output is correct |
36 |
Correct |
118 ms |
44600 KB |
Output is correct |
37 |
Correct |
120 ms |
44944 KB |
Output is correct |
38 |
Correct |
104 ms |
45460 KB |
Output is correct |
39 |
Correct |
1 ms |
208 KB |
Output is correct |
40 |
Correct |
0 ms |
208 KB |
Output is correct |
41 |
Correct |
1 ms |
208 KB |
Output is correct |
42 |
Correct |
274 ms |
20648 KB |
Output is correct |
43 |
Correct |
1014 ms |
44600 KB |
Output is correct |
44 |
Correct |
690 ms |
7712 KB |
Output is correct |
45 |
Correct |
716 ms |
44720 KB |
Output is correct |
46 |
Correct |
606 ms |
15772 KB |
Output is correct |
47 |
Correct |
905 ms |
44628 KB |
Output is correct |
48 |
Correct |
983 ms |
45388 KB |
Output is correct |
49 |
Correct |
1059 ms |
45344 KB |
Output is correct |
50 |
Correct |
1049 ms |
45340 KB |
Output is correct |
51 |
Correct |
795 ms |
44564 KB |
Output is correct |
52 |
Correct |
1100 ms |
45036 KB |
Output is correct |
53 |
Correct |
934 ms |
45384 KB |
Output is correct |
54 |
Correct |
943 ms |
45444 KB |
Output is correct |
55 |
Correct |
1 ms |
208 KB |
Output is correct |
56 |
Incorrect |
199 ms |
44508 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Incorrect |
167 ms |
36628 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |