# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
549783 | fcmalkcin | 밀림 점프 (APIO21_jumps) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
#define pb push_back
#define F(i,a,b) for(ll i=a;i<=b;i++)
const ll maxn=3e5+10 ;
const ll base=1e9;
const ll mod= 1e9+7 ;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
/// have medal in APIO
/// goal 2/8
ll nxtr[maxn][20];
ll nxtl[maxn][20];
ll nxtmx[maxn][20];
ll b[maxn];
ll pos[maxn];
void init(ll n,ll a[])
{
stack<ll> st;
for (int t=0;t<20;t++) nxtr[n+1][t]=n+1;
for (int i=n;i>=1;i--)
{
pos[a[i-1]]=i;
while (st.size()&&a[st.top()-1]<a[i-1]) st.pop();
if (st.size()) nxtr[i][0]=st.top();
else nxtr[i][0]=n+1;
for (int t=1;t<20;t++) nxtr[i][t]=nxtr[nxtr[i][t-1]][t-1];
st.push(i);
}
while (st.size()) st.pop();
for (int i=1;i<=n;i++)
{
while (st.size()&&a[st.top()-1]<a[i-1]) st.pop();
if (st.size()) nxtl[i][0]=st.top();
else nxtl[i][0]=0;
for (int t=1;t<20;t++) nxtl[i][t]=nxtl[nxtl[i][t-1]][t-1];
st.push(i);
}
for (int i=n;i>=1;i--)
{
pll nw=make_pair(0,i);
ll id=pos[i];
if (nxtl[id][0]!=0)
{
nw=max(nw,make_pair(a[nxtl[id][0]-1],nxtl[id][0]));
}
if (nxtr[id][0]!=n+1)
{
nw=max(nw,make_pair(a[nxtr[id][0]-1],nxtr[id][0]));
}
nxtmx[id][0]=nw.ss;
for (int t=1;t<20;t++) nxtmx[id][t]=nxtmx[nxtmx[id][t-1]][t-1];
}
}
ll minimum_jumps(ll a,ll b,ll c,ll d)
{
a++;
b++;
c++;
d++;
ll nw=b;
for (int t=19;t>=0;t--)
{
if (nxtr[nxtl[nw][t]][0]<=d&&nxtl[nw][t]>=a)
{
nw=nxtl[nw][t];
}
}
if (nxtr[nw][0]>d) return -1;
ll ans=0;
for (int t=19;t>=0;t--)
{
ll h=nxtmx[nw][t];
if (nxtr[h][0]<=d)
{
ans+=(1ll<<t);
nw=h;
}
}
for (int t=19;t>=0;t--)
{
if (nxtr[nw][t]<=d)
{
// cout <<nw<<" "<<t<<" wtf"<<" "<<nxtr[nw][t]<<endl;
nw=nxtr[nw][t];
ans+=(1ll<<t);
}
}
// cout <<nw<<" "<<nxtr[nw][0]<<" "<<nxtmx[nw][0]<<" "<<nxtr[nw][0]<<endl;
if (nw>=c)
{
return ans;
}
else
{
return -1;
}
}
/*int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp","r"))
{
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ll a[10]={3, 2, 1, 6, 4, 5, 7};
init(7,a);
cout <<minimum_jumps(4, 4, 6, 6);
}*/