#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
constexpr int LOGMAX = 20;
constexpr int NMAX = 2e5 + 5;
int st[LOGMAX][NMAX];
int dr[LOGMAX][NMAX];
int bst[LOGMAX][NMAX];
int Lg[NMAX];
int vf;
void init(int N, std::vector<int> H) {
deque <int> D;
Lg[1] = 0;
for (int i = 2; i <= N; ++ i )
Lg[i] = Lg[i/2] + 1;
vf = Lg[N];
for (int i = 0; i <= vf; ++ i )
for (int j = 0; j < N; ++ j )
st[i][j] = dr[i][j] = bst[i][j] = -1;
for (int i = 0; i < N; ++ i ) {
while (!D.empty() && H[D.back()] < H[i]) {
dr[0][D.back()] = i;
D.pop_back();
}
if (!D.empty()) st[0][i] = D.back();
D.push_back(i);
}
for (int i = 0; i < N; ++ i ) {
bst[0][i] = -1;
if (st[0][i] == -1) bst[0][i] = dr[0][i];
else if (dr[0][i] == -1) bst[0][i] = st[0][i];
else {
bst[0][i] = (H[st[0][i]] > H[dr[0][i]] ? st[0][i] : dr[0][i]);
}
}
for (int i = 1; i <= Lg[N]; ++ i ) {
for (int j = 0; j + (1<<i) - 1 < N; ++ j ) {
if (st[i-1][j] == -1) st[i][j] = -1;
else st[i][j] = st[i-1][st[i-1][j]];
if (dr[i-1][j] == -1) dr[i][j] = -1;
else dr[i][j] = dr[i-1][dr[i-1][j]];
if (bst[i-1][j] == -1) bst[i][j] = -1;
else bst[i][j] = bst[i-1][bst[i-1][j]];
}
}
}
int Can (int pos, int C, int D) {
int ans = 0;
if (C <= pos && pos <= D) return 0;
for (int i = vf; i >= 0; -- i ) {
if (dr[i][pos] == -1) continue;
if (dr[i][pos] >= C) continue;
pos = dr[i][pos];
ans += (1<<i);
}
pos = dr[0][pos];
++ ans;
return (pos <= D ? ans : -1);
}
int minimum_jumps(int A, int B, int C, int D) {
int best_pos_AB = B;
if (Can(B, C, D) == -1) return -1;
for (int i = vf; i >= 0; -- i ) {
if (st[i][best_pos_AB] == -1) continue;
if (st[i][best_pos_AB] < A) continue;
int new_pos = st[i][best_pos_AB];
if (Can(new_pos, C, D) >= 0) best_pos_AB = new_pos;
}
int answer = Can(best_pos_AB, C, D);
int contor = 0;
for (int i = vf; i >= 0; -- i ) {
if (bst[i][best_pos_AB] == -1) continue;
if (bst[i][best_pos_AB] >= C) continue;
int new_pos = bst[i][best_pos_AB];
int x;
if ((x = Can(new_pos, C, D)) >= 0) {
contor += (1<<i);
answer = x + contor;
best_pos_AB = bst[i][best_pos_AB];
}
}
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
191 ms |
35996 KB |
Output is correct |
4 |
Correct |
1524 ms |
44852 KB |
Output is correct |
5 |
Correct |
1151 ms |
21760 KB |
Output is correct |
6 |
Correct |
1591 ms |
44988 KB |
Output is correct |
7 |
Correct |
1144 ms |
30940 KB |
Output is correct |
8 |
Correct |
1507 ms |
44932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
191 ms |
35996 KB |
Output is correct |
4 |
Correct |
1524 ms |
44852 KB |
Output is correct |
5 |
Correct |
1151 ms |
21760 KB |
Output is correct |
6 |
Correct |
1591 ms |
44988 KB |
Output is correct |
7 |
Correct |
1144 ms |
30940 KB |
Output is correct |
8 |
Correct |
1507 ms |
44932 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |