This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
#include "jumps.h"
using namespace std;
typedef int ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF32 = 2'000'000'000, MOD = 1'000'000'007, LOG = 20;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
vector<vector<ll>> bul, bur, bub;
void dfs(ll v, vector<vector<ll>>& bu, vector<ll>& b) {
if (!bu[v].empty()) {
return;
}
bu[v].resize(LOG);
bu[v][0] = b[v];
dfs(b[v], bu, b);
for (ll i = 1; i < LOG; i++) {
bu[v][i] = bu[bu[v][i - 1]][i - 1];
}
}
void init(ll n, vector<ll> h) {
vector<ll> lb(n, -1), rb(n, -1);
vector<ll> now;
for (ll i = 0; i < n; i++) {
lb[i] = i;
rb[i] = i;
while (!now.empty() && h[now.back()] <= h[i]) {
rb[now.back()] = i;
now.pop_back();
}
if (!now.empty()) {
lb[i] = now.back();
}
now.push_back(i);
}
vector<ll> bb(n);
for (ll i = 0; i < n; i++) {
bb[i] = (h[lb[i]] > h[rb[i]] ? lb[i] : rb[i]);
}
bul.resize(n);
bur.resize(n);
bub.resize(n);
for (ll i = 0; i < n; i++) {
dfs(i, bul, lb);
dfs(i, bur, rb);
dfs(i, bub, bb);
}
}
ll minimum_jumps(ll a, ll b, ll c, ll d) {
ll now = b;
if (bur[now][0] == now || bur[now][0] > d) {
return -1;
}
for (ll i = LOG - 1; i >= 0; i--) {
ll nn = bul[now][i];
if (nn >= a && bur[nn][0] != nn && bur[nn][0] <= d) {
now = nn;
}
}
if (bur[now][0] >= c) {
return 1;
}
ll ans = 0;
for (ll i = LOG - 1; i >= 0; i--) {
ll nn = bub[now][i];
if (bur[nn][0] != nn && bur[nn][0] < c) {
ans += (1 << i);
now = nn;
}
}
ll nn = bub[now][0];
if (bur[nn][0] != nn && bur[nn][0] <= d) {
ans++;
now = nn;
}
for (ll i = LOG - 1; i >= 0; i--) {
ll nn = bur[now][i];
if (nn < c) {
now = nn;
ans += (1 << i);
}
}
if (true) {
nn = bur[now][0];
if (nn < c || nn > d) {
return -1;
}
ans++;
}
return ans;
}
#ifdef LOCAL
int main() {
int N, Q;
assert(2 == scanf("%d %d", &N, &Q));
std::vector<int> H(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &H[i]));
}
init(N, H);
for (int i = 0; i < Q; ++i) {
int A, B, C, D;
assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
printf("%d\n", minimum_jumps(A, B, C, D));
}
return 0;
}
#endif
# | 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... |