#include "jumps.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(7069);
/* -------------------- Typedefs -------------------- */
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
/* -------------------- Usings -------------------- */
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
/* -------------------- Defines -------------------- */
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);
/* -------------------- Constants -------------------- */
const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(2e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);
const int imp = -1;
const int max_N = 200005, M = 30;
int a[max_N], dp_left[max_N], dp_right[max_N], up_right[max_N][M], st[max_N][M];
pii up[max_N][M];
int n, lg;
int qry(int l, int r) {
int lgx = log2(r - l + 1);
return max(st[l][lgx], st[r - (1 << lgx) + 1][lgx]);
}
void init(int N, std::vector<int> H) {
n = N;
lg = log2(n);
a[0] = a[n + 1] = MAX;
for (int lgx = 0; lgx <= lg; ++lgx) {
up_right[n + 1][lgx] = n + 1;
up[0][lgx] = up[n + 1][lgx] = { MAX,n + 1 };
}
vector<pii> v;
for (int i = 1; i <= n; ++i) {
a[i] = H[i - 1];
v.pub({ -a[i],i });
st[i][0] = a[i];
int ind = i - 1;
while (ind) {
if (a[ind] > a[i]) break;
ind = dp_left[ind];
}
dp_left[i] = ind;
}
for (int i = n; i >= 1; --i) {
int ind = i + 1;
while (ind <= n) {
if (a[ind] > a[i]) break;
ind = dp_right[ind];
}
dp_right[i] = ind;
up_right[i][0] = ind;
for (int lgx = 1; lgx <= lg; ++lgx) {
up_right[i][lgx] = up_right[up_right[i][lgx - 1]][lgx - 1];
}
}
for (int lgx = 1; lgx <= lg; ++lgx) {
for (int i = 1; i + (1 << lgx) - 1 <= n; ++i) {
st[i][lgx] = max(st[i][lgx - 1], st[i + (1 << (lgx - 1))][lgx - 1]);
}
}
sort(all(v));
for (auto opt : v) {
int ind = opt.ss;
int nxt = dp_right[ind];
if (a[dp_left[ind]] > a[dp_right[ind]]) {
nxt = dp_left[ind];
}
up[ind][0] = { a[nxt],nxt };
for (int lgx = 1; lgx <= lg; ++lgx) {
up[ind][lgx] = up[up[ind][lgx - 1].ss][lgx - 1];
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A, ++B, ++C, ++D;
int max_to = qry(C, D);
int node = B;
while (1) {
if (dp_left[node] < A) break;
if (a[dp_left[node]] < max_to) {
node = dp_left[node];
}
else {
break;
}
}
if (a[node] >= max_to) {
return imp;
}
int ans = 1, cur_node = node;
for (int lgx = lg; lgx >= 0; --lgx) {
int nxt = up_right[cur_node][lgx];
if (nxt < C) {
cur_node = nxt;
ans += (1 << lgx);
}
}
if (dp_right[cur_node] > D) return imp;
cur_node = dp_right[cur_node];
int b_mark = a[cur_node];
cur_node = node;
int answ = 1;
for (int lgx = lg; lgx >= 0; --lgx) {
int val = up[cur_node][lgx].ff;
int nxt = up[cur_node][lgx].ss;
if (val < b_mark) {
cur_node = nxt;
answ += (1 << lgx);
}
}
if (dp_right[cur_node] >= C && dp_right[cur_node] <= D) {
ans = min(ans, answ);
}
return ans;
}
/*
10 1000
1 2 3 4 5 6 7 8 9 10
0 6 9 9
*/
/*
7 100
3 2 1 6 4 5 7
0 1 2 2
1 3 5 6
4 4 6 6
*/
# | 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... |