이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const int N = 2e5 + 5;
const int L = 18;
int prv[N], nxt[N], n, h[N], sparse[N][L], mx[4 * N];
vector<int> H;
int argmax(int x, int y) {
return H[x] > H[y] ? x : y;
}
void build_tree(int v, int l, int r) {
if (l + 1 == r) {
mx[v] = l;
return;
}
int m = l + r >> 1;
build_tree(v << 1, l, m), build_tree(v << 1 | 1, m, r);
mx[v] = argmax(mx[v << 1], mx[v << 1 | 1]);
}
int get_mx(int v, int l, int r, int L, int R) {
if (R <= l || r <= L || R <= L) return n;
if (L <= l && r <= R) return mx[v];
int m = l + r >> 1;
return argmax(get_mx(v << 1, l, m, L, R), get_mx(v << 1 | 1, m, r, L, R));
}
void init(int _n, std::vector<int> _H) {
n = _n;
H.swap(_H);
build_tree(1, 0, n);
H.push_back(-1);
vector<int> st;
FOR(i, 0, n) {
while (st.size() && H[st.back()] <= H[i]) st.pop_back();
prv[i] = (st.empty() ? -1 : st.back());
st.push_back(i);
}
st.clear();
for (int i = n - 1; i >= 0; i--) {
while (st.size() && H[st.back()] <= H[i]) st.pop_back();
nxt[i] = (st.empty() ? n : st.back());
st.push_back(i);
}
for (int i = n - 1; i >= 0; i--) h[i] = h[nxt[i]] + 1;
for (int i = 0; i < n; i++) {
int x = (prv[i] == -1 ? -1 : H[prv[i]]), y = (nxt[i] == n ? -1 : H[nxt[i]]);
if (y == -1 && x == -1) sparse[i][0] = i;
else sparse[i][0] = (x > y ? prv[i] : nxt[i]);
}
FOR(j, 1, L) FOR(i, 0, n) sparse[i][j] = sparse[sparse[i][j - 1]][j - 1];
}
int minimum_jumps(int A, int B, int C, int D) {
int mxCD = get_mx(1, 0, n, C, D + 1);
A = max(A, prv[mxCD] + 1);
int x = get_mx(1, 0, n, A, B + 1);
if (x == n) return -1;
int mxV = get_mx(1, 0, n, B + 1, C);
if (H[x] > H[mxV]) return 1;
int t = 0;
for (int i = L - 1; i >= 0; i--) if (H[sparse[x][i]] < mxV) {
x = sparse[x][i];
t |= 1 << i;
}
return t + 2;
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'void build_tree(int, int, int)':
jumps.cpp:19:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int get_mx(int, int, int, int, int)':
jumps.cpp:26:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int m = l + r >> 1;
| ~~^~~
# | 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... |