#include "jumps.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cstring>
#include <vector>
#include <string>
#include <climits>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <list>
using namespace std;
typedef long long ll;
ll n;
struct segtree{
vector<pair<ll,ll>> tree;
segtree(): tree(808080){}
void init(ll node, ll s, ll e, vector<ll> &v){
if(s==e)tree[node] = {v[s],s};
else{
ll mid = s+e>>1;
init(node<<1,s,mid,v); init(node<<1|1,mid+1,e,v);
tree[node] = max(tree[node<<1],tree[node<<1|1]);
}
}
pair<ll,ll> query(ll node, ll s, ll e, ll l, ll r){
if(l>r)return {-1,-1};
if(e<l or r<s)return {0,0};
if(l<=s and e<=r)return tree[node];
ll mid = s+e>>1;
return max(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r));
}
pair<ll,ll> Q(ll l, ll r){ return query(1,1,n,l,r); }
} seg;
vector<ll> v, Rv;
ll L[202020][22];
ll R[202020][22];
ll M[202020][22];
ll RL[202020][22];
ll RR[202020][22];
ll RM[202020][22];
void init(int N, std::vector<int> H) {
n=N;
v.push_back(0); Rv=v;
for(int i = 1 ; i <= n ; i++)v.push_back(H[i-1]), Rv.push_back(H[n-i]);
seg.init(1,1,n,v);
stack<ll> st;
for(int i = 1 ; i <= n ; i++){
while(st.size() and v[st.top()] < v[i]){
R[st.top()][0] = i;
st.pop();
}
st.push(i);
}
while(st.size())st.pop();
for(int i = n ; i >= 1 ; i--){
while(st.size() and v[st.top()] < v[i]){
L[st.top()][0] = i;
st.pop();
}
st.push(i);
}
for(int i = 1 ; i <= n ; i++){
M[i][0] = (v[L[i][0]] > v[R[i][0]] ? L[i][0] : R[i][0]);
}
for(int i = 1 ; i <= n ; i++){
RL[i][0] = n-L[n-i+1][0]+1;
RR[i][0] = n-R[n-i+1][0]+1;
RM[i][0] = n-M[n-i+1][0]+1;
}
for(int j = 1 ; j <= 20 ; j++){
for(int i = 1 ; i <= n ; i++){
L[i][j] = L[L[i][j-1]][j-1];
R[i][j] = R[R[i][j-1]][j-1];
M[i][j] = M[M[i][j-1]][j-1];
RL[i][j] = RL[RL[i][j-1]][j-1];
RR[i][j] = RR[RR[i][j-1]][j-1];
RM[i][j] = RM[RM[i][j-1]][j-1];
}
}
}
ll AB, BC, CD;
ll c;
ll solve_terminal(ll A, ll B, ll C){
ll D=C;
ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
ll ret = 0;
ll cur = B;
for(int i = 20 ; i >= 0 ; i--){
if(L[cur][i]<=0)continue;
if(L[cur][i]>=A and v[L[cur][i]] < CD)cur = L[cur][i];
}
for(int i = 20 ; i >= 0 ; i--){
if(M[cur][i]<=0)continue;
if(v[M[cur][i]]<CD and M[cur][i]<C)cur = M[cur][i], ret += (1<<i);
}
for(int i = 20 ; i >= 0 ; i--){
if(R[cur][i]<=0)continue;
if(R[cur][i]<C)cur = R[cur][i], ret += (1<<i);
}
ret++; cur = R[cur][0];
if(cur<C or cur>D)ret = 1e9;
return ret;
}
ll solve_terminal_reverse(ll A, ll B, ll C){
ll D=C;
ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
ll ret = 0;
ll cur = B;
for(int i = 20 ; i >= 0 ; i--){
if(RL[cur][i]<=0)continue;
if(RL[cur][i]>=A and Rv[RL[cur][i]] < CD)cur = RL[cur][i];
}
for(int i = 20 ; i >= 0 ; i--){
if(RM[cur][i]<=0)continue;
if(Rv[RM[cur][i]]<CD and RM[cur][i]<C)cur = RM[cur][i], ret += (1<<i);
}
for(int i = 20 ; i >= 0 ; i--){
if(RR[cur][i]<=0)continue;
if(RR[cur][i]<C)cur = RR[cur][i], ret += (1<<i);
}
ret++; cur = RR[cur][0];
if(cur<C or cur>D)ret = 1e9;
return ret;
}
int minimum_jumps(int A, int B, int C, int D) {
A++; B++; C++; D++;
ll cur = B;
AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
ll ret = 1e9;
if(AB>BC){
ll cur = B;
for(int i = 20 ; i >= 0 ; i--){
if(L[cur][i]<=0)continue;
if(v[L[cur][i]]<BC)cur = L[cur][i];
}
if(v[cur]<BC)cur = L[cur][0];
cur = R[cur][0];
if(C<=cur and cur<=D)return 1;
}
if(BC>0){
ll pos = seg.Q(B+1,C-1).second;
ret = min(ret, solve_terminal(A,B,pos)+1);
}
if(seg.Q(1,A-1).first > BC){
ll cur = A-1;
for(int i = 20 ; i >= 0 ; i--){
if(L[cur][i]<=0)continue;
if(v[L[cur][i]]<BC)cur = L[cur][i];
}
if(v[cur]<BC)cur = L[cur][0];
ret = min(ret, solve_terminal_reverse(n-B+1, n-A+1, n-cur+1)+1);
}
if(ret==1e9)ret = -1;
return ret;
}
Compilation message
jumps.cpp: In member function 'void segtree::init(ll, ll, ll, std::vector<long long int>&)':
jumps.cpp:26:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | ll mid = s+e>>1;
| ~^~
jumps.cpp: In member function 'std::pair<long long int, long long int> segtree::query(ll, ll, ll, ll, ll)':
jumps.cpp:35:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | ll mid = s+e>>1;
| ~^~
jumps.cpp: In function 'll solve_terminal(ll, ll, ll)':
jumps.cpp:91:8: warning: unused variable 'AB' [-Wunused-variable]
91 | ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
| ^~
jumps.cpp:91:31: warning: unused variable 'BC' [-Wunused-variable]
91 | ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
| ^~
jumps.cpp: In function 'll solve_terminal_reverse(ll, ll, ll)':
jumps.cpp:112:8: warning: unused variable 'AB' [-Wunused-variable]
112 | ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
| ^~
jumps.cpp:112:31: warning: unused variable 'BC' [-Wunused-variable]
112 | ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
| ^~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:133:8: warning: unused variable 'cur' [-Wunused-variable]
133 | ll cur = B;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21332 KB |
Output is correct |
2 |
Correct |
4 ms |
21080 KB |
Output is correct |
3 |
Correct |
273 ms |
192764 KB |
Output is correct |
4 |
Correct |
1255 ms |
227832 KB |
Output is correct |
5 |
Correct |
994 ms |
134332 KB |
Output is correct |
6 |
Correct |
1247 ms |
227836 KB |
Output is correct |
7 |
Correct |
909 ms |
172456 KB |
Output is correct |
8 |
Correct |
1294 ms |
227824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
23128 KB |
Output is correct |
2 |
Correct |
4 ms |
21080 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
5 ms |
21080 KB |
Output is correct |
5 |
Incorrect |
4 ms |
23128 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
23128 KB |
Output is correct |
2 |
Correct |
4 ms |
21080 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
5 ms |
21080 KB |
Output is correct |
5 |
Incorrect |
4 ms |
23128 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
23128 KB |
Output is correct |
2 |
Correct |
4 ms |
21304 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21080 KB |
Output is correct |
5 |
Incorrect |
155 ms |
195972 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
4 ms |
21080 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Incorrect |
304 ms |
124364 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
4 ms |
21080 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Incorrect |
304 ms |
124364 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21332 KB |
Output is correct |
2 |
Correct |
4 ms |
21080 KB |
Output is correct |
3 |
Correct |
273 ms |
192764 KB |
Output is correct |
4 |
Correct |
1255 ms |
227832 KB |
Output is correct |
5 |
Correct |
994 ms |
134332 KB |
Output is correct |
6 |
Correct |
1247 ms |
227836 KB |
Output is correct |
7 |
Correct |
909 ms |
172456 KB |
Output is correct |
8 |
Correct |
1294 ms |
227824 KB |
Output is correct |
9 |
Correct |
4 ms |
23128 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21080 KB |
Output is correct |
12 |
Correct |
5 ms |
21080 KB |
Output is correct |
13 |
Incorrect |
4 ms |
23128 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |