前缀

图形

image.png

image.png

image.png

题目一:

输入一个长度为 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
代码实现:

  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. /**
  4. * @author JDsen99
  5. * @description
  6. * @createDate 2021/7/6-19:33
  7. */
  8. public class Main {
  9. static int n = 100010;
  10. static int[] a = new int[n];
  11. static int[] s = new int[n];
  12. public static void main(String[] args) throws Exception {
  13. BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
  14. String[] strings = rd.readLine().split(" ");
  15. int m = Integer.parseInt(strings[0]);
  16. int count = Integer.parseInt(strings[1]);
  17. String[] num = rd.readLine().split(" ");
  18. for (int i = 0; i < m; i++)
  19. a[i + 1] = Integer.parseInt(num[i]);
  20. for (int i = 1; i <= m; i++)
  21. s[i] = s[i-1] + a[i];
  22. while(count-- != 0) {
  23. String[] target = rd.readLine().split(" ");
  24. int x = Integer.parseInt(target[0]);
  25. int y = Integer.parseInt(target[1]);
  26. System.out.println(s[y] - s[x-1]);
  27. }
  28. }
  29. }

题目二:

差分

算法思路

拥有数组b[n]后,想要对a数组中所有的数据加上c,只需要将b[1]+c即可,因为a[i]是b[i]的前缀和,a[i]=b[1]+b[1]+b[3]+……+b[n]。b[1]是所有的a[i]都拥有的子元素,将b[0]+c,那么a[n]中所有的数据都会加上c。如果想将a数组中[l,r]部分的数据全部加上c,只需要将b[l]+c,然后b[r+1]-c即可。
差分操作和前缀和一样数组下标都从1开始。
b[l]+c后,l后面的数组都会加c。r后面的数据也会被改变,要改回来就得b[r+1]-c
构造 b[n]
理解即可, 没有那么重要, 通过插入操作构造 b[n]
b[1] = a[1]
b[2] = a[2] - a[1]
b[3 ]= a[3] - a[2]

b[n] = a[n] - a[n-1]
b[n]称为 S[n]的差分
S[n]称为 b[n]的前缀和
差分的用途
对 S数组的某个区间内的数全部加上 c, 现在只需要对 b数组的 2 / 4个数加上 c
时间复杂度由 O(n) -> O(1)
一维数组差分
假设已经求出来 b数组, 只要对 b数组求前缀和就可以求出原数组 S[n]
已知 b[n], 可以用 O(n) 的时间得到 S[n]
差分帮助我们处理一种操作, 在 S数组的 [l, r]区间内加上数 c. 则

  1. 1. a[l] ~ a[L-1]无影响
  2. 1. a[l] ~ a[r] 加上了 c
  3. 1. a[r=1] ~ a[n] 无影响

S数组操作后 对于 b数组的影响, 相当于 b[l] + c, b[r + 1] - c

图形

image.png
image.png
image.png

题目一

输入一个 nn 行 mm 列的整数矩阵,再输入 qq 个询问,每个询问包含四个整数 x1,y1,x2,y2x1,y1,x2,y2,表示一个子矩阵的左上角坐标和右下角坐标。
对于每个询问输出子矩阵中所有数的和。
输入格式
第一行包含三个整数 n,m,q。
接下来 n 行,每行包含 m 个整数,表示整数矩阵。
接下来 q 行,每行包含四个整数 x1,y1,x2,y2表示一组询问。
输出格式
共 qq行,每行输出一个询问的结果。
数据范围
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.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author JDsen99
 * @description
 * @createDate 2021/7/7-8:35
 */
public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
        String[] strings = br.readLine().split(" ");
        int n = Integer.parseInt(strings[0]);
        int m = Integer.parseInt(strings[1]);
        int count = Integer.parseInt(strings[2]);
        int[][] arr = new int[n][m];
        int[][] s = new int[n + 1][m + 1];

        for (int i = 0; i < n; i++) {
            String[] strs = br.readLine().split(" ");
            for (int j = 0; j < m; j++) {
               arr[i][j] = Integer.parseInt(strs[j]);
            }
        }
        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] + arr[i-1][j-1];
            }
        }
        while(count-- != 0) {
            String[] q = br.readLine().split(" ");
            int x1 = Integer.parseInt(q[0]),
                    y1 = Integer.parseInt(q[1]),
                    x2 = Integer.parseInt(q[2]),
                    y2 = Integer.parseInt(q[3]);

            int res = s[x2][y2] - s[x1-1][y2] - s[x2][y1-1] + s[x1-1][y1-1];
            System.out.println(res);
        }
    }
}

题目二

输入一个 n 行 m 列的整数矩阵,再输入 q 个操作,每个操作包含五个整数 x1,y1,x2,y2,其中 (x1,y1)(x1,y1) 和 (x2,y2)(x2,y2) 表示一个子矩阵的左上角坐标和右下角坐标。
每个操作都要将选中的子矩阵中的每个元素的值加上 c。
请你将进行完所有操作后的矩阵输出。
输入格式
第一行包含整数 n,m,q
接下来 n 行,每行包含 m个整数,表示整数矩阵。
接下来 q 行,每行包含 5 个整数 x1,y1,x2,y2,c,表示一个操作。
输出格式
共 nn 行,每行 mm 个整数,表示所有操作进行完毕后的最终矩阵。
数据范围
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.*;

/**
 * @author JDsen99
 * @description
 * @createDate 2021/7/7-9:52
 */
public class Main {
    final static int N = 1010;
    static int[][] b = new int[N][N];

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        //BufferedWriter输出比System.out.print快
        String[] strings = br.readLine().split(" ");
        int n = Integer.parseInt(strings[0]);
        int m = Integer.parseInt(strings[1]);
        int count = Integer.parseInt(strings[2]);
        int[][] a = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            String[] strs = br.readLine().split(" ");
            for (int j = 1; j <= m; j++) {
                a[i][j] = Integer.parseInt(strs[j - 1]);
                insert(i,j,i,j,a[i][j]);
            }
        }

        while (count -- != 0) {
            strings = br .readLine().split(" ");
            int x1 = Integer.parseInt(strings[0]),
                    y1 = Integer.parseInt(strings[1]),
                    x2 = Integer.parseInt(strings[2]),
                    y2 = Integer.parseInt(strings[3]),
                    val = Integer.parseInt(strings[4]);
            insert(x1,y1,x2,y2,val);

        }
        br.close();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                b[i][j] = b[i - 1][j] + b[i][j -1] - b[i-1][j-1] + b[i][j];
                bw.write(b[i][j] + " ");
            }
            bw.write("\n");
        }
        bw.close();
        br.close();
    }
    private static void insert(int x1,int y1,int x2,int y2,int val) {
        b[x1][y1] += val;
        b[x1][y2 + 1] -= val;
        b[x2 + 1][y1] -= val;
        b[x2 + 1][y2 + 1] += val;
    }

}