一维差分
在一个数组中,例如1,2,3,4,5,6,我们想在指定区间都加上或减上一个数的话,比如在区间[2,4]都加上25,那么你就便利数组区间在2-4的下标,然后统一加上25。但是如果这个数组足够长呢,现在要给数组添加n组数字,或者减去,并且还有n个不一样的区间,这样计算量就非非常大。
首先再创建一个数组b,储存原数组a前缀和,比如a每一位都是0,那么前缀和都是0,在[0,0]区间加上1,那么就在新数组的第0位加上1,并且在第1位减去1。现在新数组就是{1,-1,0,0,0,0} ,第[1,1,] 区间加上2,那么新数组第2位就加上2,第三位减去2,就是{1,1,-2,0,0,0},前1位的前缀和就是原数组的第一位,前2位的前缀和就是原数组的第二位,公式就是 b[i] += c,b[i + 1] -=c,代码如下
private static void f(int l, int r, int c) {
b[l] += c;
b[r + 1] -= c;
}
例题:
输入一个长度为n的整数序列。
接下来输入m个操作,每个操作包含三个整数l, r, c,表示将序列中[l, r]之间的每个数加上c。
请你输出进行完所有操作后的序列。
输入格式
第一行包含两个整数n和m。
第二行包含n个整数,表示整数序列。
接下来m行,每行包含三个整数l,r,c,表示一个操作。
输出格式
共一行,包含n个整数,表示最终序列。
数据范围
1≤n,m≤100000,
1≤l≤r≤n,
−1000≤c≤1000,
−1000≤整数序列中元素的值≤1000
输入样例:
6 3
1 2 2 1 2 1
1 3 1
3 5 1
1 6 1
输出样例:
3 4 5 3 4 2
题解:
import java.util.Scanner;
public class Main {
static int N = 100010;
static int[] f = new int[N], t = new int[N];
static int n, m;
private static void insert(int l, int r, int c) {
t[l] += c;
t[r + 1] -= c;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 1; i <= n; i++){
f[i] = sc.nextInt();
insert(i, i, f[i]);
}
while(m -- > 0){
int l = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
insert(l, r, c);
}
for(int i = 1; i <= n; i++){
t[i] += t[i - 1];
System.out.print(t[i] + " ");
}
}
}
差分矩阵(二维数组差分)
在一个二维数组(2,2)和(6,6)区间里加上c,那么我们先在b[][] 数组的(2,2)位置加上c,那么就导致(2,2)之后的前缀和都加上了c,如下图
我们所要求的只是红色区域+c,但是现在蓝色区域都加上c了,所以需要(7,2)-=c 和 (2,7)-=c 和 (7,7) += c 看下图为什么是这三个点个点
上图中橙色相交区域减了两次c,所以最后要在给(7,7)加上c,所以二维数组的差分公式就是 :
private static void insert(x1 ,y1 ,x2, y2, c) {
b[x1][y1] += c;
b[x2 + 1][y1] -= c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y2 + 1] += c;
}
例题:
输入一个n行m列的整数矩阵,再输入q个操作,每个操作包含五个整数x1, y1, x2, y2, c,其中(x1, y1)和(x2, y2)表示一个子矩阵的左上角坐标和右下角坐标。
每个操作都要将选中的子矩阵中的每个元素的值加上c。
请你将进行完所有操作后的矩阵输出。
输入格式
第一行包含整数n,m,q。
接下来n行,每行包含m个整数,表示整数矩阵。
接下来q行,每行包含5个整数x1, y1, x2, y2, c,表示一个操作。
输出格式
共 n 行,每行 m 个整数,表示所有操作进行完毕后的最终矩阵。
数据范围
1≤n,m≤1000,
1≤q≤100000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤c≤1000,
−1000≤矩阵内元素的值≤1000
输入样例:
3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
1 1 2 2 1
1 3 2 3 2
3 1 3 4 1
输出样例:
2 3 4 1
4 3 4 1
2 2 2 2
代码:
import java.io.*;
public class Main {
static int N = 1010;
static int[][] f = new int[N][N], t = new int[N][N];
static int n, m, q;
private static void insert(int x1, int y1, int x2, int y2, int c) {
t[x1][y1] += c;
t[x2 + 1][y1] -= c;
t[x1][y2 + 1] -= c;
t[x2 + 1][y2 + 1] += c;
}
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String str1[] = reader.readLine().split(" ");
n = Integer.parseInt(str1[0]);
m = Integer.parseInt(str1[1]);
q = Integer.parseInt(str1[2]);
for(int i = 1; i <= n; i++) {
String str2[] = reader.readLine().split(" ");
for(int j = 1; j <= m; j++) {
f[i][j] = Integer.parseInt(str2[j - 1]);
insert(i, j, i, j, f[i][j]);
}
}
while(q -- > 0) {
String[] str3 = reader.readLine().split(" ");
int x1 = Integer.parseInt(str3[0]);
int y1 = Integer.parseInt(str3[1]);
int x2 = Integer.parseInt(str3[2]);
int y2 = Integer.parseInt(str3[3]);
int c = Integer.parseInt(str3[4]);
insert(x1, y1, x2, y2, c);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
t[i][j] = t[i - 1][j] + t[i][j - 1] - t[i - 1][j - 1] + t[i][j];
writer.write(t[i][j] + " ");
}
writer.write("\n");
}
writer.flush();
writer.close();
reader.close();
}
}