# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
986557 | ZHIRDILBILDIZ | Rainforest Jumps (APIO21_jumps) | C++14 | 0 ms | 0 KiB |
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"
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int pw[N];
int rg[N][20];
int lf[N][20];
int mx[N][20];
int path[N][20];
vector<int> H;
void build_sparce (vector<int> h) {
for (int i = 2; i <= h.size(); ++i) pw[i] = pw[i >> 1] + 1;
for (int i = 0; i < h.size(); ++i) mx[i][0] = h[i];
for (int i = 1; i < 20; ++i)
for (int j = 0; j <= (int)h.size() - (1 << i); ++j)
mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]);
}
int get_max (int l, int r) {
if (l > r) swap(l, r);
int x = pw[r - l + 1];
return max(mx[l][x], mx[r - (1 << x) + 1][x]);
}
void init (int n, vector<int> h) {
H = h;
build_sparce(h);
for (int i = 0; i < n; ++i) {
int l = i, r = n;
lf[i][0] = rg[i][0] = i;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (get_max(i, mid) == h[i])
l = mid;
else
r = mid;
}
if (r != n)
rg[i][0] = r;
l = -1, r = i;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (get_max(mid, i) == h[i])
r = mid;
else
l = mid;
}
if (l != -1)
lf[i][0] = l;
if (h[lf[i][0]] > h[rg[i][0]])
path[i][0] = lf[i][0];
else
path[i][0] = rg[i][0];
}
for (int i = 1; i < 20; ++i)
for (int j = 0; j < n; ++j) {
lf[j][i] = lf[lf[j][i - 1]][i - 1];
rg[j][i] = rg[rg[j][i - 1]][i - 1];
path[j][i] = path[path[j][i - 1]][i - 1];
}
}
int minimum_jumps(int a, int b, int c, int d) {
int vall = get_max(c, d);
int vall1 = get_max(b, c - 1);
for (int i = 19; i >= 0; --i)
if (a <= lf[b][i] && H[lf[b][i]] <= vall)
b = lf[b][i];
int kol = 0;
for (int i = 19; i >= 0; --i)
if (H[path[b][i]] <= vall1) {
b = path[b][i];
kol += (1 << i);
}
if (c <= rg[b][0] && rg[b][0] <= d)
return kol + 1;
if (v[path[b][0]] < mx)
b = path[b][0], ++kol;
for (int i = 19; i >= 0; --i)
if (rg[b][i] < c) {
b = rg[b][i];
kol += (1 << i);
}
if (c <= rg[b][0] && rg[b][0] <= d)
return kol + 1;
return -1;
}
// signed main () {
// // ios_base::sync_with_stdio(0);
// // cin.tie(0), cout.tie(0);
// int n;
// cin >> n;
// vector<int> h(n);
// for (int i = 0; i < n; ++i) cin >> h[i];
// init(n, h);
// int q;
// cin >> q;
// while (q--) {
// int a, b, c, d;
// cin >> a >> b >> c >> d;
// cout << minimum_jumps(a, b, c, d) << '\n';
// }
// return 0;
// }