#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///APIO 2021 vc
#include "jumps.h"
vector<vector<ll> >adj;
ll L[200005],R[200005];
ll l[200005][20],r[200005][20];
ll maxi[200005][20];
vector<int>arr;
ll n;
void init(int N, std::vector<int> H){
n = N;
arr = H;
priority_queue<ii>pq;
f(i,0,N){
L[i] = R[i] = INF;
}
f(i,0,N){
while(!pq.empty() && -pq.top().F < H[i]){
R[pq.top().S] = i;
pq.pop();
}
pq.push(ii(-H[i],i));
}
while(!pq.empty()){
pq.pop();
}
for(ll i = N-1;i >= 0;i--){
while(!pq.empty() && -pq.top().F < H[i]){
L[pq.top().S] = i;
pq.pop();
}
pq.push(ii(-H[i],i));
}
adj.assign(N+5,vll());
f(i,0,N){
if(L[i] != INF){
adj[i].pb(L[i]);
}
if(R[i] != INF){
adj[i].pb(R[i]);
}
}
f(i,0,N){
l[i][0] = L[i];
r[i][0] = R[i];
if(max(L[i],R[i]) == INF){
maxi[i][0] = INF;
}
else if(H[L[i]] > H[R[i]]){
maxi[i][0] = L[i];
}
else{
maxi[i][0] = R[i];
}
}
f(j,1,20){
f(i,0,N){
l[i][j] = l[min(n - 1,l[i][j-1])][j-1];
r[i][j] = r[min(n - 1,r[i][j-1])][j-1];
ll v = maxi[i][j-1];
if(v == INF || max(L[v],R[v]) == INF){
maxi[i][j] = INF;
}
else if(H[L[v]] > H[R[v]]){
maxi[i][j] = L[v];
}
else{
maxi[i][j] = R[v];
}
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
ll s = B;
///A = B and C = D
ll ans = 0;
for(ll j = 19;j >= 0;j--){
if(maxi[s][j] != INF && arr[maxi[s][j]] <= arr[C]){
s = maxi[s][j];
ans += (1LL<<j);
}
}
ll posa = 0;
if(L[s] != INF && arr[L[s]] <= arr[C]){
posa++;
for(ll j = 19;j >= 0;j--){
if(l[s][j] != INF && arr[l[s][j]] <= arr[C]){
ans += (1LL<<j);
s = l[s][j];
}
}
}
if(R[s] != INF && arr[R[s]] <= arr[C]){
posa++;
for(ll j = 19;j >= 0;j--){
if(r[s][j] != INF && arr[r[s][j]] <= arr[C]){
ans += (1LL<<j);
s = r[s][j];
}
}
}
assert(posa != 2);
if(s != C){
ans = -1;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
224 ms |
90612 KB |
Output is correct |
4 |
Correct |
1218 ms |
113800 KB |
Output is correct |
5 |
Correct |
987 ms |
57468 KB |
Output is correct |
6 |
Correct |
1056 ms |
113704 KB |
Output is correct |
7 |
Correct |
867 ms |
77732 KB |
Output is correct |
8 |
Correct |
1180 ms |
113700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
200 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Incorrect |
360 ms |
50712 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
200 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Incorrect |
360 ms |
50712 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
224 ms |
90612 KB |
Output is correct |
4 |
Correct |
1218 ms |
113800 KB |
Output is correct |
5 |
Correct |
987 ms |
57468 KB |
Output is correct |
6 |
Correct |
1056 ms |
113704 KB |
Output is correct |
7 |
Correct |
867 ms |
77732 KB |
Output is correct |
8 |
Correct |
1180 ms |
113700 KB |
Output is correct |
9 |
Incorrect |
0 ms |
328 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |