이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
const int NN=2e5+100;
const int L=18;
const int INF=1e9;
int a[NN],lo[NN];
bool vis[NN];
int seg[4*NN];
int n;
vector<int> g[NN];
vector<int> vec;
int st[NN][L][2];
int stt[NN][L];
void build()
{
lo[1]=0;
for(int i=2;i<=n+1;i++){
lo[i]=lo[i/2]+1;
}
for (int j=1;j<L;j++){
for (int i=0;i+(1 << j)<=n;i++){
stt[i][j]=max(stt[i][j-1],stt[i+(1<<(j-1))][j-1]);
}
}
}
int get(int L,int R)
{
if(R<L)assert(0);
int j = lo[R - L + 1];
int ret=max(stt[L][j], stt[R - (1 << j) + 1][j]);
return ret;
}
void init(int N, std::vector<int> H) {
n=N;
for(int i=0;i<L;i++){
st[n+1][i][0]=n+1;
st[n+1][i][1]=n+1;
}
for(int i=0;i<N;i++){
a[i]=H[i];
stt[i][0]=a[i];
}
build();
for(int i=0;i<n;i++){
if(i!=n-1&&get(i,n-1)!=a[i]){
int ans,l=i+1,r=n-1;
while(l<=r){
int mid=(l+r)/2;
int v=get(i,mid);
if(v>a[i]){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
g[a[i]].push_back(a[ans]);
}
if(i!=0&&get(0,i)!=a[i]){
int ans,l=0,r=i-1;
while(l<=r){
int mid=(l+r)/2;
int v=get(mid,i);
if(v>a[i]){
ans=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
g[a[i]].push_back(a[ans]);
}
sort(g[a[i]].begin(),g[a[i]].end());
if(g[a[i]].size()>0){
st[a[i]][0][0]=g[a[i]][0];
}
else{
st[a[i]][0][0]=n+1;
}
if(g[a[i]].size()>1){
st[a[i]][0][1]=g[a[i]][1];
}
else{
st[a[i]][0][1]=n+1;
}
}
for(int j=1;j<L;j++){
for(int i=1;i<=n;i++){
int r=i;
r=st[r][j-1][0];
r=st[r][j-1][0];
st[i][j][0]=r;
r=i;
r=st[r][j-1][1];
r=st[r][j-1][1];
st[i][j][1]=r;
}
}
}
int dfs(int u,int tar)
{
int ret=0;
for(int i=1;i>=0;i--){
for(int j=L-1;j>=0;j--){
int r=st[u][j][i];
if(r<=tar){
ret+=(1<<j);
u=r;
}
}
}
if(u==tar)return ret;
else return INF;
}
int minimum_jumps(int A,int B,int C,int D){
int u=a[B];
int tar=a[C];
if(u>tar)return -1;
int l=A,r=B;
while(l<=r){
int mid=(l+r)/2;
if(get(mid,B)<tar){
r=mid-1;
u=get(mid,B);
}
else{
l=mid+1;
}
}
int ans=dfs(u,tar);
if(ans==INF)ans=-1;
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:51:17: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
51 | int ans,l=i+1,r=n-1;
| ^~~
# | 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... |