Submission #478161

#TimeUsernameProblemLanguageResultExecution timeMemory
478161Yazan_AlattarRainforest Jumps (APIO21_jumps)C++14
100 / 100
1491 ms50580 KiB
#include "jumps.h" #include <iostream> #include <fstream> #include <vector> #include <cstring> #include <algorithm> #include <set> #include <map> #include <queue> #include <list> #include <utility> #include <cmath> #include <numeric> using namespace std; typedef long long ll; #define F first #define S second #define pb push_back #define endl "\n" #define all(x) x.begin(), x.end() const int M = 200007; const ll inf = 1e9; const ll mod = 1e9 + 7; const double pi = acos(-1); const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; int a[M], l[M][20], r[M][20], hi[M][20]; void init(int n, vector <int> H) { for(int i = 1; i <= n; ++i) a[i] = H[i - 1]; vector <int> v; a[0] = inf; a[n + 1] = inf; for(int i = 0; i <= n + 1; ++i){ while(v.size() && a[v.back()] <= a[i]) v.pop_back(); if(v.size()) l[i][0] = v.back(); else l[i][0] = i; v.pb(i); } v.clear(); for(int i = n + 1; i >= 0; --i){ while(v.size() && a[v.back()] < a[i]) v.pop_back(); if(v.size()) r[i][0] = v.back(); else r[i][0] = i; v.pb(i); } for(int i = 0; i <= n + 1; ++i) hi[i][0] = (a[r[i][0]] > a[l[i][0]] ? r[i][0] : l[i][0]); for(int j = 1; j < 20; ++j){ for(int i = 0; i <= n + 1; ++i){ l[i][j] = l[l[i][j - 1]][j - 1]; r[i][j] = r[r[i][j - 1]][j - 1]; hi[i][j] = hi[hi[i][j - 1]][j - 1]; } } return; } int get(int A, int B) { for(int i = 19; i >= 0; --i) if(r[A][i] <= B) A = r[A][i]; return A; } int minimum_jumps(int A, int B, int C, int D) { ++A; ++B; ++C; ++D; if(B + 1 == C) return (r[B][0] <= D ? 1 : -1); int mid = get(B + 1, C - 1); if(a[B] > a[mid]) return (r[B][0] <= D ? 1 : -1); int p = B; for(int i = 19; i >= 0; --i) if(l[p][i] >= A && a[l[p][i]] < a[mid]) p = l[p][i]; int ret = 0; if(l[p][0] >= A){ if(r[l[p][0]][0] <= D) return 1; } else{ for(int i = 19; i >= 0; --i){ if(a[hi[p][i]] <= a[mid]){ ret += (1 << i); p = hi[p][i]; } } if(p == mid) return (r[p][0] <= D ? ret + 1 : -1); if(l[p][0] && r[l[p][0]][0] <= D) return ret + 2; } for(int i = 19; i >= 0; --i){ if(r[p][i] < C){ ret += (1 << i); p = r[p][i]; } } return (r[p][0] >= C && r[p][0] <= D ? ret + 1 : -1); } // Don't forget special cases. (n = 1?) // Look for the constraints. (Runtime array? overflow?)
#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...