제출 #1194883

#제출 시각아이디문제언어결과실행 시간메모리
1194883browntoadRainforest Jumps (APIO21_jumps)C++20
4 / 100
435 ms45760 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; #define ll long long // #define int ll #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define REP1(i, n) FOR(i, 1, n+1) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define pii pair<int, int> #define f first #define s second #define pb push_back #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int)((x).size()) namespace{ const ll maxn = 2e5+5; const ll mxpw = 18; int smaxR[maxn][mxpw]; int smaxL[maxn][mxpw]; int jump[maxn][mxpw]; // where i jump to vector<int> h(maxn); int n; } void init(int N, std::vector<int> H) { n = N; REP(i, n) h[i] = H[i]; REP(i, n){ smaxL[i][0] = H[i]; REP1(j, mxpw-1){ smaxL[i][j] = max(smaxL[i][j-1], smaxL[max(0, i-(1<<j-1))][j-1]); } } RREP(i, n){ smaxR[i][0] = H[i]; REP1(j, mxpw-1){ smaxR[i][j] = max(smaxR[i][j-1], smaxR[min(n-1, i+(1<<j-1))][j-1]); } } stack<int> st; RREP(i, n){ while(SZ(st) && h[st.top()] < h[i]) st.pop(); if (!SZ(st)){ jump[i][0] = i; // itself } else{ jump[i][0] = st.top(); } st.push(i); } RREP(i, n){ REP1(j, mxpw-1){ jump[i][j] = jump[jump[i][j-1]][j-1]; } } } int mxH(int l, int r){ if (l > r) return 0; int p = log2(r-l+1); return max(smaxR[l][p], smaxL[r][p]); } int minimum_jumps(int A, int B, int C, int D){ if (h[B] > mxH(C, D)) return -1; if (mxH(B+1, C-1) > mxH(C, D)) return -1; int mxCD = mxH(C, D); int l = A, r = B; while(l < r){ int mid = l+r>>1; if (mxH(mid, B) < mxCD) r = mid; else l = mid+1; } int tmp = mxH(l, B); r = B; while(l < r){ int mid = l+r+1>>1; if (mxH(mid, B) == tmp) l = mid; else r = mid-1; } int cnt = 1, cur = l; RREP(i, mxpw){ if (jump[cur][i] < C){ cnt += (1<<i); cur = jump[cur][i]; } } return cnt; }
#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...