版本比较
值域树状数组
正文
增加 201 行删除 0 行
138 行未改动全部展开139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349100 行未改动138 行未改动全部展开| 2 | 0 + 2 = 2 | sum(2) = 1 | ✗ $< 4$ | 跳!pos = 2, k -= 1 || 1 | 2 + 1 = 3 | sum(3) = 2 | ✗ $< 4$ | 跳!pos = 3, k -= 2 || 结束 | 3 + 1 = 4 | — | — | 答案 = 4 |
那就把 KaTeX 当成画布来画。
---
### 核心等式
值域 BIT 的全部秘密,可以框进这一行:
$$\boxed{\text{ans}(x) = \sum_{\substack{v \le x}} c_v \quad \Longleftrightarrow \quad \text{query}\Bigl(\text{rank}(x)\Bigr)}$$
---
### 6.1 下标不是位置,是「值」
普通 BIT 的下标是**数组下标**:
$$\begin{array}{|c|c|c|c|c|c|c|c|}\hline\text{下标 } i & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\\hlinea_i & 5 & 3 & 7 & 1 & 4 & 2 & 6 & 8 \\\hline\end{array}$$
值域 BIT 的下标直接是**值本身**(或离散化后的排名):
$$\begin{array}{|c|c|c|c|c|c|c|c|c|}\hline\text{值 } v & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\\hlinec_v\ (\text{出现次数}) & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\\hline\color{blue}\text{tree}[v] & 1 & 2 & 1 & 4 & 1 & 2 & 1 & 8 \\\hline\end{array}$$
> 同一行数字,意义完全不同。左边是**位置序列**,右边是**统计直方图**。
---
### 6.2 $\text{lowbit}$ 的「管辖范围」可视化
每个节点 $i$ 管辖一段长度为 $\text{lowbit}(i)$ 的区间。用**彩色块**标出来:
$$\begin{array}{c|cccccccc}i & \textcolor{red}{1} & \textcolor{teal}{2} & \textcolor{red}{3} & \textcolor{blue}{4} & \textcolor{red}{5} & \textcolor{teal}{6} & \textcolor{red}{7} & \textcolor{purple}{8} \\\hline\text{lowbit}(i) & 1 & 2 & 1 & 4 & 1 & 2 & 1 & 8 \\\hline\text{管辖区间} & [1,1] & [1,2] & [3,3] & [1,4] & [5,5] & [5,6] & [7,7] & [1,8] \\\end{array}$$
| $\textcolor{purple}{tr_8}$ | < | < | < | < | < | < | < || :------------------------: | :-: | :---------------------: | :-: | :----------------------: | :-: | :---------------------: | :-: || $\textcolor{blue}{tr_4}$ | < | < | < | | < | < | < || $\textcolor{teal}{tr_2}$ | < | | < | $\textcolor{teal}{tr_6}$ | < | | < || $\textcolor{red}{tr_1}$ | | $\textcolor{red}{tr_3}$ | | $\textcolor{red}{tr_5}$ | | $\textcolor{red}{tr_7}$ | |
用箭头画一次**前缀查询** $\text{query}(6)$ 的跳跃路径:
$$6 \xrightarrow{-\text{lowbit}(6)=2} 4 \xrightarrow{-\text{lowbit}(4)=4} 0$$
即:
$$\text{query}(6) = \underbrace{\text{tree}[6]}_{[5,6]} + \underbrace{\text{tree}[4]}_{[1,4]} = \sum_{v=1}^{6} c_v$$
---
### 6.3 离散化:把宇宙压进一个数组
原始值域炸到 $10^9$ 没关系,只需要**保序压缩**:
$$\begin{array}{ccc}\text{原始值} & \xrightarrow{\text{排序去重}} & \text{排名} \\\{ 42,\ 10^9,\ 7,\ 42,\ 999 \} & \Longrightarrow & \{ 2,\ 4,\ 1,\ 2,\ 3 \} \\\end{array}$$
严格写:
$$\text{rank}(x) = \left| \Bigl\{ y \in S \,\Big|\, y \le x \Bigr\} \right|, \quad S = \text{sorted\_unique}(\{a_i\})$$
此后 BIT 只开到 $|S|$,所有操作在**排名空间** $[1, |S|]$ 上进行。
---
### 6.4 单点修改:往「值」的位置 +1
插入一个值 $x$,映射到排名 $p$:
$$\boxed{p \xrightarrow{+\text{lowbit}} p+\text{lowbit}(p) \xrightarrow{+\text{lowbit}} \cdots \le m}$$
公式步:
$$\begin{aligned}&\text{add}(p,\ \Delta): \\&\quad \texttt{while } p \le m: \\&\quad\quad \text{tree}[p] \mathrel{+}= \Delta \\&\quad\quad p \mathrel{+}= p\ \&\, (-p)\end{aligned}$$
---
### 6.5 第 $k$ 小:在树状数组上二分
这是值域 BIT 最帅的操作。利用树状数组的**分层结构**直接跳:
$$\boxed{\begin{aligned}&\text{find\_kth}(k): \\&\quad p \leftarrow 0,\quad \text{step} \leftarrow 2^{\lfloor \log_2 m \rfloor} \\&\quad \texttt{while } \text{step} > 0: \\&\quad\quad \texttt{if } p+\text{step} \le m \ \textcolor{red}{\textbf{and}}\ \text{tree}[p+\text{step}] < k: \\&\quad\quad\quad k \mathrel{-}= \text{tree}[p+\text{step}] \\&\quad\quad\quad p \mathrel{+}= \text{step} \\&\quad\quad \text{step} \mathrel{/}= 2 \\&\quad \textbf{return } p+1\end{aligned}}$$
> Python:```pythondef find_kth(k): p,step=0,int(log2(m))>>1 while step>0: if p+step<=m and tree[p+step]<k: k-=tree[p+step] p+=step step/=2```
原理一句话:**从高位到低位试探,如果跳过去之后累计还不够 $k$,就跳**。
---
### 6.6 一张图看懂「值域」vs「普通」
$$\begin{array}{cc}\underbrace{\begin{array}{c}\text{普通 BIT} \\\text{(下标 = 位置)}\end{array}}_{\text{维护 } \sum_{i=1}^{r} a_i}& \qquad\qquad &\underbrace{\begin{array}{c}\text{值域 BIT} \\\text{(下标 = 值)}\end{array}}_{\text{维护 } \sum_{v \le x} c_v} \\[20pt]\begin{array}{|c|c|c|c|c|}\hlinei & 1 & 2 & 3 & \dots \\\hline\text{信息} & a_1 & a_2 & a_3 & \dots \\\hline\end{array}& &\begin{array}{|c|c|c|c|c|}\hlinev & 1 & 2 & 3 & \dots \\\hline\text{信息} & c_1 & c_2 & c_3 & \dots \\\hline\end{array}\end{array}$$
---
### 6.7 一句话
> 普通 BIT 的 $i$ 问的是「**第 $i$ 个元素的前缀和是多少**」; > 值域 BIT 的 $i$ 问的是「**值不超过 $i$ 的数出现了多少次**」。 > 把「位置轴」旋转 $90^\circ$ 变成「值轴」,就是值域树状数组。
---
## 完整代码
::::info[值域树状数组模板(支持离散化 + 逆序对 + 第 k 大)]{open}```cpp line-numbers100 行未改动