当前位置:网站首页>【11. 二维差分】
【11. 二维差分】
2022-06-27 07:42:00 【小呆鸟_coding】
二维差分
思路:
- 与一维差分思想一样,
可以看上一个我的一维差分- 假设二维差分数组为
b[][],在以(x1,y1)为左上角,(x2,y2)为右下角的子矩阵中的所有元素都加上一个c.
图解
题目
输入一个 nn 行 mm 列的整数矩阵,再输入 qq 个操作,每个操作包含五个整数 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 ≤ 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#include <iostream> using namespace std; const int N = 1010; int n, m, q; int a[N][N], b[N][N]; void insert(int x1, int y1, int x2, int y2, int c) { b[x1][y1] += c; b[x2 + 1][y1] -= c; b[x1][y2 + 1] -= c; b[x2 + 1][y2 + 1] += c; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) scanf("%d", &a[i][j]); for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) insert(i, j, i, j, a[i][j]); while (q --) { int x1, y1, x2, y2, c; cin >> x1 >> y1 >> x2 >> y2 >> c; 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 - 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 ++) { printf("%d ", b[i][j]); } printf(" \n"); } }
边栏推荐
- js输出1-100之间所有的质数并求总个数
- How can I import data from Oracle into fastdfs?
- JS uses the while cycle to calculate how many years it will take to grow from 1000 yuan to 5000 yuan if the interest rate for many years of investment is 5%
- How torch.gather works
- University database mysql
- c#的初步认识
- VNC Viewer方式的远程连接树莓派
- R 语言 基于关联规则与聚类分析的消费行为统计
- Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé
- 【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法
猜你喜欢

js中判断成绩是否合格,范围在0-100,否则重新输入

One person manages 1000 servers? This automatic operation and maintenance tool must be mastered

洛谷刷题心得记录
![[Software Engineering] software engineering review outline of Shandong University](/img/38/2c783df56b50dee3bbb908f6f3e70e.png)
[Software Engineering] software engineering review outline of Shandong University

基础知识 | js基础

JS output all prime numbers between 1-100 and calculate the total number

What is the difference between volatile and synchronized?

【编译原理】山东大学编译原理复习提纲

一個人管理1000臺服務器?這款自動化運維工具一定要掌握

Error in idea connection database
随机推荐
Hutool symmetric encryption
jupyter notebook文件目录
VNC Viewer方式的远程连接树莓派
多表联查--07--- Hash join
Delay queue `delayqueue`
JS print 99 multiplication table
Manim math engine
Speech synthesis: tacotron explains [end-to-end speech synthesis model] [compared with traditional speech synthesis, it does not have complex phonetics and acoustic feature modules, but only uses < te
JDBC operation MySQL example
What are the specialties of database system engineers?
认识O(NlogN)的排序
Testing network connectivity with the blackbox exporter
C how to call line and rows when updating the database
JS example print the number and sum of multiples of all 7 between 1-100
js用switch输出成绩是否合格
磁选机是什么?
js用switch语句根据1-7输出对应英文星期几
File 与 MultipartFile概述
How to bind SQL statements to web buttons
一個人管理1000臺服務器?這款自動化運維工具一定要掌握

