제출 #838296

#제출 시각아이디문제언어결과실행 시간메모리
838296siewjh밀림 점프 (APIO21_jumps)C++17
100 / 100
1002 ms51376 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; const int MAXN = 200005; int nums, height[MAXN], lf[MAXN][20], rt[MAXN][20], hi[MAXN][20]; int query(int l, int r, int targ){ int ans = r; if (height[ans] > targ) return -1; for (int k = 19; k >= 0; k--) if (lf[ans][k] >= l && height[lf[ans][k]] < targ) ans = lf[ans][k]; return ans; } void init(int N, vector<int> H) { nums = N; for (int i = 1; i <= nums; i++) height[i] = H[i - 1]; height[0] = height[nums + 1] = nums + 1; stack<pair<int, int>> s; s.push({nums + 1, 0}); for (int i = 1; i <= nums; i++){ while (height[i] > s.top().first) s.pop(); lf[i][0] = s.top().second; s.push({height[i], i}); } while (s.size()) s.pop(); s.push({nums + 1, nums + 1}); for (int i = nums; i >= 1; i--){ while (height[i] > s.top().first) s.pop(); rt[i][0] = s.top().second; s.push({height[i], i}); } for (int i = 1; i <= nums; i++) hi[i][0] = (height[lf[i][0]] > height[rt[i][0]] ? lf[i][0] : rt[i][0]); for (int k = 1; k < 20; k++) for (int i = 1; i <= nums; i++){ if (lf[i][k - 1] == 0) lf[i][k] = 0; else lf[i][k] = lf[lf[i][k - 1]][k - 1]; if (rt[i][k - 1] == nums + 1) rt[i][k] = nums + 1; else rt[i][k] = rt[rt[i][k - 1]][k - 1]; if (hi[i][k - 1] == 0 || hi[i][k - 1] == nums + 1) hi[i][k] = hi[i][k - 1]; else hi[i][k] = hi[hi[i][k - 1]][k - 1]; } } int minimum_jumps(int A, int B, int C, int D) { A++; B++; C++; D++; int maxcd = height[query(C, D, MAXN)], curr = query(A, B, maxcd); if (curr == -1) return -1; int ans = 0; for (int k = 19; k >= 0; k--) if (height[hi[curr][k]] != nums + 1 && rt[hi[curr][k]][0] < C){ curr = hi[curr][k]; ans += (1 << k); } if (rt[curr][0] < C && height[hi[curr][0]] != nums + 1 && rt[hi[curr][0]][0] <= D){ ans++; curr = hi[curr][0]; } for (int k = 19; k >= 0; k--) if (rt[curr][k] < C){ curr = rt[curr][k]; ans += (1 << k); } if (rt[curr][0] > 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...