Submission #417351

#TimeUsernameProblemLanguageResultExecution timeMemory
417351CSQ31Rainforest Jumps (APIO21_jumps)C++14
0 / 100
197 ms40976 KiB
#include "jumps.h" #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) typedef long long int ll; typedef pair<int,int> pii; typedef vector<vector<int>> vii; const int MAXN = 2e5+5; int l[20][MAXN],r[20][MAXN],high[20][MAXN]; vector<int>H; void init(int n, vector<int>h){ H = h; H.insert(H.begin(),0); h.pb(1e9); stack<pii>stk; for(int i=0;i<=n+1;i++){ while(!stk.empty() && stk.top().fi < h[i])stk.pop(); if(!stk.empty())l[0][i] = stk.top().se; else l[0][i] = i; stk.push({h[i],i}); } while(!stk.empty())stk.pop(); for(int i=n+1;i>=0;i--){ while(!stk.empty() && stk.top().fi < h[i])stk.pop(); if(!stk.empty())r[0][i] = stk.top().se; else r[0][i] = i; stk.push({h[i],i}); } for(int i=0;i<=n+1;i++){ high[0][i] = (H[r[0][i]] > H[l[0][i]])?r[0][i]:l[0][i]; } for(int j=1;j<20;j++){ for(int i=0;i<=n+1;i++){ l[j][i] = l[j-1][l[j-1][i]]; r[j][i] = r[j-1][r[j-1][i]]; high[j][i] = high[j-1][high[j-1][i]]; } } } // int minimum_jumps(int a,int b,int c,int d){ a++; b++; c++; d++; if(c == b+1){ if(r[0][b] > d)return -1; return 1; } int mid = b+1; for(int j=19;j>=0;j--){ if(r[j][mid] < c){ mid = r[j][mid]; } } if(H[b] > H[mid])return r[0][b]<=d?1:-1; //anything higher this mid point can be possibly "unreachable" //otherwise we are safe to use high edges int cur = b; for(int j=19;j>=0;j--){ if(l[j][cur] >= a && H[l[j][cur]] < H[mid])cur = l[j][cur]; } //we want to start at cur int ans = 0; if(l[0][cur] >= a){ if(r[0][l[0][cur]] <= d)return 1; }else{ //if l[0][cur] reachable we take it,else we can never go left again for(int j=19;j>=0;j--){ if(H[high[j][cur]] <= H[mid]){ ans+=(1<<j); cur = high[j][cur]; } } if(cur == mid){ if(r[0][mid] <= d)return ans+1; else return -1; }else{ //since we cant jump right without >= h[mid] //we check if jumping left is reachable if(l[0][cur] && r[0][l[0][cur]] <= d)return ans+2; } } //we cant jump to the left anymore : ( for(int j=19;j>=0;j--){ if(r[j][cur] < c){ cur = r[j][cur]; ans+=(1<<j); } } if(r[0][cur] <= d && r[0][cur] >= c)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...