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 <bits/stdc++.h>
#include <vector>
using namespace std;
const int maxn = 2e5 + 10;
int n,d[maxn];
int a[maxn];
vector <int> adj[maxn];
int par[20][maxn];
int in[maxn],out[maxn],rt,cnt;
void dfs(int x, int p, int dep=0)
{
par[0][x] = p;
in[x]=++cnt;
d[x]=dep;
for (int i=1; i<20; i++) par[i][x]=par[i-1][par[i-1][x]];
for (int i:adj[x])
dfs(i,x,dep+1);
out[x]=cnt;
}
bool check(int u, int v)
{
return in[u]<=in[v]&&out[v]<=out[u];
}
int lca(int u, int v)
{
if (check(u,v)) return u;
if (check(v,u)) return v;
for (int i=19; i>=0; i--)
if (!check(par[i][u],v)) u=par[i][u];
return par[0][u];
}
void addedge(int u, int v)
{
adj[u].push_back(v);
}
void init(int N, std::vector<int> H) {
n=N;
for (int i=0; i<n; i++) a[i]=H[i],adj[i].clear();
deque<int> dq;
for (int i=0; i<n; i++)
{
while (!dq.empty()&&a[dq.back()] < a[i]) dq.pop_back();
if (!dq.empty()) addedge(i,dq.back());
dq.push_back(i);
}
while (!dq.empty()) dq.pop_back();
for (int i=n-1; i>=0; i--)
{
while (!dq.empty()&&a[dq.back()] < a[i]) dq.pop_back();
if (!dq.empty()) addedge(i,dq.back());
dq.push_back(i);
}
rt=-1;
for (int i=0; i<n; i++) if (rt==-1||a[i]>a[rt]) rt=i;
cnt=0;
dfs(rt,rt);
}
int minimum_jumps(int A, int B, int C, int D) {
if (lca(A,C)!=C) return -1;
return d[A]-d[C];
}
# | 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... |