이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
int low[N][18], high[N][18], rjmp[N][18], lef[N], rig[N], inv[N], logs[N], a[N];
pair<int, int> rmq[N][18];
auto Query(int l, int r) {
int o = logs[r - l + 1];
return max(rmq[l][o], rmq[r - (1 << o) + 1][o]);
}
void init(int n, vector<int> A) {
for (int i = 1; i <= n; i++) {
a[i] = A[i - 1];
rmq[i][0] = {a[i], i};
inv[a[i]] = i;
}
for (int j = 1; j < 18; j++) {
for (int i = 1; i <= n - (1 << j) + 1; i++) {
rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
int z = 0;
for (int i = 1; i <= n; i++) {
if (i == (1 << (z + 1))) z += 1;
logs[i] = z;
}
stack<int> s;
for (int i = 1; i <= n; i++) {
while (!s.empty() && a[s.top()] < a[i]) s.pop();
if (!s.empty()) lef[i] = s.top();
s.push(i);
}
while (!s.empty()) s.pop();
for (int i = n; i >= 1; i--) {
while (!s.empty() && a[s.top()] < a[i]) s.pop();
if (!s.empty()) rig[i] = s.top();
s.push(i);
}
for (int i = 1; i <= n; i++) {
if (a[lef[i]] > a[rig[i]]) {
high[i][0] = lef[i];
low[i][0] = rig[i];
}
else {
high[i][0] = rig[i];
low[i][0] = lef[i];
}
rjmp[i][0] = rig[i];
}
for (int ii = n; ii >= 1; ii--) {
int i = inv[ii];
for (int j = 1; j < 18; j++) {
high[i][j] = high[high[i][j - 1]][j - 1];
low[i][j] = low[low[i][j - 1]][j - 1];
}
}
for (int j = 1; j < 18; j++) {
for (int i = 1; i <= n - (1 << j) + 1; i++) {
rjmp[i][j] = rjmp[rjmp[i][j - 1]][j - 1];
}
}
}
int minimum_jumps(int A, int b, int c, int d) {
A += 1; b += 1; c += 1; d += 1;
int koji = Query(c, d).second;
int l = A, r = b, gde = 0;
while (l <= r) {
int mid = l + r >> 1;
if (Query(mid, b).first < a[koji]) {
gde = mid;
r = mid - 1;
}
else l = mid + 1;
}
if (gde == 0) return -1;
gde = Query(gde, b).second;
int ans = 0;
for (int i = 17; i >= 0; i--) {
int x = low[gde][i]; int y = high[gde][i];
if (y && x < c && y < c && y > lef[koji]) {
ans += (1 << i);
gde = y;
}
}
for (int i = 17; i >= 0; i--) {
if (rjmp[gde][i] && rjmp[gde][i] < c) {
ans += (1 << i);
gde = rjmp[gde][i];
}
}
gde = rjmp[gde][0];
ans += 1;
if (gde < c) return -1;
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:74:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | int mid = 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... |