이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O2,unroll-loops")
#include "jumps.h"
#include <vector>
#include <algorithm>
int subtask = -1;
#define MAX_N 200000
#define MAX_M (2*MAX_N)
int N, head[MAX_N], nxt[MAX_M], vv[MAX_M], ng[MAX_N], pg[MAX_N], HI[MAX_N], H[MAX_N];
int dd[MAX_N], pp[MAX_N], jj[MAX_N], dd_[MAX_N], pp_[MAX_N], jj_[MAX_N];
int dd1[MAX_N],pp1[MAX_N],jj1[MAX_N],ee1[MAX_N];
int dd2[MAX_N],pp2[MAX_N],jj2[MAX_N],ee2[MAX_N];
/*
* dd - high link
* dd_ - low link
* dd1 - left link
* dd2 - right link */
void link(int u, int v)
{
static int i = 1;
nxt[i] = head[u];
vv[i] = v;
head[u] = i++;
}
void binlift_connect(int*dd,int*pp,int*jj,int p,int u)
{
dd[u] = dd[p] + 1;
pp[u] = p;
jj[u] = dd[p] - dd[jj[p]] == dd[jj[p]] - dd[jj[jj[p]]] ? jj[jj[p]] : p;
}
void init(int N, std::vector<int> H) {
::N = N;
if (N <= 200) subtask = 2;
else if (N <= 2000) subtask = 3;
int flag = 1;
for (int i = 1; i < N; ++i) if (H[i] != H[i-1] + 1) flag = 0;
if(flag) subtask = 1;
static int stk[MAX_N], so;
so = 0;
for (int i = 0; i < N; stk[so++] = i++) while (so && H[stk[so-1]] < H[i]) ng[stk[so-1]]=i, link(stk[--so], i);
while(so)ng[stk[--so]]=-1;
for (int i = N - 1; i >= 0; stk[so++] = i--) while (so && H[stk[so-1]] < H[i]) pg[stk[so-1]]=i, link(stk[--so], i);
while(so)pg[stk[--so]]=-1;
for (int i = 0; i < N; ++i) HI[::H[i]=H[i]-1] = i;
for (int j = N; j--;)
{
int i = HI[j];
int high = (pg[i] == -1) ? ng[i] : (ng[i] == -1 ? pg[i] : (H[ng[i]] > H[pg[i]] ? ng[i] : pg[i]));
int low = pg[i] + ng[i] - high;
if (high != -1) binlift_connect(dd, pp, jj, high, i);
else pp[i] = jj[i] = i;
if (low != -1) binlift_connect(dd_, pp_, jj_, low, i);
else pp_[i] = jj_[i] = i;
}
if (subtask == -1) subtask = 4;
for(int i=0;i<N;++i)
{
/* left */
if(pg[i]==-1) { pp1[i]=jj1[i]=i,dd1[i]=0; }
else{
binlift_connect(dd1,pp1,jj1,pg[i],i);
if(jj1[i]==pp1[i])ee1[i]=H[pg[i]];
else ee1[i]=std::max(ee1[jj1[pp1[i]]],ee1[pp1[i]]);
}
/* right */
if(ng[i]==-1) { pp2[i]=jj2[i]=i,dd2[i]=0; }
else {
binlift_connect(dd2,pp2,jj2,ng[i],i);
if(jj2[i]==pp2[i])ee2[i]=H[ng[i]];
else ee2[i]=std::max(ee2[jj2[pp2[i]]],ee2[pp2[i]]);
}
}
}
int Q_COUNTER = 0;
int minimum_jumps(int A, int B, int C, int D) {
++Q_COUNTER;
if (subtask == 1) return (A>D)?-1:(B>=C?0:C-B);
if (subtask == 4 && Q_COUNTER > 5) subtask = 5;
if(subtask <= 4)
{
static int d[MAX_N], qu[MAX_N], qh=0, qt=0,z=1e9;
qh=qt=0,z=1e9;
for(int j=0;j<N;++j)d[j]=1e9;
for (int i = A; i <= B; ++i) d[qu[qh++]=i]=0;
while(qh-qt) for(int u=qu[qt++],j=head[u];j;j=nxt[j]) if(d[vv[j]]>d[u]+1)d[vv[j]]=d[u]+1,qu[qh++]=vv[j];
for (int j = C; j <= D; ++j)if(d[j]!=1e9&&d[j]<z)z=d[j];
return(z<1e9?z:-1);
}
if (subtask == 5 && A != B) subtask = 6;
if (subtask == 6 && C != D) subtask = 7;
if (subtask == 6)
{
if(H[B]>H[C])return -1;
int u=B;
for(;pp1[u]!=u;)
{
if(jj1[u]>=A&&ee1[u]<H[C])u=jj1[u];
else if(pp1[u]>=A&&H[pp1[u]]<H[C])u=pp1[u];
else break;
}
A=u;
}
if (subtask == 5 || subtask == 6)
{
if(A==-1)return -1;
int u=A,u1;
for(;pp[u]!=u;)
{
if (H[jj[u]]<=H[C])u=jj[u];
else if(H[pp[u]]<=H[C])u=pp[u];
else break;
}
u1=u;
for(;pp_[u]!=u;)
{
if(H[jj_[u]]<=H[C])u=jj_[u];
else if(H[pp_[u]]<=H[C])u=pp_[u];
else break;
}
if(u!=C)return -1;
return dd_[u1]-dd_[u]+dd[A]-dd[u1];
}
if (subtask == 7)
{
int highest_left;
{
int u=B;
for(;pp1[u] != u;)
{
if(jj1[u]>=A)u=jj1[u];
else if(pp1[u]>=A)u=pp1[u];
else break;
}
highest_left=u;
}
/* the right endpoint is a position that is at lower_bound(highest_left) */
{
int u=C;
for(;pp2[u]!=u;)
{
if(jj2[u]<=D&&ee2[u]<H[highest_left])u=jj2[u];
else if(pp2[u]<=D&&H[pp2[u]]<H[highest_left])u=pp2[u];
else break;
}
if (u!=pp2[u]&&pp2[u]<=D)u=pp2[u];
C=u;
}
/* select highest left endpoint possible */
{
int u=B;
for(;pp1[u]!=u;)
{
if(jj1[u]>=A&&ee1[u]<H[C])u=jj1[u];
else if(pp1[u]>=A&&H[pp1[u]]<H[C])u=pp1[u];
else break;
}
}
{
if(A==-1)return -1;
int u=A,u1;
for(;pp[u]!=u;)
{
if (H[jj[u]]<=H[C])u=jj[u];
else if(H[pp[u]]<=H[C])u=pp[u];
else break;
}
u1=u;
for(;pp_[u]!=u;)
{
if(H[jj_[u]]<=H[C])u=jj_[u];
else if(H[pp_[u]]<=H[C])u=pp_[u];
else break;
}
if(u!=C)return -1;
return dd_[u1]-dd_[u]+dd[A]-dd[u1];
}
}
return -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... |