이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <vector>
using namespace std;
#include <bits/stdc++.h>
const int MAXN = 2e5 + 5;
int DP_Min[19][MAXN], DP_Max[19][MAXN];
int seg[MAXN * 4];
vector<int> INPUT;
vector<int> omk(MAXN);
int okN;
void Build(int l, int r, int pos) {
if(l == r) {
seg[pos] = INPUT[l - 1];
return;
}
int mid = (l + r) >> 1;
Build(l, mid, pos * 2);
Build(mid + 1, r, pos * 2 + 1);
seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);
}
pair<int, int> Query(int l, int r, int pos, int ql, int qr, int qval) {
if(ql > r || l > qr) {
//cout << "l = " << l << ", r = " << r << ", there {-1, -1}\n";
return {0, 0};
}
if(l == r) {
if(seg[pos] >= qval) {
//cout << "l = " << l << ", r = " << r << ", here {-1, -1}\n";
return {-1, -1};
}
//cout << "l = " << l << ", r = " << r << ", {" << seg[pos] << ", " << seg[pos] << "}\n";
return {seg[pos], seg[pos]};
}
if(ql <= l && r <= qr && seg[pos] < qval) {
return {seg[pos], seg[pos]};
}
int mid = (l + r) >> 1;
pair<int, int> x = Query(mid + 1, r, pos * 2 + 1, ql, qr, qval);
if(x.second == -1) {
//cout << "l = " << l << ", r = " << r << ", aftermid {" << x.first << ", -1}\n";
return x;
}
pair<int, int> y = Query(l, mid, pos * 2, ql, qr, qval);
if(y.second == -1) {
//cout << "l = " << l << ", r = " << r << ", {" << max(y.first, x.first) << ", " << y.second << "}\n";
return {max(y.first, x.first), -1};
}
//cout << "l = " << l << ", r = " << r << ", {" << max(y.first, x.first) << ", " << max(y.second, x.second) << "}\n";
return {max(y.first, x.first), max(y.second, x.second)};
}
void init(int N, vector<int> H) {
okN = N;
vector<int> L(N), R(N);
stack<int> q;
for(int i = 0; i < N; i++) {
omk[H[i]] = i;
INPUT.push_back(H[i]);
while(!q.empty() && H[q.top()] <= H[i]) {
q.pop();
}
if(q.empty()) {
L[i] = -1;
}
else {
L[i] = q.top();
}
q.push(i);
}
Build(1, N, 1);
while(!q.empty()) {
q.pop();
}
for(int i = N - 1; i >= 0; i--) {
while(!q.empty() && H[q.top()] <= H[i]) {
q.pop();
}
if(q.empty()) {
R[i] = N;
}
else {
R[i] = q.top();
}
q.push(i);
}
for(int i = 0; i < N; i++) {
if(L[i] == -1 && R[i] == N) {
DP_Min[0][i] = -1;
DP_Max[0][i] = -1;
}
else if(L[i] == -1 || R[i] == N) {
if(L[i] == -1) {
DP_Min[0][i] = R[i];
DP_Max[0][i] = R[i];
}
else {
DP_Min[0][i] = L[i];
DP_Max[0][i] = L[i];
}
}
else {
if(H[L[i]] < H[R[i]]) {
DP_Min[0][i] = L[i];
DP_Max[0][i] = R[i];
}
else {
DP_Min[0][i] = R[i];
DP_Max[0][i] = L[i];
}
//DP_Min[0][i] = min(L[i], R[i]);
//DP_Max[0][i] = max(L[i], R[i]);
}
}
for(int i = 1; i < 19; i++) {
for(int j = 0; j < N; j++) {
if(DP_Min[i - 1][j] == -1) {
DP_Min[i][j] = -1;
}
else {
DP_Min[i][j] = DP_Min[i - 1][DP_Min[i - 1][j]];
}
if(DP_Max[i - 1][j] == -1) {
DP_Max[i][j] = -1;
}
else {
DP_Max[i][j] = DP_Max[i - 1][DP_Max[i - 1][j]];
}
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
A++;
B++;
//cout << "A = " << A << ", B = " << B << ", INPUT[C] = " << INPUT[C] << "\n";
int pos = Query(1, okN, 1, A, B, INPUT[C]).first;
A = omk[pos];
B = omk[pos];
//cout << "A = " << A << ", B = " << B << "\n";
int ans = 0, cur = B;
for(int i = 18; i >= 0; i--) {
if(DP_Max[i][cur] != -1 && INPUT[DP_Max[i][cur]] <= INPUT[C]) {
//cout << "prev cur = " << cur << "\n";
ans += (1 << i);
cur = DP_Max[i][cur];
//cout << "new cur = " << cur << ", with added answer: " << (1 << i) << "\n";
}
}
for(int i = 18; i >= 0; i--) {
if(DP_Min[i][cur] != -1 && INPUT[DP_Min[i][cur]] <= INPUT[C]) {
ans += (1 << i);
cur = DP_Min[i][cur];
}
}
if(cur != C) return -1;
return ans;
}
# | 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... |