博客
关于我
AcWing 798. 差分矩阵
阅读量:353 次
发布时间:2019-03-04

本文共 2509 字,大约阅读时间需要 8 分钟。

输入一个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≤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 31 2 2 13 2 2 11 1 1 11 1 2 2 11 3 2 3 23 1 3 4 1

输出样例:

2 3 4 14 3 4 12 2 2 2
import java.io.*;import java.lang.*;class Main{        static void diff(int[][] b, int i, int j, int k){//构造差分矩阵        b[i][j] += k;        b[i + 1][j] -= k;        b[i][j + 1] -= k;        b[i + 1][j + 1] += k;    }    static void diffNew(int[][] b, int x1, int y1, int x2, int y2, int k){        b[x1][y1] += k;        b[x1][y2 + 1] -= k;        b[x2 + 1][y1] -=k;        b[x2 + 1][y2 + 1] += k;    }        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[][] b = 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(b, 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]);            diffNew(b, x1, y1, x2, y2, k);        }        for(int i = 1; i <= m; ++i){            for(int j = 1; j <= n; ++j){                a[i][j] = a[i][j - 1] + a[i - 1][j] + b[i][j] - a[i - 1][j - 1];                 writer.write(a[i][j] + " ");                // System.out.printf("%d ", a[i][j]);            }            writer.write("\n");            // System.out.println();        }         //所有write下的内容,会先存在writers中,当启用flush以后,会输出存在其中的内容。如果没有调用flush,则不会将writer中的内容进行输出。        writer.flush();        buf.close();        writer.close();    }}

 

转载地址:http://pkre.baihongyu.com/

你可能感兴趣的文章
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>