#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) "MJ >> LAMELO"
#endif
template <typename T, class F = function<T(const T&, const T&)>>
struct sparse_table {
int n;
vector<vector<T>> mat;
F func;
sparse_table() {}
sparse_table(const vector<T>& a, const F& f) : func(f) {
n = static_cast<int>(a.size());
int max_log = 32 - __builtin_clz(n);
mat.resize(max_log);
mat[0] = a;
for (int j = 1; j < max_log; j++) {
mat[j].resize(n - (1 << j) + 1);
for (int i = 0; i <= n - (1 << j); i++) {
mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
}
}
}
T get(int L, int R) const {
assert(0 <= L && L <= R && R <= n - 1);
int lg = 32 - __builtin_clz(R - L + 1) - 1;
return func(mat[lg][L], mat[lg][R - (1 << lg) + 1]);
}
};
const int lg = 20;
vector<int> h;
vector<vector<int>> g;
vector<vector<int>> go_low;
vector<vector<int>> go_high;
vector<vector<int>> go_right;
sparse_table<int> st;
int n;
void init(int N, vector<int> H) {
n = N;
h = H;
h.push_back(1e9);
g.resize(n + 1);
vector<int> t;
for (int i = 0; i < n; i++) {
while (!t.empty() && h[i] > h[t.back()]) {
t.pop_back();
}
if (!t.empty()) {
g[i].push_back(t.back());
}
t.push_back(i);
}
go_right = vector<vector<int>>(n + 1, vector<int>(lg));
t.clear();
for (int i = n - 1; i >= 0; i--) {
go_right[i][0] = i;
while (!t.empty() && h[i] > h[t.back()]) {
t.pop_back();
}
if (!t.empty()) {
g[i].push_back(t.back());
go_right[i][0] = t.back();
}
t.push_back(i);
}
for (int i = 0; i < n; i++) {
if (g[i].size() == 2 && h[g[i][0]] > h[g[i][1]]) {
swap(g[i][0], g[i][1]);
}
}
go_low = vector<vector<int>>(n + 1, vector<int>(lg));
go_high = vector<vector<int>>(n + 1, vector<int>(lg));
for (int i = 0; i <= n; i++) {
go_low[i][0] = go_high[i][0] = n;
if (g[i].size() > 0) {
go_low[i][0] = g[i][0];
}
if (g[i].size() > 1){
go_high[i][0] = g[i][1];
}
}
for (int b = 1; b < lg; b++) {
for (int i = 0; i <= n; i++) {
go_low[i][b] = go_low[go_low[i][b - 1]][b - 1];
go_high[i][b] = go_high[go_high[i][b - 1]][b - 1];
go_right[i][b] = go_right[go_right[i][b - 1]][b - 1];
}
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
st = sparse_table<int>(p, [&](int i, int j) { return h[i] > h[j] ? i : j; });
}
int minimum_jumps(int a, int b, int c, int d) {
int x = st.get(c, d);
if (h[st.get(b, c - 1)] > h[x]) {
return -1;
}
int l = a;
int r = b;
while (l < r) {
int mid = (l + r) >> 1;
int pos = st.get(mid, b);
if (h[pos] > h[x]) {
l = mid + 1;
} else {
r = mid;
}
}
int s = st.get(l, b);
l = c;
r = d;
while (l < r) {
int mid = (l + r) >> 1;
int pos = st.get(c, mid);
if (h[pos] > h[s]) {
r = mid;
} else {
l = mid + 1;
}
}
int t = l;
int ans = 0;
for (int b = lg - 1; b >= 0; b--) {
int ns = go_high[s][b];
if (h[ns] < h[t]) {
s = ns;
ans += (1 << b);
}
}
assert(s < c);
if ((c <= go_low[s][0] && go_low[s][0] <= d) || (c <= go_high[s][0] && go_high[s][0] <= d)) {
return ans + 1;
}
if (h[go_high[s][0]] < h[x]) {
return ans + 2;
}
assert(s < c);
assert(h[s] < h[x]);
assert(go_high[s][0] < s || go_high[s][0] == n);
for (int b = lg - 1; b >= 0; b--) {
int ns = go_low[s][b];
if (h[ns] <= h[t]) {
s = ns;
ans += (1 << b);
}
}
return ans;
}
# |
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 |
275 ms |
78368 KB |
Output is correct |
4 |
Correct |
1444 ms |
98668 KB |
Output is correct |
5 |
Correct |
1041 ms |
49420 KB |
Output is correct |
6 |
Correct |
1539 ms |
98696 KB |
Output is correct |
7 |
Correct |
1163 ms |
67128 KB |
Output is correct |
8 |
Correct |
1474 ms |
98668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
2 ms |
208 KB |
Output is correct |
6 |
Incorrect |
3 ms |
336 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
2 ms |
208 KB |
Output is correct |
6 |
Incorrect |
3 ms |
336 KB |
Output isn't correct |
7 |
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 |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
193 ms |
78560 KB |
Output is correct |
6 |
Correct |
207 ms |
97820 KB |
Output is correct |
7 |
Correct |
103 ms |
49820 KB |
Output is correct |
8 |
Correct |
230 ms |
97872 KB |
Output is correct |
9 |
Correct |
29 ms |
14576 KB |
Output is correct |
10 |
Correct |
200 ms |
97848 KB |
Output is correct |
11 |
Correct |
196 ms |
98648 KB |
Output is correct |
12 |
Correct |
203 ms |
98364 KB |
Output is correct |
13 |
Correct |
212 ms |
98340 KB |
Output is correct |
14 |
Correct |
212 ms |
97884 KB |
Output is correct |
15 |
Correct |
243 ms |
98316 KB |
Output is correct |
16 |
Correct |
210 ms |
98700 KB |
Output is correct |
17 |
Correct |
241 ms |
98592 KB |
Output is correct |
18 |
Correct |
0 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
21 |
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 |
285 ms |
44444 KB |
Output is correct |
5 |
Correct |
1172 ms |
97876 KB |
Output is correct |
6 |
Correct |
546 ms |
15940 KB |
Output is correct |
7 |
Correct |
913 ms |
97804 KB |
Output is correct |
8 |
Correct |
553 ms |
33464 KB |
Output is correct |
9 |
Correct |
1228 ms |
97804 KB |
Output is correct |
10 |
Correct |
1328 ms |
98668 KB |
Output is correct |
11 |
Correct |
1142 ms |
98576 KB |
Output is correct |
12 |
Correct |
1213 ms |
98564 KB |
Output is correct |
13 |
Correct |
1089 ms |
97872 KB |
Output is correct |
14 |
Correct |
1335 ms |
98276 KB |
Output is correct |
15 |
Correct |
1209 ms |
98704 KB |
Output is correct |
16 |
Correct |
1162 ms |
98604 KB |
Output is correct |
17 |
Correct |
1 ms |
208 KB |
Output is correct |
18 |
Correct |
0 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
208 KB |
Output is correct |
20 |
Correct |
3 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 |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
17 ms |
1104 KB |
Output is correct |
28 |
Correct |
23 ms |
1104 KB |
Output is correct |
29 |
Correct |
19 ms |
1104 KB |
Output is correct |
30 |
Correct |
23 ms |
1212 KB |
Output is correct |
31 |
Correct |
20 ms |
1104 KB |
Output is correct |
32 |
Correct |
0 ms |
208 KB |
Output is correct |
33 |
Correct |
127 ms |
56344 KB |
Output is correct |
34 |
Correct |
209 ms |
97876 KB |
Output is correct |
35 |
Correct |
215 ms |
98556 KB |
Output is correct |
36 |
Correct |
202 ms |
97876 KB |
Output is correct |
37 |
Correct |
250 ms |
98276 KB |
Output is correct |
38 |
Correct |
193 ms |
98584 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 |
285 ms |
44444 KB |
Output is correct |
5 |
Correct |
1172 ms |
97876 KB |
Output is correct |
6 |
Correct |
546 ms |
15940 KB |
Output is correct |
7 |
Correct |
913 ms |
97804 KB |
Output is correct |
8 |
Correct |
553 ms |
33464 KB |
Output is correct |
9 |
Correct |
1228 ms |
97804 KB |
Output is correct |
10 |
Correct |
1328 ms |
98668 KB |
Output is correct |
11 |
Correct |
1142 ms |
98576 KB |
Output is correct |
12 |
Correct |
1213 ms |
98564 KB |
Output is correct |
13 |
Correct |
1089 ms |
97872 KB |
Output is correct |
14 |
Correct |
1335 ms |
98276 KB |
Output is correct |
15 |
Correct |
1209 ms |
98704 KB |
Output is correct |
16 |
Correct |
1162 ms |
98604 KB |
Output is correct |
17 |
Correct |
1 ms |
208 KB |
Output is correct |
18 |
Correct |
0 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
208 KB |
Output is correct |
20 |
Correct |
3 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 |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
17 ms |
1104 KB |
Output is correct |
28 |
Correct |
23 ms |
1104 KB |
Output is correct |
29 |
Correct |
19 ms |
1104 KB |
Output is correct |
30 |
Correct |
23 ms |
1212 KB |
Output is correct |
31 |
Correct |
20 ms |
1104 KB |
Output is correct |
32 |
Correct |
0 ms |
208 KB |
Output is correct |
33 |
Correct |
127 ms |
56344 KB |
Output is correct |
34 |
Correct |
209 ms |
97876 KB |
Output is correct |
35 |
Correct |
215 ms |
98556 KB |
Output is correct |
36 |
Correct |
202 ms |
97876 KB |
Output is correct |
37 |
Correct |
250 ms |
98276 KB |
Output is correct |
38 |
Correct |
193 ms |
98584 KB |
Output is correct |
39 |
Correct |
0 ms |
208 KB |
Output is correct |
40 |
Correct |
0 ms |
208 KB |
Output is correct |
41 |
Correct |
0 ms |
208 KB |
Output is correct |
42 |
Correct |
263 ms |
44520 KB |
Output is correct |
43 |
Correct |
1068 ms |
97888 KB |
Output is correct |
44 |
Correct |
624 ms |
16040 KB |
Output is correct |
45 |
Correct |
1105 ms |
97876 KB |
Output is correct |
46 |
Correct |
671 ms |
33464 KB |
Output is correct |
47 |
Correct |
1188 ms |
97804 KB |
Output is correct |
48 |
Correct |
1430 ms |
98672 KB |
Output is correct |
49 |
Correct |
1217 ms |
98616 KB |
Output is correct |
50 |
Correct |
1225 ms |
98560 KB |
Output is correct |
51 |
Correct |
975 ms |
97804 KB |
Output is correct |
52 |
Correct |
1190 ms |
98272 KB |
Output is correct |
53 |
Correct |
1188 ms |
98688 KB |
Output is correct |
54 |
Correct |
895 ms |
98684 KB |
Output is correct |
55 |
Correct |
1 ms |
208 KB |
Output is correct |
56 |
Correct |
266 ms |
97496 KB |
Output is correct |
57 |
Correct |
1057 ms |
97840 KB |
Output is correct |
58 |
Correct |
587 ms |
17056 KB |
Output is correct |
59 |
Correct |
1183 ms |
97848 KB |
Output is correct |
60 |
Correct |
404 ms |
34500 KB |
Output is correct |
61 |
Correct |
1080 ms |
97976 KB |
Output is correct |
62 |
Correct |
1179 ms |
98672 KB |
Output is correct |
63 |
Correct |
1050 ms |
98476 KB |
Output is correct |
64 |
Correct |
1118 ms |
98348 KB |
Output is correct |
65 |
Correct |
1222 ms |
97808 KB |
Output is correct |
66 |
Correct |
1374 ms |
98192 KB |
Output is correct |
67 |
Correct |
1243 ms |
98564 KB |
Output is correct |
68 |
Correct |
970 ms |
98568 KB |
Output is correct |
69 |
Correct |
0 ms |
208 KB |
Output is correct |
70 |
Correct |
1 ms |
208 KB |
Output is correct |
71 |
Correct |
3 ms |
336 KB |
Output is correct |
72 |
Correct |
3 ms |
336 KB |
Output is correct |
73 |
Correct |
3 ms |
336 KB |
Output is correct |
74 |
Correct |
2 ms |
336 KB |
Output is correct |
75 |
Correct |
2 ms |
336 KB |
Output is correct |
76 |
Correct |
0 ms |
208 KB |
Output is correct |
77 |
Correct |
0 ms |
208 KB |
Output is correct |
78 |
Correct |
2 ms |
208 KB |
Output is correct |
79 |
Correct |
2 ms |
336 KB |
Output is correct |
80 |
Correct |
2 ms |
336 KB |
Output is correct |
81 |
Correct |
2 ms |
336 KB |
Output is correct |
82 |
Correct |
3 ms |
336 KB |
Output is correct |
83 |
Correct |
3 ms |
336 KB |
Output is correct |
84 |
Correct |
1 ms |
336 KB |
Output is correct |
85 |
Correct |
3 ms |
336 KB |
Output is correct |
86 |
Correct |
21 ms |
1208 KB |
Output is correct |
87 |
Correct |
18 ms |
1232 KB |
Output is correct |
88 |
Correct |
20 ms |
1204 KB |
Output is correct |
89 |
Correct |
13 ms |
1104 KB |
Output is correct |
90 |
Correct |
15 ms |
1196 KB |
Output is correct |
91 |
Correct |
1 ms |
336 KB |
Output is correct |
92 |
Correct |
2 ms |
592 KB |
Output is correct |
93 |
Correct |
21 ms |
1208 KB |
Output is correct |
94 |
Correct |
16 ms |
1232 KB |
Output is correct |
95 |
Correct |
19 ms |
1104 KB |
Output is correct |
96 |
Correct |
21 ms |
1104 KB |
Output is correct |
97 |
Correct |
19 ms |
1212 KB |
Output is correct |
98 |
Correct |
1 ms |
208 KB |
Output is correct |
99 |
Correct |
212 ms |
97692 KB |
Output is correct |
100 |
Correct |
213 ms |
97788 KB |
Output is correct |
101 |
Correct |
197 ms |
98408 KB |
Output is correct |
102 |
Correct |
230 ms |
97800 KB |
Output is correct |
103 |
Correct |
265 ms |
98276 KB |
Output is correct |
104 |
Correct |
193 ms |
98676 KB |
Output is correct |
105 |
Correct |
0 ms |
208 KB |
Output is correct |
106 |
Correct |
127 ms |
56332 KB |
Output is correct |
107 |
Correct |
203 ms |
97884 KB |
Output is correct |
108 |
Correct |
285 ms |
98640 KB |
Output is correct |
109 |
Correct |
316 ms |
97888 KB |
Output is correct |
110 |
Correct |
393 ms |
98276 KB |
Output is correct |
111 |
Correct |
290 ms |
98640 KB |
Output is correct |
# |
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 |
275 ms |
78368 KB |
Output is correct |
4 |
Correct |
1444 ms |
98668 KB |
Output is correct |
5 |
Correct |
1041 ms |
49420 KB |
Output is correct |
6 |
Correct |
1539 ms |
98696 KB |
Output is correct |
7 |
Correct |
1163 ms |
67128 KB |
Output is correct |
8 |
Correct |
1474 ms |
98668 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Incorrect |
3 ms |
336 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |