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, l[200005], r[200005], jump[200005][20], jump2[200005][20];
vector<int> h;
bool one;
struct segTree {
pii seg[4*200005];
void build(int i, int l, int r) {
if(l==r) {
seg[i] = {h[l], l};
return;
}
int m = (l+r)/2;
build(i*2+1, l, m);
build(i*2+2, m+1, r);
seg[i] = max(seg[i*2+1], seg[i*2+2]);
}
pii query(int i, int l, int r, int wl, int wr) {
if(wl>r || wr<l) return {-2e9, -1};
if(wl<=l && wr>=r) return seg[i];
int m = (l+r)/2;
return max(query(i*2+1, l, m, wl, wr), query(i*2+2, m+1, r, wl, wr));
}
}t;
void init(int N, std::vector<int> H) {
n = N;
h = H;
stack<int> st;
for(int i=0;i<n;i++) {
while(!st.empty() && H[st.top()]<H[i]) st.pop();
if(st.empty()) l[i] = -1;
else l[i] = st.top();
st.push(i);
}
while(!st.empty()) st.pop();
for(int i=n-1;i>=0;i--) {
while(!st.empty() && H[st.top()]<H[i]) st.pop();
if(st.empty()) r[i] = n;
else r[i] = st.top();
st.push(i);
}
t.build(0, 0, n-1);
h.push_back(n+1);
for(int i=0;i<n;i++) {
int mx = l[i], mx2 = r[i];
if(mx==-1) mx = n;
if(h[mx]<h[mx2]) swap(mx, mx2);
jump[i][0] = mx;
jump2[i][0] = mx2;
}
jump[n][0] = jump2[n][0] = n;
for(int j=1;j<20;j++) for(int i=0;i<=n;i++) jump[i][j] = jump[jump[i][j-1]][j-1];
for(int j=1;j<20;j++) for(int i=0;i<=n;i++) jump2[i][j] = jump2[jump2[i][j-1]][j-1];
one = 1;
for(int i=0;i<n;i++) if(H[i]!=i+1) {
one = 0;
break;
}
}
int subtask3(int A, int B, int C, int D) {
vector<int> mindis(n, 2e9);
priority_queue<pii, vector<pii>, greater<pii>> pq;
for(int i=A;i<=B;i++) {
mindis[i] = 0;
pq.push({0, i});
}
while(!pq.empty()) {
auto [dis, u] = pq.top();
pq.pop();
if(mindis[u]!=dis) continue;
if(u>=C && u<=D) return dis;
if(l[u]>=0) {
if(mindis[l[u]]>dis+1) {
mindis[l[u]] = dis+1;
pq.push({dis+1, l[u]});
}
}
if(r[u]<n) {
if(mindis[r[u]]>dis+1) {
mindis[r[u]] = dis+1;
pq.push({dis+1, r[u]});
}
}
}
return -1;
}
int subtask5(int A, int B, int C) {
A = t.query(0, 0, n-1, max(A, l[C]+1), min(B, r[C]-1)).second;
if(A==-1) return -1;
int ans = 0;
for(int i=19;i>=0;i--) if(h[jump[A][i]]<h[C]) {
A = jump[A][i];
ans += (1<<i);
}
if(jump[A][0]==C) return ans+1;
for(int i=19;i>=0;i--) if(h[jump2[A][i]]<h[C]) {
A = jump2[A][i];
ans += (1<<i);
}
if(jump2[A][0]==C) return ans+1;
return -1;
}
int minimum_jumps(int A, int B, int C, int D) {
if(C==D) return subtask5(A, B, C);
if(one) return C-B;
return subtask3(A, B, C, D);
}
Compilation message (stderr)
jumps.cpp: In function 'int subtask3(int, int, int, int)':
jumps.cpp:76:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | auto [dis, u] = pq.top();
| ^
# | 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... |