前缀和,差分
引入
前缀和
对区间[l,r]询问sum
差分
对区间[l,r]都增加(或减少)一个数
前缀和
处理出一个前缀和数组
for(int i = 1; i <= nums; i ++)
{
a[i] = a[i - 1] + q[i];
}
每一项代表着所有前面项的和,所以区间[l,r]的和就相当于
s[r] - s[l - 1]
差分
处理出一个差分数组
for(int i = 1; i <= nums; i ++)
{
b[i] = a[i] - a[i - 1];
}
每一项代表着原数组这一项与前一项之差,所以区间[l,r]内增加或减少一个数只需要
b[l] += x, b[r + 1] -= x
对差分数组求前缀和变为原数组
对前缀和数组求差分变为原数组
例题:
前缀和:
输入一个长度为 n 的整数序列。
接下来再输入 m 个询问,每个询问输入一对 l,r
对于每个询问,输出原序列中从第 l 个数到第 r 个数的和。
输入格式
第一行包含两个整数 n 和 m。
第二行包含 n 个整数,表示整数数列。
接下来 m 行,每行包含两个整数 l 和 r,表示一个询问的区间范围。
输出格式
共 m 行,每行输出一个询问的结果。
数据范围
1≤l≤r≤n
1≤n,m≤100000
−1000≤数列中元素的值≤1000输入样例:
5 3 2 1 3 6 4 1 2 1 3 2 4
输出样例:
3 6 10
import java.util.Scanner;
public class Main {
static int N = (int)1e5 + 10;
static int s[] = new int[N];
static int n,m;
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++) {
int v = sc.nextInt();
s[i] = s[i - 1] + v;
}
while (m -- != 0){
int l = sc.nextInt();
int r = sc.nextInt();
System.out.println(s[r] - s[l - 1]);
}
sc.close();
}
}
二维前缀和:
输入一个 n 行 m 列的整数矩阵,再输入 qq 个询问,每个询问包含四个整数 x1,y1,x2,y2表示一个子矩阵的左上角坐标和右下角坐标。
对于每个询问输出子矩阵中所有数的和。
输入格式
第一行包含三个整数 n,m,q
接下来 n 行,每行包含 m 个整数,表示整数矩阵。
接下来 q 行,每行包含四个整数 x1,y1,x2,y2,表示一组询问。
输出格式
共 q 行,每行输出一个询问的结果。
数据范围
1≤n,m≤1000
1≤q≤200000
1≤x1≤x2≤n
1≤y1≤y2≤m
−1000≤矩阵内元素的值≤1000输入样例:
3 4 3 1 7 2 4 3 6 2 8 2 1 2 3 1 1 2 2 2 1 3 4 1 3 3 4
输出样例:
17 27 21
import java.util.Scanner;
public class Main {
static int N = 1010;
static int n,m,k;
static int s[][] = new int[N][N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int v = sc.nextInt();
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + v;
}
}
while (k -- != 0){
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 =sc.nextInt();
System.out.println(s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);
}
sc.close();
}
}
差分:
输入一个长度为 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 = (int)1e5 + 10;
static int n,m;
static int q[] = new int[N];
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++) {
int v = sc.nextInt();
add(i,i,v);
}
while (m -- != 0){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
add(a,b,c);
}
for (int i = 1; i <= n; i++) {
q[i] += q[i - 1];
System.out.print(q[i] + " ");
}
sc.close();
}
public static void add(int l,int r,int x){
q[l] += x;
q[r + 1] -= x;
}
}
二维差分:
输入一个 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.util.Scanner;
public class Main {
static int N = 1010;
static int n,m,k;
static int s[][] = new int[N][N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int v = sc.nextInt();
add(i,j,i,j,v);
}
}
while (k -- != 0){
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int v = sc.nextInt();
add(x1,y1,x2,y2,v);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
System.out.print(s[i][j] + " ");
}
System.out.println();
}
sc.close();
}
public static void add(int x1,int y1,int x2,int y2,int c){
s[x1][y1] += c;
s[x2 + 1][y2 + 1] += c;
s[x2 + 1][y1] -= c;
s[x1][y2 + 1] -= c;
}
}
- 本文作者: ShanTianQi
- 本文链接: https://stq555.com/2022/02/11/%E5%89%8D%E7%BC%80%E5%92%8C-%E5%B7%AE%E5%88%86/index.html
- 版权声明:本博客所有文章均采用 BY-NC-SA 许可协议,转载请注明出处!