이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
//#pragma GCC optimize ("O3")
//#pragma GCC target("tune=native")
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
const int nax = 200 * 1000 + 10, INF = 1e9 + 10;
int n;
int h[nax];
int lft[nax], rght[nax];
int par[nax][19];
int par2[nax][19];
void init(int N, vi H) {
n = N;
for(int i = 0; i < n; ++i) {
h[i + 1] = H[i];
}
h[0] = INF + 1, h[n + 1] = INF;
vi S = {0};
for(int i = 1; i <= n; ++i) {
while(h[S.back()] < h[i]) S.pop_back();
lft[i] = S.back();
S.PB(i);
}
S = {n + 1};
for(int i = n; i >= 1; --i) {
while(h[S.back()] < h[i]) S.pop_back();
rght[i] = S.back();
S.PB(i);
}
for(int i = 1; i <= n; ++i) {
if(h[lft[i]] > h[rght[i]]) {
par[i][0] = lft[i];
par2[i][0] = rght[i];
} else {
par[i][0] = rght[i];
par2[i][0] = lft[i];
}
}
par[n + 1][0] = 0;
par2[n + 1][0] = 0;
for(int j = 1; j < 19; ++j) {
for(int i = 1; i <= n + 1; ++i) {
par[i][j] = par[par[i][j - 1]][j - 1];
par2[i][j] = par2[par2[i][j - 1]][j - 1];
}
}
}
int minimum_jumps(int a, int b, int c, int d) {
a++, b++, c++, d++;
int H = -1;
for(int i = c; i <= d; ++i) {
H = max(H, h[i]);
}
int p = b;
for(int i = b - 1; i >= a; --i) {
if(h[i] > H) break;
if(h[p] < h[i]) p = i;
}
int ans = 0;
for(int i = 18; i >= 0; --i) {
if(h[par[p][i]] <= H) {
p = par[p][i];
ans += (1 << i);
}
}
for(int i = 18; i >= 0; --i) {
if(h[par2[p][i]] <= H) {
p = par2[p][i];
ans += (1 << i);
}
}
if(p < c || p > d) return -1;
return ans;
}
//int main() {
//ios_base::sync_with_stdio(0);
//cin.tie(0);
//init(7, {3, 2, 1, 6, 4, 5, 7});
//cout << minimum_jumps(4, 4, 6, 6) << "\n";
//cout << minimum_jumps(1, 3, 5, 6) << "\n";
//cout << minimum_jumps(0, 1, 2, 2) << "\n";
//}
# | 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... |