제출 #1021043

#제출 시각아이디문제언어결과실행 시간메모리
102104312345678밀림 점프 (APIO21_jumps)C++17
100 / 100
950 ms45116 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; const int nx=2e5+5, kx=18; int l[kx][nx], r[kx][nx], g[kx][nx]; void init(int N, std::vector<int> H) { H.push_back(INT_MIN); stack<int> s; for (int i=0; i<=N; i++) l[0][i]=r[0][i]=g[0][i]=N; for (int i=0; i<N; i++) { while (!s.empty()&&H[s.top()]<H[i]) s.pop(); if (!s.empty()) l[0][i]=s.top(); s.push(i); } while (!s.empty()) s.pop(); for (int i=N-1; i>=0; i--) { while (!s.empty()&&H[s.top()]<H[i]) s.pop(); if (!s.empty()) r[0][i]=s.top(); s.push(i); } for (int i=0; i<N; i++) g[0][i]=(H[l[0][i]]>H[r[0][i]])?l[0][i]:r[0][i]; for (int i=1; i<kx; i++) for (int j=0; j<=N; j++) l[i][j]=l[i-1][l[i-1][j]], r[i][j]=r[i-1][r[i-1][j]], g[i][j]=g[i-1][g[i-1][j]]; } int minimum_jumps(int A, int B, int C, int D) { int opt=B, ans=0; for (int i=kx-1; i>=0; i--) if (l[i][opt]>=A&&r[0][l[i][opt]]<=D) opt=l[i][opt]; if (C<=r[0][opt]&&r[0][opt]<=D) return 1; for (int i=kx-1; i>=0; i--) if (r[0][g[i][opt]]<C) ans+=(1<<i), opt=g[i][opt]; if (r[0][g[0][opt]]<=D) return ans+2; for (int i=kx-1; i>=0; i--) if (r[i][opt]<C) ans+=(1<<i), opt=r[i][opt]; if (r[0][opt]<C||r[0][opt]>D) return -1; else return ans+1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...