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;
//#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 3e5 + 5;
const int oo = 1e9 + 7, mod = 1e9 + 7;
int n, h[N];
int nxtl[N], nxtr[N];
int small_jump[N][20], large_jump[N][20];
ii mx[N][20];
void init(int _n, vector<int> _H){
n = _n;
for(int i = 1; i <= n; i++) h[i] = _H[i - 1];
h[0] = h[n + 1] = oo;
stack<int> stk;
stk.push(0);
for(int i = 1; i <= n; i++){
while(h[stk.top()] < h[i]) stk.pop();
nxtl[i] = stk.top();
stk.push(i);
}
//return;
while(!stk.empty()) stk.pop();
stk.push(n + 1);
for(int i = n; i >= 1; i--){
while(h[stk.top()] < h[i]) stk.pop();
nxtr[i] = stk.top();
stk.push(i);
}
//return;
for(int i = 1; i <= n; i++){
small_jump[i][0] = (h[nxtl[i]] < h[nxtr[i]] ? nxtl[i] : nxtr[i]);
large_jump[i][0] = (h[nxtl[i]] > h[nxtr[i]] ? nxtl[i] : nxtr[i]);
}
for(int i = 1; i <= n; i++) mx[i][0] = {h[i], i};
for(int i = 1; i <= 19; i++){
for(int j = 1; (j + (1LL << i) - 1) <= n; j++) mx[j][i] = max(mx[j][i - 1], mx[j + (1LL << (i - 1))][i - 1]);
}
for(int i = 1; i <= 19; i++){
for(int j = 1; j <= n; j++){
small_jump[j][i] = small_jump[small_jump[j][i - 1]][i - 1];
large_jump[j][i] = large_jump[large_jump[j][i - 1]][i - 1];
}
}
}
ii maxi(int l, int r){
if(l > r) return {-oo, -oo};
int k = log2(r - l + 1);
return max(mx[l][k], mx[r - (1LL << k) + 1][k]);
}
int minimum_jumps(int a, int b, int c, int d){
//return -1;
a++, b++, c++, d++;
assert(a == b && c == d);
int answer = 0;
for(int i = 19; i >= 0; i--){
if(h[large_jump[a][i]] <= h[c]){
answer += (1LL << i);
a = large_jump[a][i];
}
}
for(int i = 19; i >= 0; i--){
if(h[small_jump[a][i]] <= h[c]){
answer += (1LL << i);
a = small_jump[a][i];
}
}
if(a != c) return -1;
else return answer;
}
/*
void process(){
}
signed main(){
ios_base::sync_with_stdio(0);
process();
}*/
# | 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... |