이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define task ""
const int INF = 1e9 + 7;
const int MAX = 200020;
stack<int> st;
int nTree, nQuery, H[MAX];
int rg[18][MAX], hg[18][MAX], dp[18][MAX];
#define combine(x, y) ((x) == -1 ? (y) : (y) == -1 ? (x) : H[(x)] >= H[(y)] ? (x) : (y))
int getMax(int l, int r) {
int k = 31 - __builtin_clz(r - l + 1);
return combine(dp[k][l], dp[k][r - (1 << k) + 1]);
}
int minimum_jumps(int A, int B, int C, int D) {
int q = getMax(B, C - 1), l = A - 1, r = B;
if (H[q] > H[getMax(C, D)]) return -1;
while (r - l > 1) {
int mid = l + r >> 1;
if (H[getMax(mid, B)] < H[q]) r = mid; else l = mid;
}
int st = getMax(r, B);
if (C <= rg[0][st] && rg[0][st] <= D) return 1;
int res = 0;
for (int j = 17; j >= 0; --j) if (hg[j][st] != -1 && rg[0][hg[j][st]] != -1) {
if (rg[0][hg[j][st]] < C) {
st = hg[j][st];
res += 1 << j;
}
}
int nx = rg[0][st];
if (rg[0][nx] <= D) {
st = nx; ++res;
if (C <= rg[0][nx] && rg[0][nx] <= D) return res + 1;
}
for (int j = 17; j >= 0; --j) if (rg[j][st] != -1) {
if (rg[j][st] < C) {
st = rg[j][st];
res += 1 << j;
}
}
return res + 1;
}
void init(int N, vector<int> _H) {
nTree = N;
for (int i = 0; i < nTree; ++i) {
H[i] = _H[i];
}
st = stack<int> {};
for (int i = 0; i <= nTree - 1; ++i) {
while (st.size() && H[st.top()] < H[i]) st.pop();
hg[0][i] = st.size() ? st.top() : -1; st.push(i);
}
st = stack<int> {};
for (int i = nTree - 1; i >= 0; --i) {
while (st.size() && H[st.top()] < H[i]) st.pop();
rg[0][i] = st.size() ? st.top() : -1; st.push(i);
}
for (int i = 0; i < nTree; ++i) {
dp[0][i] = i;
hg[0][i] = combine(hg[0][i], rg[0][i]);
}
for (int j = 1; j <= 17; ++j) {
for (int i = 0; i < nTree; ++i) {
if (hg[j - 1][i] != -1) hg[j][i] = hg[j - 1][hg[j - 1][i]]; else hg[j][i] = -1;
if (rg[j - 1][i] != -1) rg[j][i] = rg[j - 1][rg[j - 1][i]]; else rg[j][i] = -1;
}
}
for (int j = 1; (1 << j) <= nTree; ++j) {
for (int i = 0; i + (1 << j) <= nTree; ++i)
dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
}
}
//int main() {
// ios_base::sync_with_stdio(false); cin.tie(nullptr);
//
// if (fopen(task".inp", "r")) {
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
// }
//
// cin >> nTree >> nQuery;
// for (int i = 0; i < nTree; ++i) {
// cin >> H[i]; dp[0][i] = i;
// }
//
// st = stack<int> {};
// for (int i = 0; i <= nTree - 1; ++i) {
// while (st.size() && H[st.top()] < H[i]) st.pop();
// hg[0][i] = st.size() ? st.top() : -1; st.push(i);
// }
// st = stack<int> {};
// for (int i = nTree - 1; i >= 0; --i) {
// while (st.size() && H[st.top()] < H[i]) st.pop();
// rg[0][i] = st.size() ? st.top() : -1; st.push(i);
// }
// for (int i = 0; i < nTree; ++i) {
// dp[0][i] = i;
// hg[0][i] = combine(hg[0][i], rg[0][i]);
// }
//
// for (int j = 1; j <= 17; ++j) {
// for (int i = 0; i < nTree; ++i) {
// if (hg[j - 1][i] != -1) hg[j][i] = hg[j - 1][hg[j - 1][i]]; else hg[j][i] = -1;
// if (rg[j - 1][i] != -1) rg[j][i] = rg[j - 1][rg[j - 1][i]]; else rg[j][i] = -1;
// }
// }
// for (int j = 1; (1 << j) <= nTree; ++j) {
// for (int i = 0; i + (1 << j) <= nTree; ++i)
// dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
// }
// while (nQuery--) {
// int A, B, C, D; cin >> A >> B >> C >> D;
// cout << minimum_jumps(A, B, C, D) << '\n';
// }
// return 0;
//}
/*
7 3
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2
*/
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:21:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:75:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
75 | dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
| ~~^~~
jumps.cpp:10:37: note: in definition of macro 'combine'
10 | #define combine(x, y) ((x) == -1 ? (y) : (y) == -1 ? (x) : H[(x)] >= H[(y)] ? (x) : (y))
| ^
jumps.cpp:75:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
75 | dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
| ~~^~~
jumps.cpp:10:43: note: in definition of macro 'combine'
10 | #define combine(x, y) ((x) == -1 ? (y) : (y) == -1 ? (x) : H[(x)] >= H[(y)] ? (x) : (y))
| ^
jumps.cpp:75:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
75 | dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
| ~~^~~
jumps.cpp:10:73: note: in definition of macro 'combine'
10 | #define combine(x, y) ((x) == -1 ? (y) : (y) == -1 ? (x) : H[(x)] >= H[(y)] ? (x) : (y))
| ^
jumps.cpp:75:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
75 | dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
| ~~^~~
jumps.cpp:10:86: note: in definition of macro 'combine'
10 | #define combine(x, y) ((x) == -1 ? (y) : (y) == -1 ? (x) : H[(x)] >= H[(y)] ? (x) : (y))
| ^
# | 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... |