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 <bits/stdc++.h>
#include "jumps.h"
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC target("lzcnt")
#pragma GCC target("popcnt")
using namespace std;
template<typename T> void ono_min(T &MIN, T CMP) { if(MIN > CMP) MIN = CMP; }
template<typename T> void ono_max(T &MAX, T CMP) { if(MAX < CMP) MAX = CMP; }
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
const int mxk = 32;
const int mxn = 2e5 + 10;
const int inf = 1e9 + 10;
int L[mxn], h[mxn];
int R[mxn], n;
vector<int> adj[mxn];
int mx[mxk][mxn], mn[mxk][mxn];
pii ll[mxk][mxn], rr[mxk][mxn];
pii str[mxn * 4];
void build(int id, int tl, int tr) {
if(tl == tr) {
str[id] = MP(h[tl], tl);
return;
}
int tm = (tl + tr) / 2;
build(id * 2 + 1, tl, tm);
build(id * 2 + 2, tm + 1, tr);
str[id] = max(str[id * 2 + 1], str[id * 2 + 2]);
}
pii query(int id, int tl, int tr, int l, int r) {
if(l > r) return MP(inf, -1);
if(tl == l && r == tr) return str[id];
int tm = (tl + tr) / 2;
if(r <= tm) return query(id * 2 + 1, tl, tm, l, r);
if(tm < l) return query(id * 2 + 2, tm + 1, tr, l, r);
pii LQ = query(id * 2 + 1, tl, tm, l, tm);
pii RQ = query(id * 2 + 2, tm + 1, tr, tm + 1, r);
return max(LQ, RQ);
}
void init(int N, vector<int> H) {
memset(L, -1, sizeof L);
memset(R, -1, sizeof R);
n = N;
for(int i = 0; i <= N; i++)
for(int k = 0; k < mxk; k++) {
mn[k][i] = mx[k][i] = 0;
ll[k][i] = rr[k][i] = MP(0, 0);
}
stack<int> st;
for(int i = 0; i < N; i++) {
while(st.size() && H[st.top()] <= H[i])
st.pop();
if(st.size()) L[i] = st.top();
else L[i] = -1;
st.push(i);
}
stack<int> en;
for(int i = N - 1; i >= 0; i--) {
while(en.size() && H[en.top()] <= H[i])
en.pop();
if(en.size()) R[i] = en.top();
else R[i] = -1;
en.push(i);
}
for(int i = 0; i < N; i++) {
h[i] = H[i];
if(L[i] > -1) {
adj[i].push_back(L[i]);
if(mn[0][H[i]] == 0)
mn[0][H[i]] = H[L[i]];
ono_min(mn[0][H[i]], H[L[i]]);
ono_max(mx[0][H[i]], H[L[i]]);
ll[0][i] = MP(H[L[i]], L[i]);
}
if(R[i] > -1) {
adj[i].push_back(R[i]);
if(mn[0][H[i]] == 0)
mn[0][H[i]] = H[R[i]];
ono_min(mn[0][H[i]], H[R[i]]);
ono_max(mx[0][H[i]], H[R[i]]);
rr[0][i] = MP(H[R[i]], R[i]);
}
}
build(0, 0, n - 1);
for(int k = 1; k < mxk; k++)
for(int i = 1; i <= n; i++) {
int mnj = mn[k - 1][i];
int mxj = mx[k - 1][i];
if(mnj) mn[k][i] = mn[k - 1][mnj];
if(mxj) mx[k][i] = mx[k - 1][mxj];
}
for(int k = 1; k < mxk; k++)
for(int i = 0; i < n; i++) {
pii pl = ll[k - 1][i];
pii pr = rr[k - 1][i];
if(pl.ss) ll[k][i] = ll[k - 1][pl.ss];
if(pr.ss) rr[k][i] = rr[k - 1][pr.ss];
}
}
int solve(int s, int f) {
if(s == f) return 0;
int ans = 0;
for(int k = mxk - 1; k >= 0; k--)
if(mx[k][s] && mx[k][s] <= f) {
s = mx[k][s];
ans += (1 << k);
}
for(int k = mxk - 1; k >= 0; k--)
if(mn[k][s] && mn[k][s] <= f) {
s = mn[k][s];
ans += (1 << k);
}
if(s == f) return ans;
return inf;
}
int go_L(int i) { if(i < 0) return -1; return L[i]; }
int go_R(int i) { if(i < 0) return -1; return R[i]; }
pii GIS(int l, int r, int upper, bool rev) {
// if(rev)
// cout << "find " << l << ' ' << r << ": " << upper << endl;
if(r < l) return MP(-1, -1);
int cur = (rev) ? r : l;
if(h[cur] > upper) return MP(-1, cur);
for(int k = mxk - 1; k >= 0; k--) {
pii p = (rev) ? ll[k][cur] : rr[k][cur];
int hi = p.ff;
int id = p.ss;
if(hi == 0) continue;
if(hi > upper) continue;
if(id < l || r < id) continue;
// if(rev) cout << cur << ' ';
cur = id;
}
// if(rev)
// cout << cur << endl;
return (rev) ? MP(cur, go_L(cur)) : MP(cur, go_R(cur));
}
int minimum_jumps(int A, int B, int C, int D) {
pii PM = query(0, 0, n - 1, B + 1, C - 1);
pii PL = query(0, 0, n - 1, A, B);
pii PR = query(0, 0, n - 1, C, D);
if(PM.ss >= 0 && PM.ff > PR.ff) return -1;
auto think = [&](int st, int en, bool ML, bool MR) -> int {
if(st == -1 || en == -1) return inf;
if(ML) if(st < A || B < st) return inf;
if(MR) if(en < C || D < en) return inf;
if(st == en) return 0;
return solve(h[st], h[en]);
};
// cout << endl;
// cout << A << ' ' << B << " -> " << C << ' ' << D << endl;
int gay = (PM.ff == inf) ? 0 : PM.ff;
auto find_en = [&](int st) -> int {
if(st == -1) return -1;
int en = GIS(C, D, max(gay, h[st]), 0).ss;
return en;
};
pii tmp = GIS(A, B, PM.ff, 1);
int st1 = tmp.ff;
int st2 = tmp.ss;
int st3 = B;
int st4 = PL.ss;
int en1 = find_en(st1);
int en2 = find_en(st2);
int en3 = find_en(st3);
int en4 = find_en(st4);
int ans1 = think(st1, en1, 1, 1);
int ans2 = think(st2, en2, 1, 1);
int ans3 = think(st3, en3, 1, 1);
int ans4 = think(st4, en4, 1, 1);
// cout << st1 << ' ' << en1 << " = " << ans1 << endl;
// cout << st2 << ' ' << en2 << " = " << ans2 << endl;
// cout << st3 << ' ' << en3 << " = " << ans3 << endl;
// cout << st4 << ' ' << en4 << " = " << ans4 << endl;
int sda = inf;
if(PM.ff > PL.ff) {
int st = PL.ss;
int mid = GIS(0, A - 1, PM.ff, 1).ss;
int en = find_en(mid);
sda = think(st, mid, 1, 0) + think(mid, en, 0, 1);
// cout << st << ' ' << mid << ' ' << en << " = " << sda << endl;
}
int ans = inf;
ono_min(ans, ans1);
ono_min(ans, ans2);
ono_min(ans, ans3);
ono_min(ans, ans4);
ono_min(ans, sda);
return (ans < inf) ? ans : -1;
}
// 7 4 1 2 5 3 6 9 8
// 9 8 7 6 5 4 3 2 1
# | 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... |