This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "jumps.h"
#include <vector>
int subtask = -1;
#define MAX_N 200000
#define MAX_M (2*MAX_N)
int tin[MAX_N], tout[MAX_N], head[MAX_N], nxt[MAX_M], vv[MAX_M], indeg[MAX_N], rt, timer, dd[MAX_N];
void link(int u, int v)
{
static int i = 1;
nxt[i] = head[u];
vv[i] = v;
++indeg[v];
head[u] = i++;
}
void dfs(int u)
{
tin[u] = timer++;
for (int j = head[u]; j; j = nxt[j]) dd[vv[j]] = dd[u] + 1, dfs(vv[j]);
tout[u] = timer - 1;
}
void init(int N, std::vector<int> H) {
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]) link(stk[--so], i);
so = 0;
for (int i = N - 1; i >= 0; stk[so++] = i--)
while (so && H[stk[so-1]] < H[i]) link(stk[--so], i);
for (int i = 0; i < N; ++i)
if (indeg[i] == 0) rt = i;
dfs(rt);
}
int minimum_jumps(int A, int B, int C, int D) {
if (subtask == 1)
{
if (A > D) return -1;
if (B >= C) return 0;
return C - B;
}
int z = 1e9;
for (int i = A; i <= B; ++i)
for (int j = C; j <= D; ++j)
if (tin[i] <= tin[j] && tout[j] <= tout[i] && dd[i] - dd[j] < z) z = dd[i] - dd[j];
if (z == 1e9) return -1;
return z;
}
# | 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... |