이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 10;
int n;
int low[Nmax][20], high[Nmax][20];
int L[Nmax], R[Nmax];
int h[Nmax];
void init(int N, 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()) 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);
}
for (int i = 1; i <= n; ++i) {
if (h[L[i]] < h[R[i]]) {
high[i][0] = R[i];
low[i][0] = L[i];
} else {
high[i][0] = L[i];
low[i][0] = R[i];
}
}
for (int lg = 1; (1 << lg) <= n; ++lg) {
for (int i = 1; i <= n; ++i) {
low[i][lg] = low[low[i][lg - 1]][lg - 1];
high[i][lg] = high[high[i][lg - 1]][lg - 1];
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A, ++B, ++C, ++D;
// asume that A = B and C = D
int need = 0;
for (int i = 19; i >= 0; --i) {
if (high[A][i] && h[high[A][i]] <= h[C]) {
need += (1 << i);
A = high[A][i];
}
}
if (A == C) return need;
for (int i = 19; i >= 0; --i) {
if (low[A][i] && h[low[A][i]] <= h[C]) {
need += (1 << i);
A = low[A][i];
}
}
if (A != C) return -1;
return need;
}
//
//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;
//}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |