Submission #542020

#TimeUsernameProblemLanguageResultExecution timeMemory
542020PherokungRainforest Jumps (APIO21_jumps)C++14
100 / 100
1153 ms50504 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define F first #define S second typedef pair<int,int> pa; int n,h[200005],l[200005][20],r[200005][20],mx[200005][20]; stack<int> stk; void init(int N, vector<int> H) { n = N; for(int i=0;i<N;i++) h[i+1] = H[i]; h[n+1] = -1; h[0] = -1; for(int i=1;i<=n;i++){ while(!stk.empty()){ if(h[stk.top()] < h[i]) stk.pop(); else break; } if(stk.empty()) l[i][0] = -1; else l[i][0] = stk.top(); stk.push(i); } while(!stk.empty()) stk.pop(); for(int i=n;i>=1;i--){ while(!stk.empty()){ if(h[stk.top()] < h[i]) stk.pop(); else break; } if(stk.empty()) r[i][0] = -1; else r[i][0] = stk.top(); stk.push(i); } while(!stk.empty()) stk.pop(); for(int i=1;i<=n;i++){ if(l[i][0] == -1) mx[i][0] = r[i][0]; else if(r[i][0] == -1) mx[i][0] = l[i][0]; else if(h[l[i][0]] > h[r[i][0]]) mx[i][0] = l[i][0]; else if(h[l[i][0]] < h[r[i][0]]) mx[i][0] = r[i][0]; } for(int i=1;i<=18;i++){ for(int j=1;j<=n;j++){ if(l[j][i-1] == -1) l[j][i] = -1; else l[j][i] = l[l[j][i-1]][i-1]; if(r[j][i-1] == -1) r[j][i] = -1; else r[j][i] = r[r[j][i-1]][i-1]; if(mx[j][i-1] == -1) mx[j][i] = -1; else mx[j][i] = mx[mx[j][i-1]][i-1]; } } } int minimum_jumps(int A, int B, int C, int D) { A++, B++, C++, D++; int pos = B,ans = 0; for(int i=18;i>=0;i--){ if(l[pos][i] == -1) continue; if(l[pos][i] >= A && r[l[pos][i]][0] <= D && r[l[pos][i]][0] != -1){ pos = l[pos][i]; } } //printf(">>%d\n",pos); for(int i=18;i>=0;i--){ if(mx[pos][i] == -1) continue; //printf("!!%d : %d %d\n",i,mx[pos][i],r[mx[pos][i]][0]); if(r[mx[pos][i]][0] != -1 && r[mx[pos][i]][0] < C){ pos = mx[pos][i]; ans += (1<<i); } } if(r[pos][0] != -1 && r[pos][0] < C && r[mx[pos][0]][0] <= D && r[mx[pos][0]][0] != -1){ ans++; pos = mx[pos][0]; } for(int i=18;i>=0;i--){ if(r[pos][i] == -1) continue; if(r[pos][i] < C){ pos = r[pos][i]; ans += (1<<i); } } if(r[pos][0] > D || r[pos][0] == -1) return -1; else return ans+1; } /* 7 3 3 2 1 6 4 5 7 4 4 6 6 1 3 5 6 0 1 2 2 */
#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...