이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
typedef pair<int,int> pa;
int n,h[200005],l[200005][20],r[200005][20],mx[200005][20];
stack<int> stk;
void init(int N, vector<int> H) {
n = N;
for(int i=0;i<N;i++) h[i+1] = H[i];
h[n+1] = -1;
h[0] = -1;
for(int i=1;i<=n;i++){
while(!stk.empty()){
if(h[stk.top()] < h[i]) stk.pop();
else break;
}
if(stk.empty()) l[i][0] = -1;
else l[i][0] = stk.top();
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i=n;i>=1;i--){
while(!stk.empty()){
if(h[stk.top()] < h[i]) stk.pop();
else break;
}
if(stk.empty()) r[i][0] = -1;
else r[i][0] = stk.top();
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i=1;i<=n;i++){
if(l[i][0] == -1) mx[i][0] = r[i][0];
else if(r[i][0] == -1) mx[i][0] = l[i][0];
else if(h[l[i][0]] > h[r[i][0]]) mx[i][0] = l[i][0];
else if(h[l[i][0]] < h[r[i][0]]) mx[i][0] = r[i][0];
}
for(int i=1;i<=18;i++){
for(int j=1;j<=n;j++){
if(l[j][i-1] == -1) l[j][i] = -1;
else l[j][i] = l[l[j][i-1]][i-1];
if(r[j][i-1] == -1) r[j][i] = -1;
else r[j][i] = r[r[j][i-1]][i-1];
if(mx[j][i-1] == -1) mx[j][i] = -1;
else mx[j][i] = mx[mx[j][i-1]][i-1];
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
A++, B++, C++, D++;
int pos = B,ans = 0;
for(int i=18;i>=0;i--){
if(l[pos][i] == -1) continue;
if(l[pos][i] >= A && r[l[pos][i]][0] <= D && r[l[pos][i]][0] != -1){
pos = l[pos][i];
}
}
//printf(">>%d\n",pos);
for(int i=18;i>=0;i--){
if(mx[pos][i] == -1) continue;
//printf("!!%d : %d %d\n",i,mx[pos][i],r[mx[pos][i]][0]);
if(r[mx[pos][i]][0] != -1 && r[mx[pos][i]][0] < C){
pos = mx[pos][i];
ans += (1<<i);
}
}
if(r[pos][0] != -1 && r[pos][0] < C && r[mx[pos][0]][0] <= D && r[mx[pos][0]][0] != -1){
ans++;
pos = mx[pos][0];
}
for(int i=18;i>=0;i--){
if(r[pos][i] == -1) continue;
if(r[pos][i] < C){
pos = r[pos][i];
ans += (1<<i);
}
}
if(r[pos][0] > D || r[pos][0] == -1) return -1;
else return ans+1;
}
/*
7 3
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2
*/
# | 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... |