Submission #569370

#TimeUsernameProblemLanguageResultExecution timeMemory
569370TurkhuuRainforest Jumps (APIO21_jumps)C++17
23 / 100
1338 ms53692 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; const int L = 18; int n; vector<int> a; vector<vector<int>> lo, hi; void init(int N, vector<int> H){ n = N, a = H; lo.assign(n, vector(L, -1)); hi.assign(n, vector(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 || l >= r)){ lo[i][0] = r; hi[i][0] = l; } else{ lo[i][0] = l; hi[i][0] = r; } s.insert(idx[i]); } for(int j = 1; j < L; j++){ for(int i = 0; i < n; i++){ if(lo[i][j - 1] != -1){ lo[i][j] = lo[lo[i][j - 1]][j - 1]; } if(hi[i][j - 1] != -1){ hi[i][j] = hi[hi[i][j - 1]][j - 1]; } } } } int minimum_jumps(int A, int B, int C, int D){ int H = a[A], G = a[C], c = 0; for(int i = L - 1; i >= 0; i--){ if(hi[H][i] != -1 && hi[H][i] <= G){ H = hi[H][i]; c += 1 << i; } } for(int i = L - 1; i >= 0; i--){ if(lo[H][i] != -1 && lo[H][i] <= G){ H = lo[H][i]; c += 1 << i; } } if(H != G){ c = -1; } return c; }
#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...