侧边栏壁纸
博主头像
CoderKui

坐中静,舍中得,事上练

  • 累计撰写 51 篇文章
  • 累计创建 69 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

差分

CoderKui
2021-11-28 / 0 评论 / 0 点赞 / 232 阅读 / 1,030 字 / 正在检测是否收录...

差分

差分其实就是前缀和的逆运算

给定元素组:a1, a2, a3, ..., an

构造:b1, b2, b3, ..., bn

使得:ai = b1 + b2 + .. + bn

称b为a的差分

一维差分例子:

输入一个长度为 nn 的整数序列。

接下来输入 mm 个操作,每个操作包含三个整数 l,r,cl,r,c,表示将序列中 [l,r][l,r] 之间的每个数加上 cc。

请你输出进行完所有操作后的序列。

输入格式

第一行包含两个整数 nn 和 mm。

第二行包含 nn 个整数,表示整数序列。

接下来 mm 行,每行包含三个整数 l,r,cl,r,c,表示一个操作。

输出格式

共一行,包含 nn 个整数,表示最终序列。

数据范围

1≤n,m≤1000001≤n,m≤100000,
1≤l≤r≤n1≤l≤r≤n,
−1000≤c≤1000−1000≤c≤1000,
−1000≤整数序列中元素的值≤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.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n + 2];
        int[] b = new int[n + 2];
        for(int i = 1; i <= n; ++i) {
            a[i] = sc.nextInt();
        }
        for(int i = 1; i <= n; ++i) {
            insert(b, i, i, a[i]);  
        }
        while(m-- > 0) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            int c = sc.nextInt();
            insert(b, l, r, c);
        }
        for(int i = 1; i <= n; ++i) {
            b[i] += b[i - 1];
        }
        for(int i = 1; i <= n; ++i) {
            System.out.print(b[i] + " ");
        }
    }
    
    public static void insert(int[] num, int l, int r, int c){
        num[l] += c;
        num[r + 1] -= c;
    }
}


二维差分例子:

输入一个 nn 行 mm 列的整数矩阵,再输入 qq 个操作,每个操作包含五个整数 x1,y1,x2,y2,cx1,y1,x2,y2,c,其中 (x1,y1)(x1,y1) 和 (x2,y2)(x2,y2) 表示一个子矩阵的左上角坐标和右下角坐标。

每个操作都要将选中的子矩阵中的每个元素的值加上 cc。

请你将进行完所有操作后的矩阵输出。

输入格式

第一行包含整数 n,m,qn,m,q。

接下来 nn 行,每行包含 mm 个整数,表示整数矩阵。

接下来 qq 行,每行包含 55 个整数 x1,y1,x2,y2,cx1,y1,x2,y2,c,表示一个操作。

输出格式

共 nn 行,每行 mm 个整数,表示所有操作进行完毕后的最终矩阵。

数据范围

1≤n,m≤10001≤n,m≤1000,
1≤q≤1000001≤q≤100000,
1≤x1≤x2≤n1≤x1≤x2≤n,
1≤y1≤y2≤m1≤y1≤y2≤m,
−1000≤c≤1000−1000≤c≤1000,
−1000≤矩阵内元素的值≤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.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int q = sc.nextInt();
        int[][] a = new int[n + 10][m + 10];
        int[][] b = new int[n + 10][m + 10];
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                a[i][j] = sc.nextInt();
            }
        }
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                insert(b, i, j, i, j, a[i][j]);
            }
        }
        while(q-- > 0) {
            int x1, y1, x2, y2, c;
            x1 = sc.nextInt();y1 = sc.nextInt();
            x2 = sc.nextInt();y2 = sc.nextInt();
            c = sc.nextInt();
            insert(b, x1, y1, x2, y2, c);
        }
        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];
            }
        }
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                System.out.print(b[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    public static void insert(int[][] nums, int x1, int y1, int x2, int y2, int c) {
        nums[x1][y1] += c;
        nums[x2 + 1][y1] -= c;
        nums[x1][y2 + 1] -= c;
        nums[x2 + 1][y2 + 1] += c;
    }
}
0

评论区