跳到正文

个人记录

Replace

lkrqwq约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月10日
篇幅
约 11 分钟

版本

  1. v1当前

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:

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

For each 0≤k≤log⁡2n0 \le k \le \log_2 n, f2k(l,r)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 f2k(l,r)f^{2^k}(l, r) is combinable.

Lemma: f2k(l,r)∪f2k(L,R)=f2k((l,r)∪(L,R))((l,r)∩(L,R)≠∅,(l,r)≠(L,R),k≥0)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=0k = 0, then 2k=12^k = 1, we need to prove:

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

Expanding the left side, we get:

f(l,r)∪f(L,R)=(min⁡(min⁡i=lrai,min⁡i=LRai),max⁡(max⁡i=lrai,max⁡i=LRai))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:

f(l,r)∪f(L,R)=(min⁡(min⁡i=lrai,min⁡i=LRai),max⁡(max⁡i=lrai,max⁡i=LRai))=(min⁡i=min⁡(l,L)max⁡(r,R)ai,max⁡i=min⁡(l,L)max⁡(r,R)ai)=f(min⁡(l,L),max⁡(r,R))=f((l,r)∪(L,R))\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≥0k \ge 0, then we need to prove it holds for k+1k + 1.

According to the induction hypothesis:

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

Applying 2k2^k to both sides:

f2k(f2k((l,r)∪(L,R)))=f2k(f2k(l,r))∪f2k(f2k(L,R))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:

f2k+1((l,r)∪(L,R))=f2k+1(l,r)∪f2k+1(L,R)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⁡2n\log_2 n to 00, we enumerate kk in descending order. If f2k(l,r)≠(1,n)f^{2^k}(l, r) \neq (1, n), we add 2k2^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)(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)(l, r) = (1, n) or the sequence contains both 11 and nn. The first case can be ignored; for the second case, it’s clear that f(1,n)=(1,n)f(1, n) = (1, n), so we won’t miss it.

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

Here’s the reference code:

C++
#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.

原文