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];
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) {
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) {
for (int i = 19; i >= 0; --i)
if (a <= lf[b][i] && rg[lf[b][i]][0] <= d)
b = lf[b][i];
int kol = 0;
for (int i = 19; i >= 0; --i)
if (path[b][i] < c && rg[path[b][i]][0] < c) {
// cout << "path["<<b<<"]["<<i<<"] = " << path[b][i] << '\n';
b = path[b][i];
kol += (1 << i);
}
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;
// }
Compilation message (stderr)
jumps.cpp: In function 'void build_sparce(std::vector<int>)':
jumps.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i = 2; i <= h.size(); ++i) pw[i] = pw[i >> 1] + 1;
| ~~^~~~~~~~~~~
jumps.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < h.size(); ++i) mx[i][0] = h[i];
| ~~^~~~~~~~~~
# | 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... |