이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int lft[400007],rgt[400007],jmpl[400007][19],jmpg[400007][19];
bool czy1=1;
bool odw[400007];
int n;
vector<int>v;
void init(int N,vector<int>V){
n=N;
v=V;
while(v.size()<400007)
v.push_back(400006);
stack<int>s;
lft[400006]=400006;
rgt[400006]=400006;
//s.push(n);
for(int i=0;i<n;i++){
lft[i]=400006;
rgt[i]=400006;
}
//return;
for(int i=0;i<n;i++){
if(s.empty() || v[s.top()]>v[i])
s.push(i);
else{
rgt[s.top()]=i;
s.pop();
i--;
}
}
while(s.size()>0) s.pop();
for(int i=n-1;i>=0;i--){
if(s.empty() || v[s.top()]>v[i])
s.push(i);
else{
czy1=0;
lft[s.top()]=i;
s.pop();
i++;
}
}
for(int i=0;i<n;i++){
int pm=lft[i];
if(v[lft[i]]>v[rgt[i]]){
lft[i]=rgt[i];
rgt[i]=pm;
}
jmpg[i][0]=rgt[i];
jmpl[i][0]=lft[i];
}
jmpl[400006][0]=400006;
jmpg[400006][0]=400006;
for(int i=1;i<19;i++){
for(int j=0;j<n;j++){
jmpl[j][i]=jmpl[jmpl[j][i-1]][i-1];
jmpg[j][i]=jmpg[jmpg[j][i-1]][i-1];
// cout<<jmpg[j][i]<<" ";
}
jmpl[400006][i]=400006;
jmpg[400006][i]=400006;
// cout<<"\n";
}
//for(int i=0;i<n;i++)
//cout<<lft[i]<<" "<<rgt[i]<<"\n";
}
int minimum_jumps(int a,int b,int c,int d){
if(czy1)
return c-b;
int odp=0;
if(a==b && c==d){
int akindex=-1;
//int odp=0;
while(v[jmpg[a][akindex+1]]<=v[c])
akindex++;
if(akindex>=0){
odp+=1<<akindex;
//cout<<akindex<<" a ";
//return 0;
//cout<<a<<" ";
a=jmpg[a][akindex];
//cout<<a<<" ";
akindex--;}
while(akindex>=0){
if(v[jmpg[a][akindex]]>v[c]){
akindex--;
continue;
}
odp+=1<<akindex;
a=jmpg[a][akindex];
akindex--;
//cout<<a<<" ";
}
akindex=-1;
while(v[jmpl[a][akindex+1]]<=v[c])
akindex++;
if(akindex>=0){
odp+=1<<akindex;
//cout<<akindex<<" a ";
//return 0;
a=jmpl[a][akindex];
akindex--;}
while(akindex>=0){
if(v[jmpl[a][akindex]]>v[c]){
akindex--;
continue;
}
odp+=1<<akindex;
a=jmpl[a][akindex];
akindex--;
//cout<<a<<" ";
}
if(a!=c)
odp=-1;
return odp;
a=b;
}
odw[400006]=1;
int ans[n];
for(int i=0;i<n;i++){ odw[i]=0; ans[i]=400007;}
for(int i=a;i<=b;i++) odw[i]=1;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
for(int i=a;i<=b;i++){
pq.push({0,i});
}
while(!pq.empty()){
pair<int,int>pom=pq.top();
pq.pop();
ans[pom.second]=pom.first;
if(!odw[lft[pom.second]]){
odw[lft[pom.second]]=1;
pq.push({pom.first+1,lft[pom.second]});
}
if(!odw[rgt[pom.second]]){
odw[rgt[pom.second]]=1;
pq.push({pom.first+1,rgt[pom.second]});
}
//return 0;
}
int mn=400007;
for(int i=c;i<=d;i++){
mn=min(mn,ans[i]);
}
if(mn==400007)
mn=-1;
//cout<<ans[11];
//cout<<mn;
return mn;
}
# | 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... |