#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
const int NN=2e5+100;
const int L=23;
const int INF=1e9;
int a[NN];
bool vis[NN];
int seg[4*NN];
int n;
vector<int> g[NN];
vector<int> vec;
int st[NN][L][2];
void build(int p,int s,int e)
{
if(s==e){
seg[p]=a[s];
return;
}
int mid=(s+e)/2;
build(2*p,s,mid);
build(2*p+1,mid+1,e);
seg[p]=max( seg[2*p],seg[2*p+1]);
}
int get(int p,int s,int e,int a, int b)
{
if(a<=s&&e<=b){
return seg[p];
}
if(s>b||e<a){
return 0;
}
int mid=(s+e)/2;
return max(get(2*p,s,mid,a,b),get(2*p+1,mid+1,e,a,b));
}
void init(int N, std::vector<int> H) {
n=N;
for(int i=0;i<L;i++){
st[n+1][i][0]=n+1;
st[n+1][i][1]=n+1;
}
for(int i=0;i<N;i++){
a[i]=H[i];
}
build(1,0,n-1);
for(int i=0;i<n;i++){
if(i!=n-1&&get(1,0,n-1,i,n-1)!=a[i]){
int ans,l=i,r=n-1;
while(l<=r){
int mid=(l+r)/2;
int v=get(1,0,n-1,i+1,mid);
if(v>=a[i]){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
g[a[i]].push_back(a[ans]);
}
if(i!=0&&get(1,0,n-1,0,i)!=a[i]){
int ans,l=0,r=i;
while(l<=r){
int mid=(l+r)/2;
int v=get(1,0,n-1,mid,i-1);
if(v>=a[i]){
ans=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
g[a[i]].push_back(a[ans]);
}
sort(g[a[i]].begin(),g[a[i]].end());
//reverse(g[a[i]].begin(),g[a[i]].end());
/*
cout<<a[i]<<" : ";
for(auto x:g[a[i]])cout<<x<<" ";cout<<endl;\
*/
if(g[a[i]].size()>0){
st[a[i]][0][0]=g[a[i]][0];
// cout<<1<<" ";
}
else{
st[a[i]][0][0]=n+1;
// cout<<0<<" ";
}
if(g[a[i]].size()>1){
st[a[i]][0][1]=g[a[i]][1];
// cout<<1<<"\n";
}
else{
st[a[i]][0][1]=n+1;
// cout<<0<<"\n";
}
}
// cout<<endl;
// cout<<endl;
for(int j=1;j<L;j++){
for(int i=1;i<=n;i++){
int r=i;
r=st[r][j-1][0];
r=st[r][j-1][0];
st[i][j][0]=r;
// cout<<r<<" ";
r=i;
r=st[r][j-1][1];
r=st[r][j-1][1];
st[i][j][1]=r;
}
// cout<<endl;
}
// cout<<endl;
//cout<<endl;
}
int dfs(int u,int tar)
{
int ret=0;
for(int i=1;i>=0;i--){
for(int j=4;j>=0;j--){
int r=st[u][j][i];
// cout<<i<<" "<<r<<"\n";
if(r<=tar){
ret+=(1<<j);
u=r;
}
}
}
if(u==tar)return ret;
else return INF;
}
int minimum_jumps(int A, int B, int C, int D) {
int ans=dfs(a[A],a[C]);
if(ans==INF)ans=-1;
return ans;
}
Compilation message
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:52:17: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | int ans,l=i,r=n-1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4936 KB |
Output is correct |
2 |
Correct |
4 ms |
4936 KB |
Output is correct |
3 |
Incorrect |
1052 ms |
42520 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4936 KB |
Output is correct |
2 |
Correct |
4 ms |
4936 KB |
Output is correct |
3 |
Correct |
4 ms |
4936 KB |
Output is correct |
4 |
Correct |
1147 ms |
26588 KB |
Output is correct |
5 |
Correct |
3256 ms |
51692 KB |
Output is correct |
6 |
Correct |
1032 ms |
12796 KB |
Output is correct |
7 |
Correct |
3260 ms |
51652 KB |
Output is correct |
8 |
Correct |
1111 ms |
21384 KB |
Output is correct |
9 |
Correct |
3143 ms |
51612 KB |
Output is correct |
10 |
Incorrect |
2096 ms |
51700 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4936 KB |
Output is correct |
2 |
Correct |
4 ms |
4936 KB |
Output is correct |
3 |
Correct |
4 ms |
4936 KB |
Output is correct |
4 |
Correct |
1147 ms |
26588 KB |
Output is correct |
5 |
Correct |
3256 ms |
51692 KB |
Output is correct |
6 |
Correct |
1032 ms |
12796 KB |
Output is correct |
7 |
Correct |
3260 ms |
51652 KB |
Output is correct |
8 |
Correct |
1111 ms |
21384 KB |
Output is correct |
9 |
Correct |
3143 ms |
51612 KB |
Output is correct |
10 |
Incorrect |
2096 ms |
51700 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4936 KB |
Output is correct |
2 |
Correct |
4 ms |
4936 KB |
Output is correct |
3 |
Incorrect |
1052 ms |
42520 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |