이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*************************************
* author: marvinthang *
* created: 21.04.2023 12:23:51 *
*************************************/
#include <bits/stdc++.h>
#include "jumps.h"
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
const int MAX = 2e5 + 5;
const int LOG = 20;
int N, H[MAX], left[MAX], right[MAX][LOG], higher[MAX][LOG], ma[MAX][LOG];
#define MAX_HEIGHT(a, b) (H[(a)] < H[(b)] ? b : a)
void init(int n, vector<int> h) {
N = n;
REP(i, N) H[i + 1] = h[i];
stack <int> st;
st.push(0);
H[0] = H[N + 1] = N + 1;
FORE(i, 1, N) {
while (H[st.top()] < H[i]) st.pop();
left[i] = st.top();
st.push(i);
}
right[0][0] = right[N + 1][0] = N + 1;
st.push(N + 1);
FORDE(i, N, 1) {
while (H[st.top()] < H[i]) st.pop();
right[i][0] = st.top();
st.push(i);
}
FORE(i, 1, N) {
higher[i][0] = MAX_HEIGHT(left[i], right[i][0]);
ma[i][0] = i;
}
FOR(k, 1, LOG) REP(i, N + 2) {
higher[i][k] = higher[higher[i][k - 1]][k - 1];
right[i][k] = right[right[i][k - 1]][k - 1];
if (i + MASK(k) - 1 <= N) ma[i][k] = MAX_HEIGHT(ma[i][k - 1], ma[i + MASK(k - 1)][k - 1]);
}
}
int get_max_height(int l, int r) {
int h = __lg(r - l + 1);
return MAX_HEIGHT(ma[l][h], ma[r - MASK(h) + 1][h]);
}
int minimum_jumps(int A, int B, int C, int D) {
++A; ++B; ++C; ++D;
int p = H[get_max_height(C, D)];
if (H[get_max_height(B, C - 1)] > p) return -1;
int l = A, r = B;
while (l <= r) {
int m = l + r >> 1;
if (H[get_max_height(m, B)] < p) r = m - 1;
else l = m + 1;
}
int start = get_max_height(l, B);
if (C <= right[start][0] && right[start][0] <= D) return 1;
int res = 0;
REPD(i, LOG) if (right[higher[start][i]][0] < C) {
start = higher[start][i];
res ^= MASK(i);
}
int nxt = higher[start][0];
if (right[nxt][0] <= D) {
start = nxt;
++res;
if (C <= right[nxt][0] && right[nxt][0] <= D) return res + 1;
}
REPD(i, LOG) if (right[start][i] < C) {
start = right[start][i];
res += MASK(i);
}
return res + 1;
}
#ifdef LOCAL
#include "jumps.h"
#include <cassert>
#include <cstdio>
#include <vector>
int main() {
file("jumps");
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;
}
#endif
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:93:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
93 | 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... |