当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
wind7 无法安装tools (问题已解决)
mindspore安装过程中报错cannot find zlib
Take you to an in-depth understanding of the version update of 3.4.2, what does it bring to users?
Do you know these basic types of software testing?
sql注入之宽字节注入,limit,order by
@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
MindSpore官方RandomChoiceWithMask算子用例报错
PID与ADRC
ARP Spoofing - Tutorial Details
maya视图如何切换
随机推荐
10个超赞的C语言开源项目,值得学习
Small program subcontracting and subcontracting pre-download
多元函数的3D可视化,终于被我总结出来了,数学真是太美了
YAPI使用
ZZULIOJ:1021: 三个整数的最大值
微信小程序相互跳转如何携带参数
2022年起重机械指挥操作证考试题库及模拟考试
TCP协议之《QUICKACK模式》
Pytorch中的torch.index_select对应MindSpore哪个方法
GBase 8s迁移失败
TCP协议之《Out-Of-Window报文限速》
Flutter 如何安装 pub.dev 上的 package
TCP协议之《延迟ACK策略》
请问mindspore支持l1范数归一化吗
mediaserver创建
数据库中数据的正确性和相容性是什么
Dynamic Web Development Fundamentals
TCP协议之《ACK状态4种详解》
@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
TCP协议之《发送缓存控制tcp_notsent_lowat》