树状数组是用于维护单点修改,区间查询的一个复杂度和常数均优秀的代码长度短且好写的数据结构,它的复杂度是单次操作 O ( lg n ) \mathcal O(\lg n) O ( lg n ) 的。
引入
树状数组可以维护的操作需要满足结合律,交换律和可差分性,具体来说对于一个操作 ⊙ \odot ⊙ ,需要满足
( a ⊙ b ) ⊙ c = a ⊙ ( b ⊙ c ) (a\odot b)\odot c=a\odot(b\odot c) ( a ⊙ b ) ⊙ c = a ⊙ ( b ⊙ c )
a ⊙ b = b ⊙ a a\odot b=b\odot a a ⊙ b = b ⊙ a
已知 a ⊙ b a\odot b a ⊙ b 和 a a a 可以快速求出 c c c 。
如果我们要实现一个单点修改区间查询的数据结构,我们可以把区间查询拆成两个前缀和查询再用可差分性求出答案,这就是树状数组的思路。树状数组通过预处理部分特殊区间的和从而实现 O ( lg n ) \mathcal O(\lg n) O ( lg n ) 的修改和查询。
树状数组
设 n n n 表示数组中的元素个数,a a a 表示原数组,c c c 表示我们预处理出的树状数组。
其中 lowbit ( x ) \text{lowbit}(x) lowbit ( x ) 表示 x x x 在二进制下的最低位,例如 lowbit ( 7 ) = 1 , lowbit ( 12 ) = 4 \text{lowbit}(7)=1,\text{lowbit}(12)=4 lowbit ( 7 ) = 1 , lowbit ( 12 ) = 4 ,在实现中可以由 x&-x 计算出(因为补码的特殊性)。
在树状树组中,c i c_i c i 表示 a [ i − lowbit ( i ) + 1 , i ] a_{[i-\text{lowbit}(i)+1,i]} a [ i − lowbit ( i ) + 1 , i ] 的累积 ⊙ \odot ⊙ ,即有 c i = ⨀ j = i − lowbit ( i ) + 1 i a j c_i=\displaystyle{\bigodot_{j=i-\text{lowbit}(i)+1}^i}a_j c i = j = i − lowbit ( i ) + 1 ⨀ i a j 。
性质
性质一:对于任意 x ≤ y x\le y x ≤ y ,要么 c x c_x c x 和 c y c_y c y 不交,要么 c x c_x c x 管辖的区间包含于 c y c_y c y 管辖的区间。
证明 假设 c x c_x c x 和 c y c_y c y 相交,即 [ x − lowbit ( x ) + 1 , x ] [x-\text{lowbit}(x)+1,x] [ x − lowbit ( x ) + 1 , x ] 与 [ y − lowbit ( y ) + 1 , y ] [y-\text{lowbit}(y)+1,y] [ y − lowbit ( y ) + 1 , y ] 相交,那么有 y − lowbit ( y ) + 1 ≤ x ≤ y y-\text{lowbit}(y)+1\le x\le y y − lowbit ( y ) + 1 ≤ x ≤ y 。
将 y y y 表示为 y = s × 2 k + 1 + 2 k , k , s ∈ N y=s\times2^{k+1}+2^k,k,s\in\mathbb N y = s × 2 k + 1 + 2 k , k , s ∈ N ,则 y − lowbit ( y ) + 1 = s × 2 k + 1 + 1 y-\text{lowbit}(y)+1=s\times2^{k+1}+1 y − lowbit ( y ) + 1 = s × 2 k + 1 + 1 ,那么 x x x 就可以表示为 x = s × 2 k + 1 + b , b ∈ [ 1 , 2 k ] x=s\times2^{k+1}+b,b\in[1,2^k] x = s × 2 k + 1 + b , b ∈ [ 1 , 2 k ] 。那么 lowbit ( x ) ∈ [ 1 , b ] \text{lowbit}(x)\in[1,b] lowbit ( x ) ∈ [ 1 , b ] ,因此 x − lowbit(x) + 1 ≥ s × 2 k + 1 + 1 x-\text{lowbit(x)}+1\ge s\times 2^{k+1}+1 x − lowbit(x) + 1 ≥ s × 2 k + 1 + 1 ,所以 c x c_x c x 管辖区间左端点不小于 c y c_y c y 管辖区间右端点,所以 c x c_x c x 管辖区间包含于 c y c_y c y 。
性质二:c x c_x c x 真包含于 c x + lowbit ( x ) c_{x+\text{lowbit}(x)} c x + lowbit ( x ) 。
证明 因为 x + lowbit ( x ) > x x+\text{lowbit}(x)>x x + lowbit ( x ) > x ,所以只需要证明 c x c_x c x 左端点不小于 c x + lowbit ( x ) c_{x+\text{lowbit}(x)} c x + lowbit ( x ) 的左端点。
把 x x x 表示为 x = s × 2 k + 1 + 2 k , k , s ∈ N x=s\times2^{k+1}+2^k,k,s\in\mathbb N x = s × 2 k + 1 + 2 k , k , s ∈ N ,那么
x + lowbit ( x ) = s × 2 k + 1 + 2 k + 2 k = ( s + 1 ) × 2 k + 1 x+\text{lowbit}(x)=s\times2^{k+1}+2^k+2^k=(s+1)\times2^{k+1} x + lowbit ( x ) = s × 2 k + 1 + 2 k + 2 k = ( s + 1 ) × 2 k + 1 所以 lowbit ( x + lowbit ( x ) ) ≥ 2 k + 1 > lowbit ( x ) \text{lowbit}(x+\text{lowbit}(x))\ge2^{k+1}>\text{lowbit}(x) lowbit ( x + lowbit ( x )) ≥ 2 k + 1 > lowbit ( x ) 。
所以 c x + lowbit ( x ) c_{x+\text{lowbit}(x)} c x + lowbit ( x ) 管辖区间的左端点
x + lowbit ( x ) − lowbit ( x + lowbit ( x ) ) + 1 ≤ x x+\text{lowbit}(x)-\text{lowbit}(x+\text{lowbit}(x))+1\le x x + lowbit ( x ) − lowbit ( x + lowbit ( x )) + 1 ≤ x 所以 c x c_x c x 真包含于 c x + lowbit ( x ) c_{x+\text{lowbit}(x)} c x + lowbit ( x )
性质三:对于 y ∈ ( x , x + lowbit ( x ) ) y\in(x,x+\text{lowbit}(x)) y ∈ ( x , x + lowbit ( x )) ,c x c_x c x 与 c y c_y c y 无交。
证明 设 x = s × 2 k + 1 + 2 k , k , s ∈ N x=s\times2^{k+1}+2^k,k,s\in\mathbb N x = s × 2 k + 1 + 2 k , k , s ∈ N ,则 y = s × 2 k + 1 + 2 k + b , b ∈ [ 1 , 2 k ) y=s\times2^{k+1}+2^k+b,b\in[1,2^k) y = s × 2 k + 1 + 2 k + b , b ∈ [ 1 , 2 k ) 。
所以 lowbit ( y ) = lowbit ( b ) ≤ b \text{lowbit}(y)=\text{lowbit}(b)\le b lowbit ( y ) = lowbit ( b ) ≤ b ,因此 c y c_y c y 左端点
y − lowbit ( y ) + 1 = x + b − lowbit ( b ) + 1 > x y-\text{lowbit}(y)+1=x+b-\text{lowbit}(b)+1>x y − lowbit ( y ) + 1 = x + b − lowbit ( b ) + 1 > x 所以 c x c_x c x 和 c y c_y c y 无交。
从性质二和性质三可以推出对于任意 c x c_x c x ,c x + lowbit ( x ) c_{x+\text{lowbit}(x)} c x + lowbit ( x ) 是最近的包含 c x c_x c x 的区间。
修改
考虑修改一个元素会涉及到那些区间。设现在要修改 k k k ,那么显然第一次修改 k k k ,之后根据性质二和性质三的推论,如果上一个修改的是 p p p ,这次修改 p + lowbit ( p ) p+\text{lowbit}(p) p + lowbit ( p ) ,直到 p > n p>n p > n 为止。
实现 C++ 复制 void update (int k , int x ) {
for (; k <= n; k += k & - k)
c[k] += x; // 示例用的 \odot = +,根据具体情况修改
}
很明显每次加上 lowbit \text{lowbit} lowbit 都会让当前 lowbit \text{lowbit} lowbit 乘上 2 2 2 ,而 lowbit ( x ) ≤ x \text{lowbit}(x)\le x lowbit ( x ) ≤ x ,故最多只会更新 lg n \lg n lg n 个节点,复杂度为 O ( lg n ) \mathcal O(\lg n) O ( lg n ) 。
查询
假设现在要查询 ⨀ a [ 1 , k ] \bigodot a_{[1,k]} ⨀ a [ 1 , k ] ,那么 c k c_k c k 覆盖了 [ k − lowbit ( k ) + 1 , k ] [k-\text{lowbit}(k)+1,k] [ k − lowbit ( k ) + 1 , k ] ,于是接着求 ⨀ a [ 1 , k − lowbit ( k ) ] \bigodot a_{[1,k-\text{lowbit}(k)]} ⨀ a [ 1 , k − lowbit ( k )] 就行了,即让 k ← k − lowbit ( k ) k\leftarrow k-\text{lowbit}(k) k ← k − lowbit ( k ) ,直到 k = 0 k=0 k = 0 。
实现 C++ 复制 int query (int k ) {
int res = 0 ;
for (; k; k -= k & - k)
res += c[k]; // 示例用的 \odot = +,根据具体情况调整
return res;
}
放一份完整实现( ⊙ = + \odot=+ ⊙ = + ):
C++ 复制 int c[N], n;
void update (int k , int x ) {
for (; k <= n; k += k & - k)
c[k] += x;
}
int query (int k ) {
int res = 0 ;
for (; k; k -= k & - k)
res += c[k];
return res;
}
其它运用
区间修改单点查询
直接把维护的数组变为差分数组即可。
区间修改区间查询
维护差分数组,设为 d d d ,推一下式子:
∑ i = 1 n ∑ j = 1 i d j = ∑ i = 1 n d i ( n − i + 1 ) = ( n + 1 ) ∑ i = 1 n d i − ∑ i = 1 n i d i \begin{align}
\sum_{i=1}^n\sum_{j=1}^id_j&=\sum_{i=1}^nd_i(n-i+1)\\
&=(n+1)\sum_{i=1}^nd_i-\sum_{i=1}^nid_i
\end{align} i = 1 ∑ n j = 1 ∑ i d j = i = 1 ∑ n d i ( n − i + 1 ) = ( n + 1 ) i = 1 ∑ n d i − i = 1 ∑ n i d i
维护 d i d_i d i 和 d i × i d_i\times i d i × i 即可。
二维树状数组
事实上就是把 c k c_k c k 扩展为了 c i , j c_{i,j} c i , j 表示从 ( i − lowbit ( i ) + 1 , j − lowbit ( j ) + 1 ) (i-\text{lowbit}(i)+1,j-\text{lowbit}(j)+1) ( i − lowbit ( i ) + 1 , j − lowbit ( j ) + 1 ) 到 ( i , j ) (i,j) ( i , j ) 这个矩阵的值,就是树状数组套树状数组,在每个 c k c_k c k 位置再开一个树状数组而已。复杂度显然 O ( lg 2 n ) \mathcal O(\lg^2 n) O ( lg 2 n )
代码:
C++ 复制 int c[N][M], n, m;
void update (int x , int y , int k ) {
for (int i = x; i <= n; i += i & - i)
for (int j = y; j <= m; j += j & - j)
c[i][j] += k;
}
int query (int x , int y ) {
int res = 0 ;
for (int i = x; i; i -= i & - i)
for (int j = y; j; j -= j & - j)
res += c[i][j];
return res;
}
// 前缀和 => 差分
int query (int x1 , int y1 , int x2 , int y2 ) {
return query (x2, y2) - query (x1 - 1 , y2) - query (x2, y1 - 1 ) + query (x1 - 1 , y1 - 1 );
}
以此类推,可以实现高维树状数组,复杂度依此类推。二维树状数组也可以实现矩阵加矩阵和,和区间加区间和类似,这里就不展开了。
树状数组上倍增
如果我们要求第一个 k k k 使得前缀和大于等于某个数字,直接二分是 O ( lg 2 n ) \mathcal O(\lg^2n) O ( lg 2 n ) 的,但是我们可以通过树状数组 c c c 数组的特殊性,使用倍增的思想来做。具体来说就是从高位往低位尝试,设当前为 x x x ,那么 x + 2 k , k ∈ N , 2 ∣ x / 2 k x+2^k,k\in\mathbb N,2|x/2^k x + 2 k , k ∈ N , 2∣ x / 2 k 的 lowbit \text{lowbit} lowbit 正好为 2 k 2^k 2 k ,新增的和可以用 c x + lowbit(x) c_{x+\text{lowbit(x)}} c x + lowbit(x) 表示。这样就可以 O ( lg n ) \mathcal O(\lg n) O ( lg n ) 做了。
Tricks
Θ ( n ) \Theta(n) Θ ( n ) 建树
预处理出前缀和 s s s 然后根据定义 c k = s k − s k − lowbit ( k ) c_k=s_k-s_{k-\text{lowbit}(k)} c k = s k − s k − lowbit ( k ) 。
时间戳优化
这适用于需要快速多次清空树状数组的情况。
我们在每个 c c c 的位置打上一个时间戳表示最后一次使用它的时间,在查询和修改时如果发现经过位置的时间戳比当前时间戳旧,就清空它。这样清空树状数组只需要让时间戳加一就行了。
代码:
C++ 复制 int n, c[N], d[N], tick;
void update (int k , int x ) {
for (; k <= n; k += k & - k)
c[k] = (d[k] == tick ? c[k] : 0 ) + x;
}
int query (int k ) {
int res = 0 ;
for (; k; k -= k & - k)
d[k] == tick && (res += c[k]);
return res;
}
void clear () {
tick++ ;
}
只查询前缀和的不可差分信息维护
对于不可差分但满足交换律、结合律的运算,例如 max , min , gcd \max,\min,\gcd max , min , g cd ,树状数组无法维护它们的任意区间信息,因为树状数组只能求前缀和。但如果保证不会出现查询左端点不为序列左端点的情况就可以用树状数组维护,相比于线段树不仅码量小了,常数也变小了。堪称卡常利器。