Submission #569200

#TimeUsernameProblemLanguageResultExecution timeMemory
569200TurkhuuRainforest Jumps (APIO21_jumps)C++17
0 / 100
4062 ms27368 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; const int L = 18; int n; vector<int> a, d; vector<vector<int>> anc; void init(int N, vector<int> H){ n = N, a = H; d.resize(n, -1); anc.assign(n, vector<int>(L, -1)); vector<int> idx(n); for(int i = 0; i < n; i++){ idx[--a[i]] = i; } set<int> s; for(int i = n - 1; i >= 0; i--){ auto j = s.lower_bound(idx[i]); int l = -1, r = -1; if(j != s.end()){ r = a[*j]; } if(j != s.begin()){ l = a[*prev(j)]; } if(l != -1 || r != -1){ if(l > r){ anc[i][0] = l; if(r != -1){ d[i] = r; } } else{ anc[i][0] = r; if(l != -1){ d[i] = l; } } } s.insert(idx[i]); } for(int j = 1; j < L; j++){ for(int i = 0; i < n; i++){ if(anc[i][j - 1] != -1){ anc[i][j] = anc[anc[i][j - 1]][j - 1]; } } } } int minimum_jumps(int A, int B, int C, int D){ int ans = -1; for(int S = A; S <= B; S++){ for(int E = C; E <= D; E++){ int H = a[S], G = a[E], c = 0; while(H != -1 && H < G){ for(int i = L - 1; i >= 0; i--){ if(anc[H][i] != -1 && anc[H][i] <= G){ H = anc[H][i]; c += 1 << i; break; } } if(H != -1 && H < G){ H = d[H]; c++; } } if(H == G){ if(ans == -1 || c < ans){ ans = c; } } } } return ans; }
#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...