当前位置:网站首页>798. 差分矩阵
798. 差分矩阵
2022-08-10 03:41:00 【Hunter_Kevin】
题目
输入一个 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≤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 a[N][N], b[N][N];
int main()
{
int n, m, q;
cin >> n >> m >> q;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++){
cin >> a[i][j];
// a[i][j]是b[i][j]的矩阵前缀和数组,围绕a数组和b数组的关系来写代码即可
b[i][j] = a[i][j] - a[i][j-1] - a[i-1][j] + a[i-1][j-1];
}
while(q --){
int x1, y1, x2, y2, c;
cin >> x1 >> y1 >> x2 >> y2 >> c;
b[x1][y1] += c;
b[x1][y2+1] -= c;
b[x2+1][y1] -= c;
b[x2+1][y2+1] += c;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
a[i][j] = a[i][j-1] + a[i-1][j] - a[i-1][j-1] + b[i][j];
cout << a[i][j] << ' ';
}
cout << endl;
}
return 0;
}
边栏推荐
猜你喜欢
一种能让大型数据聚类快2000倍的方法,真不戳
【mindspore产品】【8卡分布式训练】davinci_model : load task fail, return ret
RoyalScope分析仪:CAN总线波形台阶和信号幅值低的问题
【科研绘图】琴图 +箱型图混合 matplotlib库和seabsorn库的使用
超全面的Android面试题汇总
【Mindspore】【310推理】导入mindir文件出错
2022华数杯思路分析
Embedded Sharing Collection 32
maya图片如何导入
Software life cycle (the work of each phase of software engineering)
随机推荐
ZZULIOJ:1022: 三整数排序
Spark面试问题总结
一种能让大型数据聚类快2000倍的方法,真不戳
[STL]map与set
数据库设计中反映用户对数据要求的模式叫什么
ZZULIOJ:1030: 判断直角三角形
GBase 8s迁移失败
torch.nn.CrossEntropyLoss()对应的MindSpore算子是哪个?
leetcode 283:移动零
【Mindspore】【310推理】导入mindir文件出错
Take you to an in-depth understanding of the version update of 3.4.2, what does it bring to users?
【Verilog数字系统设计(夏雨闻)6-------模块的结构、数据类型、变量和基本运算符号2】
【MindSpore功能】运行SSD-MobileNetV1 FPN样例报错
mediaserver创建
mindspore gpu版本安装问题
【IO复用】poll
Flutter 如何安装 pub.dev 上的 package
原型和原型链
Kotlin协程:父子协程的绑定与传递
shell三剑客之sed命令