# 1、差分&差分矩阵

# AcWing 797. 差分

# AcWing 798. 差分矩阵

import java.util.*;

//ACWing
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.chafeng();
    }
    int N = 100010;
    int a[] = new int[N];
    int b[] = new int[N];
    void insert(int l, int r, int c) {
        b[l] += c;
        b[r + 1] -= c;
    }
    //一维差分
    void chafeng() {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        for (int i = 0; i < n; i++) insert(i, i, a[i]);
        for (int i = 0; i < m; i++) {
            int l = in.nextInt();
            int r = in.nextInt();
            int c = in.nextInt();
            insert(l-1, r-1, c);
        }
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += b[i];
            System.out.print(sum + " ");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;

//ACWing
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.chafengjuzheng();
    }
    //差分矩阵数组
    void insert(int x1,int y1,int x2,int y2,int c){
        b[x1][y1]+=c;
        b[x2+1][y2+1]+=c;
        b[x2+1][y1]-=c;
        b[x1][y2+1]-=c;
    }

    int N=1010;
    int a[][]=new int[N][N];
    int b[][]=new int[N][N];
    void chafengjuzheng(){
        Scanner in=new Scanner(System.in);
        int n=in.nextInt(),m=in.nextInt(),q=in.nextInt();
        for (int i = 1; i <=n; i++) {
            for (int j = 1; j <=m; j++) {
                a[i][j]=in.nextInt();
                insert(i,j,i,j,a[i][j]);
            }
        }
        for (int i = 0; i < q; i++) {
            int x1=in.nextInt(),y1=in.nextInt();
            int x2=in.nextInt(),y2=in.nextInt();
            int c=in.nextInt();
            insert(x1,y1,x2,y2,c);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                b[i][j]+=b[i][j-1]+b[i-1][j]-b[i-1][j-1];
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44