This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://www.instagram.com/_modwwe/
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2")
#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int inf=1e9;
const int mod2=998244353;
const int mod1=998244353;
struct icd
{
long double a;
int b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c,d,e;
};
int n,m,s1,s2,s4,s3,sf,k,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,s33,dem3,l,r,mid;
int i,s10,s12;
int kk;
int el=29;
int st[18][200002];
int st2[18][200002];
int st3[18][200002];
int a[200002];
int c[200002];
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { assert(l <= r); return uniform_int_distribution<ll>(l, r)(rd); }
void init(int N, vector<int> H)
{
n=N;
for(int i=1; i<=n; i++)
a[i]=H[i-1],st2[0][i]=a[i],c[a[i]]=i;
c[n+1]=n+1;
stack<int> s;
for(int i=1; i<=n; i++)
{
while(!s.empty()&&s.top()<a[i])st[0][s.top()]=a[i],st3[0][s.top()]=a[i], s.pop();
s.push({a[i]});
}
while(!s.empty())st3[0][s.top()]=n+1,s.pop();
st[0][n+1]=n+1;
st3[0][n+1]=n+1;
st[0][n]=n+1;
for(int i=n; i>=1; --i)
{
while(!s.empty()&&s.top()<a[i])st[0][s.top()]=max(st[0][s.top()],a[i]), s.pop();
s.push({a[i]});
}
for(int i=1; i<18; i++)
{
for(int j=1; j<=n+1; j++)
st[i][j]=st[i-1][st[i-1][j]],
st3[i][j]=st3[i-1][st3[i-1][j]];
for(int j=1; j<=n; j++)
st2[i][j]=max(st2[i-1][j],st2[i-1][j+(1<<(i-1))]);
}
}
int get(int l,int r)
{
int k = 31 - __builtin_clz(r - l + 1);
return max(st2[k][l],st2[k][r-(1<<k)+1]);
}
int go(int l,int r,int l2,int r2,int e,int f)
{
int rr=r;
while(l<=r)
{
int mid=l+r>>1;
if(get(mid,rr)>r2)l=mid+1;
else r=mid-1;
}
if(l>rr) return -1;
s4=get(rr+1,e);
s2=get(l,rr);
s3=0;
for(int i=17;i>=0;--i)
if(st[i][s2]<s4)
{
s2=st[i][s2];
s3+=(1<<i);
}
if(st[0][s2]<=r2&&a[e]!=s4&&s2<s4)
s2=st[0][s2],s3++;
else if(st[0][s2]<=r2&&s2<s4&&st3[1][s2]<s4)
s2=st[0][s2],s3++;
for(int i=17;i>=0;--i)
if(st3[i][s2]<s4)
{
s2=st3[i][s2];
s3+=(1<<i);
}
if(st3[0][s2]<=r2&&s2<s4){
s3+=1;
s2=s4;
}
if(c[s2]<e)s2=st3[0][s2],s3++;
if(s2>r2) return -1;
if(c[s2]>=e&&c[s2]<=f) return s3;
return -1;
}
int minimum_jumps(int A,int B,int C,int D)
{
A++;
B++;
C++;
D++;
return go(A,B,a[C],get(C,D),C,D);
}
Compilation message (stderr)
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:62:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
62 | for(int i=1; i<=n; i++)
| ^~~
jumps.cpp:64:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
64 | c[n+1]=n+1;
| ^
jumps.cpp: In function 'int go(int, int, int, int, int, int)':
jumps.cpp:99:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
99 | int mid=l+r>>1;
| ~^~
# | 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... |