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:

Considering doubling, we have the formula:

f^{2^k}(l, r) = f^{2^{k - 1}}(f^{2^{k - 1}}(l, r))

For each 0 \le k \le \log_2 n, f^{2^k}(l, r) can obviously be processed using an ST Table.

However, this isn't rigorous enough since we don't know if f^{2^k}(l, r) is combinable.

Lemma: f^{2^k}(l, r) \cup f^{2^k}(L, R) = f^{2^k}((l, r) \cup (L, R)) ((l, r) \cap (L, R) \neq \emptyset, (l, r) \neq (L, R), k \ge 0)

Proof:

By mathematical induction.

Consider the base case, where k = 0, then 2^k = 1, we need to prove:

f(l, r) \cup f(L, R) = f((l, r) \cup (L, R))

Expanding the left side, we get:

f(l, r) \cup f(L, R) = (\min(\min_{i = l} ^ r a_i, \min_{i = L} ^ R a_i), \max(\max_{i = l} ^ r a_i, \max_{i = L} ^ R a_i))

Simplifying further, we get:

\begin{aligned} f(l, r) \cup f(L, R) &= (\min(\min_{i = l} ^ r a_i, \min_{i = L} ^ R a_i), \max(\max_{i = l} ^ r a_i, \max_{i = L} ^ R a_i)) \\ &= (\min_{i = \min(l, L)}^{\max(r, R)}a_i, \max_{i = \min(l, L)} ^ {\max(r, R)} a_i) \\ &= f(\min(l, L), \max(r, R)) \\ &= f((l, r) \cup (L, R)) \end{aligned}

Thus, the base case holds.

Next, assume the lemma holds for some k \ge 0, then we need to prove it holds for k + 1.

According to the induction hypothesis:

f^{2^k}((l, r) \cup (L, R)) = f^{2^k}(l, r) \cup f^{2^k}(L, R)

Applying 2^k to both sides:

f^{2^k}(f^{2^k}((l, r) \cup (L, R))) = f^{2^k}(f^{2^k}(l, r)) \cup f^{2^k}(f^{2^k}(L, R))

Simplifying:

f^{2^{k + 1}}((l, r) \cup (L, R)) = f^{2^{k + 1}}(l, r) \cup f^{2^{k + 1}}(L, R)

Thus, the equation holds.

Proof Complete.

For each query, we first calculate the number of steps to jump to the final target. Then, from \log_2 n to 0, we enumerate k in descending order. If f^{2^k}(l, r) \neq (1, n), we add 2^k to the answer. The final answer is this number of steps plus 1.

One might ask: Why enumerate jumps from largest to smallest? What if we encounter (1, n) during the jumps?

This is easily explained. First, we find that for an interval, there might be a solution if and only if (l, r) = (1, n) or the sequence contains both 1 and n. The first case can be ignored; for the second case, it's clear that f(1, n) = (1, n), so we won't miss it.

Preprocessing time complexity is O(n\log^2 n), and each query's time complexity is O(\log n).

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.