[总结|题解] P10589 楼兰图腾

· · 题解

:::epigraph[——某位在楼兰古城被三元组枚举卡到 TLE 的 OIer] "我枚举了 i,枚举了 j,枚举了 k,然后发现 n=2\times 10^5 的时候,我的程序跑得比壁画风化还慢。" :::

前置知识

建议先阅读 树状数组基础(打不开点我)和 值域树状数组(打不开点我)。本文默认你已理解 lowbit、add、sum 的跳步原理。

12345的渗透学习法是我从B站视频《一个金牌选手的信竞攻略》学来的,建议大家也去看一下

① 叫什么

楼兰图腾(Luogu P10589),标准学名是排列上中间极值三元组计数。

对 1\sim n 的一个排列 y,统计满足 i<j<k 的两种形状:

\begin{array}{c} \vee\text{型(谷点):} y_i > y_j < y_k \\[6pt] \wedge\text{型(峰点):} y_i < y_j > y_k \end{array}

② 地位

树状数组维护前缀序关系的入门巅峰题。它用一道题讲透了一个核心思想:

固定中间点,两侧分别统计,乘法原理合成。

这一思想直接延伸到三元组计数、排列逆序对变种、笛卡尔树统计等一系列问题。搞懂它,你才算真正会用树状数组做 "序关系" 而不仅是 "区间和"。

③ 用途

::::info[典型应用场景]

④ 核心原理

把三元组 (i,j,k) 看成"先固定中间点 j,再选左右两个跟班",问题立刻降维:

:::align{center}

\begin{array}{c|c|c} \text{中间点 } j & \text{左侧 } i\in[1,j-1] & \text{右侧 } k\in[j+1,n] \\ \hline \vee\text{型要求} & y_i > y_j & y_k > y_j \\ \wedge\text{型要求} & y_i < y_j & y_k < y_j \\ \end{array}

:::

关键洞察:左侧信息用树状数组维护,右侧信息利用排列的完备性直接算。

⑤ 具体原理

5.1 问题转化:从 O(n^3) 枚举到 O(n) 中间点

暴力枚举三元组是 O(n^3),显然不行。固定中间点 j 后:

\boxed{ \begin{array}{c} \vee\text{型个数} = \displaystyle\sum_{j=1}^{n} \text{左大}(j) \times \text{右大}(j) \\[12pt] \wedge\text{型个数} = \displaystyle\sum_{j=1}^{n} \text{左小}(j) \times \text{右小}(j) \end{array} }

对每个 j 求出四个数:左大、右大、左小、右小。

5.2 左侧信息:树状数组维护

从左到右扫描,树状数组里存的是已经经过的值(即 j 的左侧):

\text{less} = \text{bit.query}(y_j - 1) = \text{左侧比 }y_j\text{ 小的个数} \text{greater\_left} = (j-1) - \text{less} = \text{左侧比 }y_j\text{ 大的个数}
树状数组状态(扫描到 j 之前) 含义
\text{bit.query}(y_j-1) 左侧值域 [1, y_j-1] 的出现次数
(j-1) - \text{上述值} 左侧值域 [y_j+1, n] 的出现次数

5.3 右侧信息:排列性质的妙用之所在

这是全题最精彩的一步,也是不需要从右往左再扫一遍的原因。

因为 y 是 1\sim n 的排列:

所以:

\text{less\_right} = (y_j - 1) - \text{less} \text{greater\_right} = (n - y_j) - \text{greater\_left}

:::align{center}

\begin{array}{c|c|c|c} \text{类别} & \text{值域总数} & \text{左侧已有} & \text{右侧剩余} \\ \hline \text{比 }y_j\text{ 小} & y_j - 1 & \color{blue}{\text{less}} & \color{red}{(y_j-1) - \text{less}} \\ \text{比 }y_j\text{ 大} & n - y_j & \color{blue}{\text{greater\_left}} & \color{red}{(n-y_j) - \text{greater\_left}} \\ \end{array}

:::

右侧不需要树状数组,减法直接出答案。 这是排列题独有的优雅。

5.4 乘法原理:\vee 型与 \wedge 型的合成

:::align{center}

\boxed{ \begin{array}{c} \vee\text{(谷点)} = \text{左大} \times \text{右大} \\[8pt] \wedge\text{(峰点)} = \text{左小} \times \text{右小} \end{array} }

:::

5.5 逐步推演样例

输入:5 / 1 5 3 2 4

::cute-table{tuack}

步骤 j y_j \text{less} \text{greater\_left} \text{less\_right} \text{greater\_right} \vee 增量 \wedge 增量
1 1 0 0 0 4 0 0
2 5 1 0 3 0 0 3
3 3 1 1 1 1 1 1
4 2 1 2 0 1 2 0
5 4 3 1 0 0 0 0
\vee = 0+0+1+2+0 = \boxed{3}, \quad \wedge = 0+3+1+0+0 = \boxed{4}

5.6 代码分段实现

第一段:头文件与类型定义

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;           // 答案可能爆 int,先 typedef 好

bits/stdc++.h 万能头,typedef long long ll 是 OI 标配。答案最大 C_n^3 \approx 1.3\times 10^{15},必须 long long。

第二段:树状数组模板

struct Fenwick {
    int n;                      // 值域上限,本题直接等于 n(排列)
    vector<int> t;              // 树状数组本体,t[i] 表示排名 i 的出现次数

    Fenwick(int n = 0) { init(n); }     // 构造函数:创建时自动 init

    void init(int n_) {                 // 初始化:开 n+1 空间
        n = n_;
        t.assign(n + 1, 0);             // n+1 个 0,t[0] 闲置,有效范围 1..n
    }

