当前位置:网站首页>Acwing 798 two dimensional difference (difference matrix)
Acwing 798 two dimensional difference (difference matrix)
2022-06-12 16:18:00 【_ Liuxiaoyu】
Enter a n That's ok m The integer matrix of columns , Input again q Operations , Each operation contains five integers x1,y1,x2,y2,c, among (x1,y1) and (x2,y2) Represents the upper left and lower right coordinates of a submatrix .
For each operation, add the value of each element in the selected sub matrix c.
Please output the matrix after all operations .
Input format
The first line contains integers n,m,q.
Next n That's ok , Each row contains m It's an integer , Represents an integer matrix .
Next q That's ok , Each row contains 5 It's an integer x1,y1,x2,y2,c, Represents an operation .
Output format
common n That's ok , Each row m It's an integer , Represents the final matrix after all operations are completed .
Data range
1≤n,m≤1000,
1≤q≤100000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤c≤1000,
−1000≤ The values of the elements in the matrix ≤1000
sample input :
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
sample output :
2 3 4 1
4 3 4 1
2 2 2 2
effect : Same as one-dimensional difference , We construct a two-dimensional difference fraction group to Let the original two-dimensional array a Add... To each element in the selected neutron matrix c The operation of , Can be O(n*n) The time complexity is optimized to O(1)
Two dimensional difference principle : Same as one-dimensional difference , You need to construct a matrix b, Make primitive matrix a Is its prefix and matrix .

How to find :
The core b[x1][y1] + = c;b[x1,][y2+1] - = c;b[x2+1][y1] - = c;b[x2+1][y2+1] + = c;
#include<iostream>
using namespace std;
const int N = 1010;
int a[N][N], b[N][N];
int n,m,q;
// The core , Insert the function
inline 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()
{
cin >> n >> m >> q;
for(int i =1; i<=n; i++)
for(int j =1;j<=m;j++)
{
cin >> a[i][j];
insert(i,j,i,j,a[i][j]);
}
for(int i=1;i<=q;i++)
{
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]; // The prefix and Equal to the matrix to be solved later
}
for(int i = 1; i<=n;i++)
{
for(int j=1;j<=m; j++)
{
cout << b[i][j] << " ";
}
cout << endl;
}
return 0;
}
边栏推荐
- puppeteer入门之 Page 类
- 5-5 configuring MySQL replication log point based replication
- Redis General Command
- Multimix:从医学图像中进行的少量监督,可解释的多任务学习
- MySQL blob and text types
- Kill program errors in the cradle with spotbugs
- 面试:什么是浅拷贝、深拷贝?
- Batch --04--- moving components
- 思考游戏王决斗链接中抽卡概率问题
- Tensorflow function: tf nn. in_ top_ k()
猜你喜欢

统计机器学习代码合集

Scanpy (VI) analysis and visualization of spatial transcriptome data

超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群

Explore the Apache shardingsphere SQL parse format function

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)

Batch --03---cmdutil

思考游戏王决斗链接中抽卡概率问题

When programming is included in the college entrance examination...

【工具推荐】个人本地 markdown 知识图谱软件 Obsidian

Read MHD and raw images, slice, normalize and save them
随机推荐
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.7 实例(存储分配程序)
acwing796 子矩阵的和
Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)
Statistical machine learning code set
同源?跨域?如何解决跨域?
[weekly replay] game 80 of leetcode
acwing794 高精度除法
Global and Chinese market of soft capsule manufacturing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
第一章 线性表
统计机器学习代码合集
面试:‘==‘与equals()之间的区别
acwing 797 差分
Step by step to create a trial version of ABAP program containing custom screen
Super detailed dry goods! Docker+pxc+haproxy build a MySQL Cluster with high availability and strong consistency
What is fintech? How fintech can help small businesses succeed
Global and Chinese markets of automatic glue applicators 2022-2028: Research Report on technology, participants, trends, market size and share
写代码也有本手俗手之分,而我们要善于发现妙手!
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.1 文件描述符
Batch --03---cmdutil