跳到正文

题解

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

djhcbhgs44约 17 分钟
原文
文章信息与版本

文章信息

发布
2026年8月9日
收录
2026年8月9日
更新
2026年11月17日
篇幅
约 17 分钟
题目
P10589

版本

  1. v2当前
  2. v1

“我枚举了 ii,枚举了 jj,枚举了 kk,然后发现 n=2×105n=2\times 10^5 的时候,我的程序跑得比壁画风化还慢。”

——某位在楼兰古城被三元组枚举卡到 TLE 的 OIer

前置知识

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


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

① 叫什么

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

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

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

② 地位

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

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

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


③ 用途

典型应用场景
  • ∨\vee 型计数:中间点是”谷底”,左右都比它高
  • ∧\wedge 型计数:中间点是”峰顶”,左右都比它低
  • 排列性质的神来之笔:因为 yy 是 1∼n1\sim n 的排列,右侧信息不需要第二棵树状数组,直接通过值域总数减去左侧得到

④ 核心原理

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

中间点 j左侧 i∈[1,j−1]右侧 k∈[j+1,n]∨型要求yi>yjyk>yj∧型要求yi<yjyk<yj\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(n3)O(n^3) 枚举到 O(n)O(n) 中间点

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

∨型个数=∑j=1n左大(j)×右大(j)∧型个数=∑j=1n左小(j)×右小(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} }

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


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

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

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

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

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

因为 yy 是 1∼n1\sim n 的排列:

  • 值域中比 yjy_j 小的共有 yj−1y_j - 1 个
  • 值域中比 yjy_j 大的共有 n−yjn - y_j 个

所以:

less_right=(yj−1)−less\text{less\_right} = (y_j - 1) - \text{less} greater_right=(n−yj)−greater_left\text{greater\_right} = (n - y_j) - \text{greater\_left}
类别值域总数左侧已有右侧剩余比 yj 小yj−1less(yj−1)−less比 yj 大n−yjgreater_left(n−yj)−greater_left\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 型的合成

∨(谷点)=左大×右大∧(峰点)=左小×右小\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

步骤 jjyjy_jless\text{less}greater_left\text{greater\_left}less_right\text{less\_right}greater_right\text{greater\_right}∨\vee 增量∧\wedge 增量
11000400
25103003
33111111
42120120
54310000
∨=0+0+1+2+0=3,∧=0+3+1+0+0=4\vee = 0+0+1+2+0 = \boxed{3}, \quad \wedge = 0+3+1+0+0 = \boxed{4}

5.6 代码分段实现

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

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

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

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


第二段:树状数组模板

C++
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? 因为 yy 是 1∼n1\sim n 的排列,不需要离散化。query(y[j]-1) 直接查”比 yjy_j 小的值在左侧出现了多少次”。


第三段:主函数 —— 输入

C++
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,y1),…,(n,yn)(1,y_1),\dots,(n,y_n) 对齐。bit(n) 直接以 nn 为值域上限。


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

C++
    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(n3)O(n^3) 的枚举被压成了 O(nlog⁡n)O(n\log n)。


第五段:输出

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

5.7 关键变量对照表

变量名类型含义计算方式
lessint左侧比 yjy_j 小的个数bit.query(y[j]-1)
greater_leftint左侧比 yjy_j 大的个数(j-1) - less
less_rightint右侧比 yjy_j 小的个数(y[j]-1) - less
greater_rightint右侧比 yjy_j 大的个数(n-y[j]) - greater_left
vll∨\vee 型累计个数+= greater_left * greater_right
all∧\wedge 型累计个数+= less * less_right

5.8 边界条件验证

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

5.9 完整代码

AC 代码(核心逻辑高亮)
C++
#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;
}

复杂度总览

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

一句话总结

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

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

原文