This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int K = 18;
const int INF = 1e9;
int up[K][N], mvr[K][N], mvl[K][N], h[N], idx[N], lh[N], rh[N], n;
int sp[K][N], lg[N];
int ml[N],mr[N],best[N],arr[N];
int MAXN;
int que(int l, int r) {
int i=lg[r-l+1];
return max(sp[i][l], sp[i][r-(1<<i)+1]);
}
void init(int N, std::vector<int> H) {
n=N;
for (int i=0; i<n; ++i) h[i+1]=H[i], idx[h[i+1]]=i+1;
h[0]=INF-1, h[n+1]=INF;
stack<int> st; st.push(0);
for (int i=1; i<=n; ++i) {
while (h[st.top()] <= h[i]) st.pop();
assert(!st.empty());
lh[i]=st.top(), st.push(i);
} st=stack<int>();
for (int i=1; i<=n; ++i) sp[0][i]=h[i];
for (int j=1; j<K; ++j) {
for (int i=1; i+(1<<(j-1))<=n; ++i) sp[j][i]=max(sp[j-1][i], sp[j-1][i+(1<<(j-1))]);
}
lg[1]=0;
for (int i=2; i<N; ++i) lg[i]=lg[i/2]+1;
st.push(n+1);
for (int i=n; i>=1; --i) {
while (h[st.top()] <= h[i]) st.pop();
assert(!st.empty());
rh[i]=st.top(), st.push(i);
}
// assume A=B, C=D for testing reasons
for (int i=n; i>=1; --i) {
int j=idx[i];
int r=rh[j], l=lh[j];
assert(h[l] > h[j] && h[j] < h[r]);
if (h[l] > h[r] && l > 0) up[0][j]=l;
else up[0][j]=r;
for (int k=1; k<K; ++k) up[k][j]=up[k-1][up[k-1][j]];
}
for (int k=0; k<K; ++k) mvr[k][n+1]=n+1;
for (int i=n; i>=1; --i) {
int j=idx[i];
mvr[0][j]=rh[j];
for (int k=1; k<K; ++k) mvr[k][j]=mvr[k-1][mvr[k-1][j]];
}
for (int i=n; i>=1; --i) {
int j=idx[i];
mvl[0][j]=lh[j];
for (int k=1; k<K; ++k) mvl[k][j]=mvl[k-1][mvl[k-1][j]];
}
for(int i = 1;i<=n;i++){
ml[i] = lh[i];
mr[i] = rh[i];
best[i] = up[0][i];
arr[i] = h[i];
}
MAXN = n;
}
int minimum_jumps(int a,int b,int c,int d){
a ++,b ++;
c ++,d ++;
// if(one) return c - b;
int m2 = c;
for(int i = c;i <= d;i ++){
if(arr[i] > arr[m2]) m2 = i;
}
int m1 = MAXN + 1;
for(int i = a;i <= b;i++){
if(arr[i] > arr[m1] && arr[i] < arr[m2]) m1 = i;
}
if(m1 == MAXN + 1) return -1;
int last = m1;
int ans = 0;
while(arr[best[last]] < arr[m2]){
if(last >= c && last <= d) return ans;
if(mr[last] >= c && mr[last] <= d) return ans + 1;
last = best[last];
assert(last < d && last != MAXN);
ans ++;
}
while(!(last >= c && last <= d)){
if(arr[last] > arr[m2] || last > d) return -1;
last = mr[last];
ans ++;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |