이 제출은 이전 버전의 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], 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];
}
}
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];
}
}
}
int Dist(int p, int q) {
int d = 0;
for (int i = 17; i >= 0; i--) {
if (high[p][i] && a[high[p][i]] <= a[q]) {
d += (1 << i);
p = high[p][i];
}
}
for (int i = 17; i >= 0; i--) {
if (low[p][i] && a[low[p][i]] <= a[q]) {
d += (1 << i);
p = low[p][i];
}
}
if (p != q) return -1;
return d;
}
int minimum_jumps(int a, int b, int c, int d) {
a += 1; b += 1; c += 1; d += 1;
int koji = Query(c, d).first;
int l = a, r = b, gde = 0;
while (l <= r) {
int mid = l + r >> 1;
if (Query(mid, b).first < koji) {
gde = mid;
r = mid - 1;
}
else l = mid + 1;
}
if (gde == 0) return -1;
gde = Query(gde, b).second;
l = c, r = d;
int ret = -1;
while (l <= r) {
int mid = l + r >> 1;
int x = Dist(gde, Query(c, mid).second);
if (x != -1) {
ret = x;
r = mid - 1;
}
else l = mid + 1;
}
return ret;
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:85:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp:97:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
97 | 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... |