前缀和
一维前缀和
令
s [ 1 ] = a [ 1 ] s[1] = a[1] s[1]=a[1]
s [ 2 ] = a [ 1 ] + a [ 2 ] s[2] = a[1] + a[2] s[2]=a[1]+a[2]
s [ 3 ] = a [ 1 ] + a [ 2 ] + a [ 3 ] s[3] = a[1] + a[2] + a[3] s[3]=a[1]+a[2]+a[3]
s [ n ] = ∑ i = 1 n a [ i ] s[n] = \sum_{i=1}^na[i] s[n]=i=1∑na[i]
那么
s [ i ] = s [ i − 1 ] + a [ i ] s[i]=s[i-1]+a[i] s[i]=s[i−1]+a[i]
那么 a [ l ] a[l] a[l]到 a [ r ] a[r] a[r]的区间和
∑ i = l r a [ i ] = s [ r ] − s [ l − 1 ] \sum_{i=l}^ra[i] = s[r] - s[l-1] i=l∑ra[i]=s[r]−s[l−1]
二维前缀和
令
s [ i ] [ j ] = ∑ x = 1 i ∑ y = 1 j a [ i ] [ j ] s[i][j] = \sum_{x=1}^i\sum_{y=1}^ja[i][j] s[i][j]=x=1∑iy=1∑ja[i][j]
那么
s [ i ] [ j ] = s [ i − 1 ] [ j ] + s [ i ] [ j − 1 ] − s [ i − 1 ] [ j − 1 ] + a [ i ] [ j ] ( 默 认 数 组 越 界 的 s 为 0 ) s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j]\space\space\space\space\space\space(默认数组越界的s为0) s[i][j]=s[i−1][j]+s[i][j−1]−s[i−1][j−1]+a[i][j] (默认数组越界的s为0)
那么 ( x 1 , y 1 ) 到 ( x 2 , y 2 ) 的 区 间 和 (x_1,y_1)到(x_2,y_2)的区间和 (x1,y1)到(x2,y2)的区间和
∑ x = x 1 x 2 ∑ y = y 1 y 2 a [ i ] [ j ] = s [ x 2 ] [ y 2 ] − s [ x 1 − 1 ] [ y 2 ] − s [ x 2 ] [ y 1 − 1 ] + s [ x 1 − 1 ] [ y 1 − 1 ] \sum_{x=x_1}^{x_2}\sum_{y=y1}^{y_2}a[i][j]=s[x_2][y_2]-s[x_1-1][y_2]-s[x_2][y_1-1]+s[x_1-1][y_1-1] x=x1∑x2y=y1∑y2a[i][j]=s[x2][y2]−s[x1−1][y2]−s[x2][y1−1]+s[x1−1][y1−1]
差分
一维差分
令
b [ 1 ] = a [ 1 ] b[1] = a[1] b[1]=a[1]
b [ 2 ] = a [ 2 ] − a [ 1 ] b[2] = a[2] - a[1] b[2]=a[2]−a[1]
b [ 3 ] = a [ 3 ] − a [ 2 ] b[3] = a[3] - a[2] b[3]=a[3]−a[2]
b [ i ] = a [ i ] − a [ i − 1 ] b[i] = a[i] - a[i-1] b[i]=a[i]−a[i−1]
那么
a [ i ] = a [ i − 1 ] + b [ i ] a[i] = a[i-1]+b[i] a[i]=a[i−1]+b[i]
那么
∑ i = 1 n b [ i ] = a [ n ] \sum_{i=1}^nb[i] = a[n] i=1∑nb[i]=a[n]
将 a [ l ] a[l] a[l] 到 a [ r ] a[r] a[r] 的区间加上 c
a ′ [ l ] = a [ l ] + c a^{'}[l]\space\space\space\space\space\space\space=a[l] \space\space\space\space\space\space\space\space+\space c a′[l] =a[l] + c
a ′ [ l + 1 ] = a [ l + 1 ] + c a^{'}[l+1]=a[l+1]\space+\space c a′[l+1]=a[l+1] + c
a ′ [ l + 2 ] = a [ l + 2 ] + c a^{'}[l+2]=a[l+2]\space+\space c a′[l+2]=a[l+2] + c
. . . . . . \space...\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space... ... ...
a ′ [ r ] = a [ r ] + c a^{'}[r]\space\space\space\space\space\space=a[r]\space\space\space\space\space\space\space+\space c a′[r] =a[r] + c
令
b ′ [ l ] = a ′ [ l ] − a [ l − 1 ] b^{'}[l]\space\space\space\space\space\space\space=a^{'}[l] \space\space\space\space\space\space-\space a[l-1] b′[l] =a′[l] − a[l−1]
b ′ [ l + 1 ] = a ′ [ l + 1 ] − a ′ [ l ] b^{'}[l+1]=a^{'}[l+1]-\space a^{'}[l] b′[l+1]=a′[l+1]− a′[l]
a ′ [ l + 2 ] = a ′ [ l + 2 ] − a ′ [ l + 1 ] a^{'}[l+2]=a^{'}[l+2] -\space a^{'}[l+1] a′[l+2]=a′[l+2]− a′[l+1]
. . . . . . \space...\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space... ... ...
b ′ [ r ] = a ′ [ r ] − a ′ [ r − 1 ] b^{'}[r]\space\space\space\space\space\space=a^{'}[r]\space\space\space\space\space\space\space-\space a^{'}[r-1] b′[r] =a′[r] − a′[r−1]
b ′ [ r + 1 ] = a [ r + 1 ] − a ′ [ r − 1 ] b^{'}[r+1]=a[r+1]- a^{'}[r-1] b′[r+1]=a[r+1]−a′[r−1]
那么
b ′ [ l ] = b [ l ] + d b^{'}[l]\space\space\space\space\space\space\space=b[l]\space\space\space\space\space\space\space\space+d b′[l] =b[l] +d
b ′ [ l + 1 ] = b [ l + 1 ] b^{'}[l+1]=b[l+1] b′[l+1]=b[l+1]
b ′ [ l + 2 ] = b [ l + 2 ] b^{'}[l+2]=b[l+2] b′[l+2]=b[l+2]
. . . . . . \space...\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space... ... ...
b ′ [ r ] = b [ r ] b^{'}[r]\space\space\space\space\space\space=b[r] b′[r] =b[r]
b ′ [ r + 1 ] = b [ r + 1 ] − d b^{'}[r+1]=b[r+1]\space-d b′[r+1]=b[r+1] −d
若将 r r r 换成 + ∞ +\infty +∞,那么
b [ l ] + = c ⇒ ⋂ i = l + ∞ ( a [ l ] + = c ) b[l]+=c \Rightarrow\bigcap_{i=l}^{+\infty}(a[l]+=c) b[l]+=c⇒i=l⋂+∞(a[l]+=c)
因此
⋂ i = l r ( a [ i ] + = c ) ⇒ { b [ l ] + = c b [ r + 1 ] − = c \bigcap_{i=l}^r(a[i]+=c)\Rightarrow \begin{cases} b[l]+=c\\ b[r+1]-=c \end{cases} i=l⋂r(a[i]+=c)⇒{ b[l]+=cb[r+1]−=c
二维差分
令
∑ x = 1 i ∑ y = 1 j b [ i ] [ j ] = a [ i ] [ j ] \sum_{x=1}^i\sum_{y=1}^jb[i][j]=a[i][j] x=1∑iy=1∑jb[i][j]=a[i][j]
那么
a [ i ] [ j ] = a [ i − 1 ] [ j ] + a [ i ] [ j − 1 ] − a [ i − 1 ] [ j − 1 ] + b [ i ] [ j ] a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+b[i][j] a[i][j]=a[i−1][j]+a[i][j−1]−a[i−1][j−1]+b[i][j]
那么
b [ i ] [ j ] + = c ⇒ ⋂ x = i + ∞ ⋂ y = j + ∞ ( a [ i ] [ j ] + = c ) b[i][j]+=c\Rightarrow \bigcap_{x=i}^{+\infty}\bigcap_{y=j}^{+\infty}(a[i][j]+=c) b[i][j]+=c⇒x=i⋂+∞y=j⋂+∞(a[i][j]+=c)
a [ i ] [ j ] + = c ⇒ { b [ i ] [ j ] + = c b [ i ] [ j + 1 ] − = c b [ i + 1 ] [ j ] − = c b [ i + 1 ] [ j + 1 ] + = c a[i][j]+=c\Rightarrow \left\{ \begin{array}{c} b[i][j]+=c \\ b[i][j+1]-=c \\ b[i+1][j]-=c \\ b[i+1][j+1]+=c \end{array} \right. a[i][j]+=c⇒⎩⎪⎪⎨⎪⎪⎧b[i][j]+=cb[i][j+1]−=cb[i+1][j]−=cb[i+1][j+1]+=c
⋂ x = x 1 x 2 ⋂ y = y 1 y 2 ( a [ i ] [ j ] + = c ) ⇒ { b [ x 1 ] [ y 1 ] + = c b [ x 1 ] [ y 2 + 1 ] − = c b [ x 2 + 1 ] [ y 1 ] − = c b [ x 2 + 1 ] [ y 2 + 1 ] + = c \bigcap_{x=x_1}^{x_2}\bigcap_{y=y_1}^{y_2}(a[i][j]+=c)\Rightarrow \left\{ \begin{array}{c} b[x_1][y_1]+=c \\ b[x_1][y_2+1]-=c \\ b[x_2+1][y_1]-=c \\ b[x_2+1][y_2+1]+=c \end{array} \right. x=x1⋂x2y=y1⋂y2(a[i][j]+=c)⇒⎩⎪⎪⎨⎪⎪⎧b[x1][y1]+=cb[x1][y2+1]−=cb[x2+1][y1]−=cb[x2+1][y2+1]+=c