Replace
English version. You can view the Chinese version here.
What a classic *3500, encountered this problem during NOIP simulation, aced it.
Here's the breakdown of the humor:
- [4] Doubling
- [6] Sparse Table (ST Table)
- [6] Segment Tree
Considering doubling, we have the formula:
For each
However, this isn't rigorous enough since we don't know if
Lemma:
Proof:
By mathematical induction.
Consider the base case, where
k = 0 , then2^k = 1 , we need to prove:
Expanding the left side, we get:
Simplifying further, we get:
Thus, the base case holds.
Next, assume the lemma holds for some
k \ge 0 , then we need to prove it holds fork + 1 .
According to the induction hypothesis:
Applying
Simplifying:
Thus, the equation holds.
Proof Complete.
For each query, we first calculate the number of steps to jump to the final target. Then, from
One might ask: Why enumerate jumps from largest to smallest? What if we encounter
This is easily explained. First, we find that for an interval, there might be a solution if and only if
Preprocessing time complexity is
Here's the reference code:
#include <bits/stdc++.h>
/*
So wonderful problem, let me enjoy it.
Let f(l, r) = (min(al, al+1, ... ar), max(al, al+1, ..., ar))
Then we can precompute f^{2^k}(l, r)
So i can do O(nlog^2n) precompute, qlogn queries.
*/
int stmin[19][19][100005];
int stmax[19][19][100005];
int a[100005];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr), std::cout.tie(nullptr);
// freopen("dilemma.in", "r", stdin);
// freopen("dilemma.out", "w", stdout);
int n, q;
std::cin >> n >> q;
// std::vector<int> a(n + 1, 0);
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
}
int b = (int)log2(n) + 1;
// std::vector<std::vector<std::vector<int>>> stmin(19, std::vector<std::vector<int>>(19, std::vector<int>(n + 1, 0)))
// , stmax(19, std::vector<std::vector<int>>(19, std::vector<int>(n + 1, 0)));
for (int i = 1; i <= n; ++i) {
stmin[0][0][i] = stmax[0][0][i] = a[i];
}
for (int i = 1; i <= b; ++i) {
for (int j = 1; j <= n - (1 << i) + 1; ++j) {
stmin[0][i][j] = std::min(stmin[0][i - 1][j], stmin[0][i - 1][j + (1 << i - 1)]);
stmax[0][i][j] = std::max(stmax[0][i - 1][j], stmax[0][i - 1][j + (1 << i - 1)]);
}
}
auto qmin = [&](int x, int l, int r) -> int { // f^{2^k}(l, r)[0]
int k = 31 - __builtin_clz(r - l + 1);
return std::min(stmin[x][k][l], stmin[x][k][r - (1 << k) + 1]);
};
auto qmax = [&](int x, int l, int r) -> int { // f^{2^k}(l, r)[1]
int k = 31 - __builtin_clz(r - l + 1);
return std::max(stmax[x][k][l], stmax[x][k][r - (1 << k) + 1]);
};
// f^{2^k}(l, r) = f^{2^{k - 1}}(f^{2^{k - 1}}(l, r))
for (int i = 1; i <= b; ++i) {
for (int j = 0; j <= b; ++j) {
for (int k = 1; k <= n - (1 << j) + 1; ++k) {
stmin[i][j][k] = qmin(i - 1, stmin[i - 1][j][k], stmax[i - 1][j][k]);
stmax[i][j][k] = qmax(i - 1, stmin[i - 1][j][k], stmax[i - 1][j][k]);
}
}
}
while (q--) {
int l, r;
std::cin >> l >> r;
if (l == 1 && r == n) {
std::cout << "0\n";
continue;
}
int st = 1; // at least one
for (int i = b; i >= 0; --i) { // jump
int x = qmin(i, l, r);
int y = qmax(i, l, r);
if (!(x == 1 && y == n)) {
l = x, r = y;
st += (1 << i);
}
}
std::cout << (qmin(0, l, r) != 1 || qmax(0, l, r) != n ? -1 : st) << "\n"; // because st = 1, we'll query once
}
}
Easter Egg: NOIP Simulation Phigros Edition.