Submission #550370

#TimeUsernameProblemLanguageResultExecution timeMemory
550370Killer2501Rainforest Jumps (APIO21_jumps)C++14
100 / 100
1173 ms50540 KiB
#include "jumps.h" #include<bits/stdc++.h> using namespace std; const int N = 2e5+5; int L[N][20], R[N][20], P[N][20], a[N]; void init(int n, vector<int> A) { for(int i = 1; i <= n; i ++)a[i] = A[i-1]; stack<int> st; for(int i = 1; i <= n; i ++) { while(!st.empty() && a[i] > a[st.top()])st.pop(); L[i][0] = st.empty() ? 0 : st.top(); st.push(i); } while(!st.empty())st.pop(); for(int i = n; i > 0; i --) { while(!st.empty() && a[i] > a[st.top()])st.pop(); R[i][0] = st.empty() ? n+1 : st.top(); P[i][0] = (a[L[i][0]] < a[R[i][0]] ? R[i][0] : L[i][0]); st.push(i); } //for(int i = 1; i <= n; i ++)cout << L[i][0] <<" "<<R[i][0] <<" "<<P[i][0] << '\n'; for(int j = 1; j <= 18; j ++) { for(int i = 1; i <= n; i ++) { L[i][j] = L[L[i][j-1]][j-1]; if(R[i][j-1] == n+1)R[i][j] = n+1; else R[i][j] = R[R[i][j-1]][j-1]; P[i][j] = P[P[i][j-1]][j-1]; } } } int minimum_jumps(int A, int B, int C, int D) { ++A, ++B, ++C, ++D; int pos = B, res = 0; for(int j = 18; j >= 0; j --) { if(L[pos][j] >= A && R[L[pos][j]][0] <= D)pos = L[pos][j]; } for(int j = 18; j >= 0; j --) { if(P[pos][j] && R[P[pos][j]][0] < C) { pos = P[pos][j]; res += (1<<j); } } if(R[pos][0] < C && R[P[pos][0]][0] <= D) { ++res; pos = P[pos][0]; } //cout << pos <<" "<<res<<" "; for(int j = 18; j >= 0; j --) { if(R[pos][j] < C) { pos = R[pos][j]; res += (1<<j); } } if(R[pos][0] > D)return -1; return res+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...