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>
#define pii pair<int,int>
using namespace std;
int n;
vector<int> h, adj[200005];
struct segTree {
int seg[800005][2];
void build(int i, int l, int r, int type) {
if(l==r) {
if(type==1) {
seg[i][0] = 2e9;
seg[i][1] = -2e9;
} else {
seg[i][0] = seg[i][1] = h[l];
}
return;
}
int m = (l+r)/2;
build(i*2+1, l, m, type);
build(i*2+2, m+1, r, type);
seg[i][0] = min(seg[i*2+1][0], seg[i*2+2][0]);
seg[i][1] = max(seg[i*2+1][1], seg[i*2+2][1]);
}
void update(int i, int l, int r, int x, int val) {
if(l==r) {
seg[i][0] = seg[i][1] = val;
return;
}
int m = (l+r)/2;
if(x<=m) update(i*2+1, l, m, x, val);
else update(i*2+2, m+1, r, x, val);
seg[i][0] = min(seg[i*2+1][0], seg[i*2+2][0]);
seg[i][1] = max(seg[i*2+1][1], seg[i*2+2][1]);
}
int query(int i, int l, int r, int wl, int wr, int j) {
if(wl<=l && wr>=r) return seg[i][j];
if(wl>r || wr<l) return (j==0 ? 2e9 : -2e9);
int m = (l+r)/2;
return (j==0 ? min(query(i*2+1, l, m, wl, wr, j), query(i*2+2, m+1, r, wl, wr, j)) : max(query(i*2+1, l, m, wl, wr, j), query(i*2+2, m+1, r, wl, wr, j)));
}
int queryh(int i, int l, int r, int wl, int wr, int bound) {
if(wl>r || wr<l || seg[i][0]>bound) return -2e9;
if(wl<=l && wr>=r && seg[i][1]<bound) return seg[i][1];
int m = (l+r)/2;
return max(queryh(i*2+1, l, m, wl, wr, bound), queryh(i*2+2, m+1, r, wl, wr, bound));
}
}posi, hi;
int tol[200005], tor[200005], diss[200005];
void dfs(int u, int dis) {
diss[u] = dis;
for(int v:adj[u]) dfs(v, dis+1);
}
vector<pii> v;
void init(int N, std::vector<int> H) {
n = N;
h = H;
for(int i=0;i<n;i++) v.push_back({h[i], i});
sort(v.begin(), v.end());
posi.build(0, 0, n-1, 1);
for(int i=n-1;i>=0;i--) {
tol[v[i].second] = posi.query(0, 0, n-1, 0, v[i].second-1, 1);
tor[v[i].second] = posi.query(0, 0, n-1, v[i].second+1, n-1, 0);
if(tor[v[i].second]==2e9) tor[v[i].second] = n;
adj[tor[v[i].second]].push_back(v[i].second);
posi.update(0, 0, n-1, v[i].second, v[i].second);
}
hi.build(0, 0, n-1, 2);
dfs(n, 0);
}
int minimum_jumps(int A, int B, int C, int D) {
A = hi.queryh(0, 0, n-1, A, B, h[C]);
if(A==-2e9) return -1;
A = v[A-1].second;
int ret = diss[A]-diss[C];
int ct = 1;
while(tol[A]>-2e9 && h[tol[A]]<h[C] && ct+1<ret) {
A = tol[A];
ret = min(ret, diss[A]-diss[C]+ct);
ct++;
}
return ret;
}
# | 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... |