本文共 2780 字,大约阅读时间需要 9 分钟。
为了高效处理大规模矩阵的多个区间加法操作,我们采用差分数组(Difference Array)方法。这种方法将每个操作分解为四个点的增减,避免了直接遍历矩阵的高时间复杂度。
diff,其大小为 (m+2) x (n+2),以防止越界。(x1, y1, x2, y2, c),在 diff 数组中进行以下更新: diff[x1][y1] += cdiff[x1][y2+1] -= cdiff[x2+1][y1] -= cdiff[x2+1][y2+1] += cdiff 的行前缀和,然后计算列前缀和,得到每个位置的增量。import java.io.*;class Main { public static void main(String[] args) throws Exception { BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); String[] strNums = buf.readLine().split(" "); int m = Integer.valueOf(strNums[0]); int n = Integer.valueOf(strNums[1]); int q = Integer.valueOf(strNums[2]); int[][] a = new int[m + 2][n + 2]; int[][] diff = new int[m + 2][n + 2]; for (int i = 1; i <= m; ++i) { String[] nums = buf.readLine().split(" "); for (int j = 1; j <= n; ++j) { int k = Integer.valueOf(nums[j - 1]); diff[i][j] += k; } } for (int i = 0; i < q; ++i) { String[] nums = buf.readLine().split(" "); int x1 = Integer.valueOf(nums[0]); int y1 = Integer.valueOf(nums[1]); int x2 = Integer.valueOf(nums[2]); int y2 = Integer.valueOf(nums[3]); int k = Integer.valueOf(nums[4]); diff[x1][y1] += k; if (y2 + 1 <= m) { diff[x1][y2 + 1] -= k; } if (x2 + 1 <= n) { diff[x2 + 1][y1] -= k; if (y2 + 1 <= m) { diff[x2 + 1][y2 + 1] += k; } } } for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (i > 1) { diff[i][j] += diff[i - 1][j]; } if (j > 1) { diff[i][j] += diff[i][j - 1]; if (i > 1) { diff[i][j] -= diff[i - 1][j - 1]; } } } } for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { a[i][j] += diff[i][j]; } } for (int i = 1; i <= m; ++i) { writer.write(String.join(" ", String.valueOf(a[i][j]) + "\n")); } buf.close(); writer.flush(); writer.close(); }} n 和 m,以及操作的数量 q。a 和差分数组 diff。这种方法通过差分数组将多个区间加法操作高效处理,避免了直接遍历矩阵的高时间复杂度,确保了程序在大规模数据下的性能。
转载地址:http://pkre.baihongyu.com/