    // 单点修改:在位置 x 增加 v
    // 跳步:x += x & -x,即加上 lowbit,往祖先爬
    void add(int x, int v) {
        for (; x <= n; x += x & -x) 
            t[x] += v;
    }

    // 前缀查询:查询值域 [1..x] 的出现次数总和
    // 跳步:x -= x & -x,即减去 lowbit,往左下削
    int query(int x) {
        int res = 0;
        for (; x > 0; x -= x & -x) 
            res += t[x];
        return res;
    }
};

为什么值域直接开 n? 因为 y 是 1\sim n 的排列,不需要离散化。query(y[j]-1) 直接查"比 y_j 小的值在左侧出现了多少次"。

第三段:主函数 —— 输入

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);           // 解除同步 + 解除 tie,加速输入输出

    int n;
    if (!(cin >> n)) return 0; // 处理空输入,防 RE

    vector<int> y(n + 1);       // 下标从 1 开始,y[1..n]
    for (int i = 1; i <= n; i++) 
        cin >> y[i];

    Fenwick bit(n);             // 值域就是 1..n,无需离散化
    ll v = 0, a = 0;            // v: ∨ 型个数, a: ∧ 型个数

y(n+1) 是为了让下标从 1 开始,和题目坐标 (1,y_1),\dots,(n,y_n) 对齐。bit(n) 直接以 n 为值域上限。

第四段:核心扫描循环(灵魂所在)

    for (int j = 1; j <= n; j++) {

        // -------- 左侧信息(树状数组已维护)--------
        int less = bit.query(y[j] - 1);     
        // 值域 [1, y[j]-1] 在左侧出现了多少次
        // 即:左侧比 y[j] 小的元素个数

        int greater_left = (j - 1) - less;
        // j-1 是左侧总元素数
        // 左侧要么小要么大,所以 "大" = 总数 - "小"

        // -------- 右侧信息(排列性质直接算)--------
        int less_right = (y[j] - 1) - less;
        // 值域中比 y[j] 小的总共有 y[j]-1 个
        // 减去左侧已经出现的 less,剩下的全在右侧

        int greater_right = (n - y[j]) - greater_left;
        // 值域中比 y[j] 大的总共有 n-y[j] 个
        // 减去左侧已经出现的 greater_left,剩下的全在右侧

        // -------- 乘法原理累加 --------
        v += 1LL * greater_left * greater_right;
        // ∨ 型:左大 × 右大
        // 1LL 强制转 long long,防中间结果溢出

        a += 1LL * less * less_right;
        // ∧ 型:左小 × 右小

        // -------- 把 y[j] 加入左侧集合 --------
        bit.add(y[j], 1);
        // 当前 j 成为后续点的"左侧历史"
    }

四行计算,一行更新,整个 O(n^3) 的枚举被压成了 O(n\log n)。

第五段:输出

    cout << v << ' ' << a << '\n';
    return 0;  // 完结散花!
}

5.7 关键变量对照表

::cute-table{tuack}

变量名 类型 含义 计算方式
less int 左侧比 y_j 小的个数 bit.query(y[j]-1)
greater_left int 左侧比 y_j 大的个数 (j-1) - less
less_right int 右侧比 y_j 小的个数 (y[j]-1) - less
greater_right int 右侧比 y_j 大的个数 (n-y[j]) - greater_left
v ll \vee 型累计个数 += greater_left * greater_right
a ll \wedge 型累计个数 += less * less_right

5.8 边界条件验证

场景 验证 结果
j=1(最左端) less=0, greater_left=0 两侧至少有一侧为空,贡献为 0,正确
j=n(最右端) less_right=0 或 greater_right=0 同上,贡献为 0,正确
y_j=1(最小值) less=0, less_right=0 \wedge 型贡献为 0(不可能有比 1 更小的),正确
y_j=n(最大值) greater_left=0, greater_right=0 \vee 型贡献为 0(不可能有比 n 更大的),正确
全递增排列 只有 \wedge 型 每个中间点都是峰,验证通过
全递减排列 只有 \vee 型 每个中间点都是谷,验证通过

5.9 完整代码

::::success[AC 代码(核心逻辑高亮)]{open}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Fenwick {
    int n;
    vector<int> t;
    Fenwick(int n = 0) { init(n); }
    void init(int n_) {
        n = n_;
        t.assign(n + 1, 0);
    }
    void add(int x, int v) {
        for (; x <= n; x += x & -x) t[x] += v;
    }
    int query(int x) {
        int res = 0;
        for (; x > 0; x -= x & -x) res += t[x];
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    if (!(cin >> n)) return 0;
    vector<int> y(n + 1);
    for (int i = 1; i <= n; i++) cin >> y[i];

    Fenwick bit(n);
    ll v = 0, a = 0;

    for (int j = 1; j <= n; j++) {
        int less = bit.query(y[j] - 1);
        int greater_left = (j - 1) - less;

        int less_right = (y[j] - 1) - less;
        int greater_right = (n - y[j]) - greater_left;

        v += 1LL * greater_left * greater_right;
        a += 1LL * less * less_right;

        bit.add(y[j], 1);
    }

    cout << v << ' ' << a << '\n';
    return 0;
}

::::

复杂度总览

::cute-table{tuack}

指标 复杂度 说明
时间 O(n \log n) 单次 query + add 为 O(\log n),共 n 次
空间 O(n) 树状数组 + 原数组
常数 极小 单 BIT,无离散化,无递归,纯循环

一句话总结

固定中间点,左侧 BIT 查序关系,右侧排列性质直接算,乘法原理合成答案。

这道题的灵魂不是树状数组,而是 "排列的完备性" ——它让你省掉了第二棵树状数组,把一道看似需要扫描两遍的题,优雅地压缩到了一遍线性扫描。