#include "jumps.h"
#include <bits/stdc++.h>
#define ii pair<int,int>
#define F first
#define S second
using namespace std;
const int NN=2e5+100;
const int L=18;
const int INF=1e9;
int a[NN];
bool vis[NN];
int seg[4*NN];
vector<pair<int,ii>> mst[NN];
int n;
vector<int> g[NN];
vector<int> vec;
int st[NN][L][2];
void build2(int p,int s,int e)
{
if(s==e){
seg[p]=a[s];
return;
}
int mid=(s+e)/2;
build2(2*p,s,mid);
build2(2*p+1,mid+1,e);
seg[p]=max( seg[2*p],seg[2*p+1]);
}
int get2(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(get2(2*p,s,mid,a,b),get2(2*p+1,mid+1,e,a,b));
}
void compine(int p)
{
int sz=mst[p*2].size();
int sz2=mst[p*2+1].size();
int l1=0,l2=0;
while(l1<sz&&l2<sz2){
if(mst[p*2][l1]<mst[p*2+1][l2]){
mst[p].push_back({mst[p*2][l1].F,{l1,l2-1}});
l1++;
}
else{
mst[p].push_back({mst[p*2+1][l2].F,{l1-1,l2}});
l2++;
}
}
while(l1<sz){
mst[p].push_back({mst[p*2][l1].F,{l1,l2-1}});
l1++;
}
while(l2<sz2){
mst[p].push_back({mst[p*2+1][l2].F,{l1-1,l2}});
l2++;
}
}
void build(int p,int s,int e)
{
int mid=(s+e)/2;
if(s==e){
mst[p].push_back({a[s],{0,0}});
return;
}
build(p*2,s,mid);
build(p*2+1,mid+1,e);
if(s!=e)compine(p);
}
int get(int p,int s,int e,int a,int b,int i)
{
// cout<<p<<" "<<s<<" "<<e<<" "<<i<<endl;
if(a<=s&&e<=b){
if(i==-1)return n+1;
return mst[p][i].F;
}
if(s>b||e<a)return 0;
int i1=mst[p][i].S.F;
int i2=mst[p][i].S.S;
int mid=(s+e)/2;
return max(get(p*2,s,mid,a,b,i1)
,get(p*2+1,mid+1,e,a,b,i2));
}
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);
build2(1,0,n-1);
for(int i=0;i<n;i++){
if(i!=n-1&&get2(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=get2(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&&get2(1,0,n-1,0,i)!=a[i]){
int ans,l=0,r=i;
while(l<=r){
int mid=(l+r)/2;
int v=get2(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());
if(g[a[i]].size()>0){
st[a[i]][0][0]=g[a[i]][0];
}
else{
st[a[i]][0][0]=n+1;
}
if(g[a[i]].size()>1){
st[a[i]][0][1]=g[a[i]][1];
}
else{
st[a[i]][0][1]=n+1;
}
}
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;
r=i;
r=st[r][j-1][1];
r=st[r][j-1][1];
st[i][j][1]=r;
}
}
}
int dfs(int u,int tar)
{
int ret=0;
for(int i=1;i>=0;i--){
for(int j=L-1;j>=0;j--){
int r=st[u][j][i];
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){
if(a[C]==1)return -1;
int u=get(1,0,n-1,A,B,a[C]-1);
// cout<<u<<endl;
int tar=a[C];
int ans=dfs(u,tar);
if(ans==INF)ans=-1;
return ans;
}
Compilation message
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:111:17: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
111 | int ans,l=i,r=n-1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9672 KB |
Output is correct |
3 |
Runtime error |
81 ms |
54784 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
9636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
9636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
9672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9672 KB |
Output is correct |
2 |
Correct |
9 ms |
9672 KB |
Output is correct |
3 |
Correct |
6 ms |
9708 KB |
Output is correct |
4 |
Runtime error |
1054 ms |
109256 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9672 KB |
Output is correct |
2 |
Correct |
9 ms |
9672 KB |
Output is correct |
3 |
Correct |
6 ms |
9708 KB |
Output is correct |
4 |
Runtime error |
1054 ms |
109256 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9672 KB |
Output is correct |
3 |
Runtime error |
81 ms |
54784 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |