이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "jumps.h"
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int pw[N];
int rg[N][20];
int lf[N][20];
int mx[N][20];
int path[N][20];
vector<int> _h;
void build_sparce (vector<int> h) {
for (int i = 2; i <= h.size(); ++i) pw[i] = pw[i >> 1] + 1;
for (int i = 0; i < h.size(); ++i) mx[i][0] = h[i];
for (int i = 1; i < 20; ++i)
for (int j = 0; j <= (int)h.size() - (1 << i); ++j)
mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]);
}
int get_max (int l, int r) {
if (l > r) swap(l, r);
int x = pw[r - l + 1];
return max(mx[l][x], mx[r - (1 << x) + 1][x]);
}
void init (int n, vector<int> h) {
_h = h;
build_sparce(h);
for (int i = 0; i < n; ++i) {
int l = i, r = n;
lf[i][0] = rg[i][0] = i;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (get_max(i, mid) == h[i])
l = mid;
else
r = mid;
}
if (r != n)
rg[i][0] = r;
l = -1, r = i;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (get_max(mid, i) == h[i])
r = mid;
else
l = mid;
}
if (l != -1)
lf[i][0] = l;
if (h[lf[i][0]] > h[rg[i][0]])
path[i][0] = lf[i][0];
else
path[i][0] = rg[i][0];
}
for (int i = 1; i < 20; ++i)
for (int j = 0; j < n; ++j) {
lf[j][i] = lf[lf[j][i - 1]][i - 1];
rg[j][i] = rg[rg[j][i - 1]][i - 1];
path[j][i] = path[path[j][i - 1]][i - 1];
}
}
int minimum_jumps(int a, int b, int c, int d) {
int vall = get_max(c, d);
int vall1 = get_max(b, c - 1);
for (int i = 19; i >= 0; --i)
if (a <= lf[b][i] && _h[lf[b][i]] <= vall)
b = lf[b][i];
int kol = 0;
for (int i = 19; i >= 0; --i)
if (_h[path[b][i]] <= vall1) {
b = path[b][i];
kol += (1 << i);
}
if (c <= rg[b][0] && rg[b][0] <= d)
return kol + 1;
if (_h[path[b][0]] < vall)
b = path[b][0], ++kol;
for (int i = 19; i >= 0; --i)
if (rg[b][i] < c) {
b = rg[b][i];
kol += (1 << i);
}
if (c <= rg[b][0] && rg[b][0] <= d)
return kol + 1;
return -1;
}
// signed main () {
// // ios_base::sync_with_stdio(0);
// // cin.tie(0), cout.tie(0);
// int n;
// cin >> n;
// vector<int> h(n);
// for (int i = 0; i < n; ++i) cin >> h[i];
// init(n, h);
// int q;
// cin >> q;
// while (q--) {
// int a, b, c, d;
// cin >> a >> b >> c >> d;
// cout << minimum_jumps(a, b, c, d) << '\n';
// }
// return 0;
// }
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'void build_sparce(std::vector<int>)':
jumps.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i = 2; i <= h.size(); ++i) pw[i] = pw[i >> 1] + 1;
| ~~^~~~~~~~~~~
jumps.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < h.size(); ++i) mx[i][0] = h[i];
| ~~^~~~~~~~~~
# | 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... |