当前位置:网站首页>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;
}
边栏推荐
- Object.keys遍历一个对象
- < 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(五)
- 小程序:如何在插件中获取用户手机号
- 学习记录[email protected]一文搞懂canvas
- Global and Chinese market of vascular prostheses 2022-2028: Research Report on technology, participants, trends, market size and share
- Project training of Software College of Shandong University rendering engine system basic renderer (6)
- Acwing high precision multiplication
- Scanpy (VI) analysis and visualization of spatial transcriptome data
- Kill program errors in the cradle with spotbugs
- Writing code can also be classified as "manual" or "vulgar", and we should be good at finding good hands!
猜你喜欢
Thread pool execution process
acwing 801. 二进制中1的个数(位运算)
Redis General Command
C packing and unpacking
Multimix:从医学图像中进行的少量监督,可解释的多任务学习
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
批量--04---移动构件
RTOS RT thread bare metal system and multi thread system
What is JUC in high concurrency programming
The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence
随机推荐
Sha6 of D to large integer
[thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.2 低级 I/O(read 和 write)
(四)GoogleNet複現
RTOS RT thread bare metal system and multi thread system
Interview: why do integer wrapper classes try to use equals() to compare sizes
acwing 2816. 判断子序列
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
【周赛复盘】LeetCode第80场双周赛
Keep an IT training diary 067- good people are grateful, bad people are right
小飞页升级变智能修复Bug更快速了
Divide training set, test set and verification set
What is JUC in high concurrency programming
[browser principle] variable promotion
Writing code can also be classified as "manual" or "vulgar", and we should be good at finding good hands!
leetcode-54. Spiral matrix JS
Interview: what are shallow copy and deep copy?
Glibc memory management model frees C library memory cache
Explore the Apache shardingsphere SQL parse format function
记一篇IT培训日记067-好人感恩,坏人无错