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>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e5+10;
int n, h[maxn], nx[maxn][25], nxl[maxn][25], nxr[maxn][25];
pair<int,int> tr[22*maxn];
int f1[22*maxn], f2[22*maxn], bg[maxn], cnt = 0;
int att(int no, int l, int r, int no1, int pos, int val) {
if(no == 0) no = ++cnt;
if(l > pos || r < pos) return no;
if(l == r) {
tr[no] = mp(val,l);
return no;
}
int mid = (l+r)/2;
if(pos <= mid) {
//vai para esquerda
f1[no] = att(f1[no],l,mid,f1[no1],pos,val);
f2[no] = f2[no1];
}
else {
f2[no] = att(f2[no],mid+1,r,f2[no1],pos,val);
f1[no] = f1[no1];
}
tr[no] = max(tr[f1[no]],tr[f2[no]]);
return no;
}
pair<int,int> qrrmx(int no, int l, int r, int L, int R) {
if(l > R || r < L) return mp(0,0);
if(l >= L && r <= R) return tr[no];
int mid = (l+r)/2;
return max(qrrmx(f1[no],l,mid,L,R),qrrmx(f2[no],mid+1,r,L,R));
}
void init(int N, vector<int> H) {
n = N;
vector<pair<int,int>> vec;
for(int i = 1; i <= n; i++) {
h[i] = H[i-1];
vec.pb(mp(h[i],i));
}
// cout << cnt << endl;
//os proximos da esquerda e direita de cada um e 0 se não tiver
stack<pair<int,int>> stl;
for(int i = 1; i <= n; i++) {
while(stl.size() && stl.top().fr < h[i]) {
stl.pop();
}
if(stl.size()) nxl[i][0] = stl.top().sc;
else nxl[i][0] = 0;
stl.push(mp(h[i],i));
}
stack<pair<int,int>> str;
for(int i = n; i >= 1; i--) {
while(str.size() && str.top().fr < h[i]) {
str.pop();
}
if(str.size()) nxr[i][0] = str.top().sc;
else nxr[i][0] = 0;
str.push(mp(h[i],i));
}
}
int minimum_jumps(int A, int B, int C, int D) {
A++;
B++;
C++;
D++;
int d[n+10];
queue<int> q;
for(int i = 1; i <= n; i++) d[i] = -1;
for(int i = A; i <= B; i++) {
d[i] = 0;
q.push(i);
}
while(q.size()) {
int u = q.front();
q.pop();
int v;
v = nxl[u][0];
if(v != 0 && d[v] == -1) {
d[v] = d[u]+1;
q.push(v);
}
v = nxr[u][0];
if(v != 0 && d[v] == -1) {
d[v] = d[u]+1;
q.push(v);
}
}
int ans = n+10;
for(int i = C; i <= D; i++) {
if(d[i] != -1) ans = min(ans, d[i]);
}
if(ans == n+10) return -1;
return ans;
}
# | 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... |