Submission #569942

#TimeUsernameProblemLanguageResultExecution timeMemory
569942toastifishiRainforest Jumps (APIO21_jumps)C++14
100 / 100
1084 ms50592 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; const int MAXLG = 20; int n, heights[MAXN]; int L[MAXN][MAXLG], R[MAXN][MAXLG], high[MAXN][MAXLG]; void init(int N, vector<int> H) { n = N; heights[0] = heights[n + 1] = 1e9; for(int i = 0; i < n; i++) heights[i + 1] = H[i]; vector <int> ii; ii.push_back(0); for(int i = 1; i <= n; i++) { while(heights[ii.back()] < heights[i]) ii.pop_back(); L[i][0] = ii.back(); ii.push_back(i); } ii.clear(); ii.push_back(n + 1); for(int i = n; i >= 1; i--) { while(heights[ii.back()] < heights[i]) ii.pop_back(); R[i][0] = ii.back(); ii.push_back(i); high[i][0] = ((heights[L[i][0]] > heights[R[i][0]] ? L[i][0] : R[i][0])); } R[n + 1][0] = n + 1; for(int p = 1; p <= 19; p++) { R[n + 1][p] = n + 1; for(int i = 1; i <= n; i++) { L[i][p] = L[L[i][p - 1]][p - 1]; R[i][p] = R[R[i][p - 1]][p - 1]; high[i][p] = high[high[i][p - 1]][p - 1]; } } } int minimum_jumps(int A, int B, int C, int D) { A++, B++, C++, D++; int furthest = B; for(int i = 19; i >= 0; i--) { if(R[furthest][i] < C) furthest = R[furthest][i]; } if(furthest == B) { if(R[B][0] <= D) return 1; } int st = B; for(int i = 19; i >= 0; i--) { if(L[st][i] >= A && heights[L[st][i]] < heights[furthest]) st = L[st][i]; } if(L[st][0] >= A && R[L[st][0]][0] <= D) return 1; int cur = st, ans = 0; for(int i = 19; i >= 0; i--) { if(heights[high[cur][i]] <= heights[furthest]) { cur = high[cur][i]; ans += 1 << i; } } if(R[cur][0] >= C && R[cur][0] <= D) return ans + 1; if(L[cur][0] != 0 && R[L[cur][0]][0] <= D) return ans + 2; for(int i = 19; i >= 0; i--) { if(R[cur][i] < C) { cur = R[cur][i]; ans += 1 << i; } } if(R[cur][0] <= D) return ans + 1; else 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...