前缀和与差分
前缀和
什么是前缀和?
前缀和(Prefix Sum)的定义为:对于一个给定的数列 A, 它的前缀和数列 S 是通过递推能求出来得 部分和。
例如:
A = {1, 2, 3, 4, 5}
S = {1, 3, 6, 10, 15}
即:Sn = a1 + a2 + a3 + … + an
类似于数列的前N项和。
// n 表示数组的长度,但是实际数组的长度是 n + 1, 这是为了for循环中 i 是以 1 开始的,防止出现边界问题,在数组下标为 0 的位置不放元素,所以初始值为 0
int[] a = new int[n + 1];
int[] s = new int[n + 1];
for (int i = 1;i <= n;i ++) a[i] = sc.nextInt(); // 为数组a的每一个位置赋值
for (int i = 1;i <= n;i ++) s[i] += a[i - 1]; // 计算每一位的前缀和放到数组中
// 想要获取某段位置上的数据之和就很简单了,假设想要获取 l 位置到 r 位置上的数据之和
int result = s[r] - s[l - 1];
进一步思考,将上面的一维数组换成二维数组也就是矩阵的形式,
如下面的图所示:
想要求得黄色部分的数据之和,就要用绿色部分 - 蓝色部分 - 红色部分 + 紫色部分。
换成公式:
s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]
在进行矩阵的前缀和累加时的过程是这样的:
S[x, y] 的值就是被黄框圈起来的元素之和, 黄框部分 = 蓝框部分 + 红框部分 - 红框蓝框相较部分 + 黄色的部分
公式:
S[i, j]=S[i, j − 1] + S[i − 1, j] − S[i − 1, j − 1] + a[i, j]
差分
差分相当于是前缀和的逆运算。
设 a[N] 是我们的目标数组,b[N] 是我们的差分数组,则它们的关系是:
a1 = b1
a2 = b1 + b2
a3 = b1 + b2 + b3
…
an = b1 + b2 + … + bn
也就是说数组 a 是 数组 b 的前缀和。
假如我们想要在 数组 a 的[l, r]范围内每个元素都加上 c ,可以考虑迭代方式加上 c ,但是时间复杂度是 O(n)
但是利用 数组 a 的差分数组 b 就可以这样做到:
b[l] += c;
b[r + 1] -= c;
a 是 b 的前缀和, 也就是说 b 中的每一个元素累加起来就是a 中的元素,如果在 b 数组的某个位置加上 c ,a 是 b 的累加,那么从a [l] 到 a[r] 中的每一个数都相当于加上了c。
例题:
import java.util.Scanner;
public class Main {
static final int N = 100010;
static int[] a = new int[N];
static int[] b = new int[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
b[i] = a[i] - a[i - 1];
}
while (m -- != 0) {
int l = sc.nextInt(), r = sc.nextInt(), c = sc.nextInt();
b[l] += c;
b[r + 1] -= c;
}
for (int i = 1; i <= n; i ++) {
a[i] = b[i] + a[i - 1];
System.out.print(a[i] + " ");
}
}
}
换成差分矩阵的形式:
如上图所示:
b[x1][y1] += c
使这个被黄色框圈起来的部分都加上了 c
,所以,想要符合题意只让红框内的部分都加上 c
需要将黄色框中除了红框的部分都减去 c
b[x2 + 1][y1] -= c
使得蓝色框内的所有元素都减去 c
b[x1][y2 + 1] -= c
使得绿色框内的所有元素都减去了 c
因为容斥原理,需要将紫色的部分在加回来所以要把紫色部分的元素都加上 c
: b[x2 + 1][y2 + 1]
b[x1][y1] += c;
b[x2 + 1][y1] -= c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y2 + 1] += c;
import java.util.Scanner;
public class Main {
static final int N = 1010;
static int[][] a = new int[N][N];
static int[][] b = new int[N][N];
public static void insert(int x1, int y1, int x2, int y2, int c) {
b[x1][y1] += c;
b[x2 + 1][y1] -= c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y2 + 1] += c;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt(), q = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j ++) {
a[i][j] = sc.nextInt();
insert(i, j, i, j, a[i][j]);
}
}
while (q-- != 0) {
int l1 = sc.nextInt(), r1 = sc.nextInt(), l2 = sc.nextInt(), r2 = sc.nextInt(), c = sc.nextInt();
insert(l1, r1, l2, r2, c);
}
for (int i = 1;i <= n; i ++) {
for (int j = 1; j <= m; j++) {
b[i][j] = b[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}