제출 #413774

#제출 시각아이디문제언어결과실행 시간메모리
413774flashmt밀림 점프 (APIO21_jumps)C++17
0 / 100
431 ms55900 KiB
#include <bits/stdc++.h> using namespace std; const int N = 200200; const int LG = 18; int n, l[N], r[N], toL[N][LG], toR[N][LG], maxH[N][LG]; vector<int> h; void initEdges() { vector<pair<int, int>> sortedH; for (int i = 0; i < n; i++) sortedH.push_back({h[i], i}); sort(sortedH.begin(), sortedH.end(), greater<pair<int, int>>()); set<int> s; s.insert(-1); s.insert(n); for (auto [_, i] : sortedH) { auto it = s.lower_bound(i); r[i] = *it; l[i] = *(--it); s.insert(i); } } void constructTable() { for (int i = 0; i < n; i++) { toL[i][0] = l[i]; toR[i][0] = r[i]; maxH[i][0] = h[i]; for (int j = 1; j < LG; j++) toL[i][j] = toR[i][j] = n; } for (int j = 0; j + 1 < LG; j++) for (int i = 0; i < n; i++) { if (toL[i][j] < n) toL[i][j + 1] = toL[toL[i][j]][j]; if (toR[i][j] < n) toR[i][j + 1] = toR[toR[i][j]][j]; if (i + (1 << j) < n) maxH[i][j + 1] = max(maxH[i][j], maxH[i + (1 << j)][j]); } } void init(int N, vector<int> H) { n = N; h = H; h[n] = 0; initEdges(); constructTable(); } int getMaxH(int l, int r) { int bit = 31 - __builtin_clz(r - l + 1); return max(maxH[l][bit], maxH[r - (1 << bit) + 1][bit]); } int between(int x, int y, int z) { return y <= x && x <= z; } int minimum_jumps(int A, int B, int C, int D) { int s = B, maxDestH = getMaxH(C, D); for (int i = LG - 1; i >= 0; i--) { int x = toL[s][i]; if (x >= A && x < n && h[x] < maxDestH) s = x; } if (between(l[s], C, D) || between(r[s], C, D)) return 1; int dist = 0; if (l[s] < n && h[l[s]] > h[r[s]] && h[l[s]] <= maxDestH) { s = l[s]; dist = 1; } for (int i = LG - 1; i >= 0; i--) { int x = toR[s][i]; if (x <= D && h[x] <= maxDestH) { s = x; dist += 1 << i; if (s >= C) return dist; } } return -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...