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], lg2[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++) lg2[i] = log2(i);
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 = lg2[r - l + 1];
return max(mx[l][k], mx[r - (1LL << k) + 1][k]);
}
int cal(int a, int c){
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;
}
int cal2(int a, int b, int c){
//assert(c == d);
if(h[b] > h[c]){
return -1;
}
int l = a, r = b;
while(l < r){
int mid = (l + r) >> 1;
if(maxi(mid, b).fi > h[c]) l = mid + 1;
else r = mid;
}
int pos = maxi(l, b).se;
return cal(pos, c);
}
int minimum_jumps(int a, int b, int c, int d){
//return -1;
a++, b++, c++, d++;
if(b == (c - 1)){
if(h[b] > maxi(c, d).fi) return -1;
else return 1;
}
ii temp1 = maxi(a, b), temp2 = maxi(b + 1, c - 1), temp3 = maxi(c, d);
if(min(temp1.fi, temp3.fi) > temp2.fi){
int l = a, r = b;
while(l < r){
int mid = (l + r + 1) >> 1;
if(maxi(mid, b).fi < h[c]) r = mid - 1;
else l = mid;
}
if(temp3.fi > h[l]) return 1;
else a = l + 1;
}
if(temp3.fi < temp2.fi) return -1;
int answer = oo;
answer = min(answer, cal2(a, b, temp2.se) + 1);
if(a && maxi(1, a - 1).fi > h[c]){
int l = 1, r = a - 1;
while(l < r){
int mid = (l + r + 1) >> 1;
if(maxi(mid, a - 1).fi < h[c]) r = mid - 1;
else l = mid;
}
if(h[l] < temp3.fi) answer = min(answer, cal2(a, b, l) + 1);
}
return (answer == oo ? -1 : 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... |