当前位置:网站首页>acwing 798二维差分(差分矩阵)
acwing 798二维差分(差分矩阵)
2022-06-12 16:08:00 【_刘小雨】
输入一个 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
作用: 同一维差分,我们构造二维差分数组目的是为了 让原二维数组a中所选中子矩阵中的每一个元素加上c的操作,可以由O(n*n)的时间复杂度优化成O(1)
二维差分原理:同一维差分一样, 需要构造一个矩阵b, 使得原矩阵a 是它的前缀和矩阵。

如何求:
核心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;
// 核心, 插入函数
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]; //前缀和 等于后面求的矩阵
}
for(int i = 1; i<=n;i++)
{
for(int j=1;j<=m; j++)
{
cout << b[i][j] << " ";
}
cout << endl;
}
return 0;
}
边栏推荐
- 一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
- UDP summary (tcp/ip details volume 1/2)
- 记一篇IT培训日记067-好人感恩,坏人无错
- Chapter I linear table
- Match single character
- Saga architecture pattern: implementation of cross service transactions under microservice architecture
- Divide training set, test set and verification set
- Servlet knowledge explanation (2)
- 盒马,最能代表未来的零售
- < 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)
猜你喜欢

Why doesn't Alibaba recommend MySQL use the text type?

聊聊事件监听那些事-上

线程池执行流程
![[browser principle] variable promotion](/img/19/f6b26d97c6024893a21dd40e2bbc47.jpg)
[browser principle] variable promotion
![[practical case of light source] UV-LED curing innovation makes the production line more smooth](/img/6f/04f52a37782c54b1050f908f46eadf.png)
[practical case of light source] UV-LED curing innovation makes the production line more smooth

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

如何使用Grafana轻松实现OVL数据可视化

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)

Project training of Software College of Shandong University rendering engine system basic renderer (IV)

Solution of user and root forgetting password in virtual machine
随机推荐
Scanpy(六)空间转录组数据的分析与可视化
【工具推荐】个人本地 markdown 知识图谱软件 Obsidian
UDP summary (tcp/ip details volume 1/2)
国产CPLD中AG1280Q48进行开发的实践之一:思路分析
Scanpy (VI) analysis and visualization of spatial transcriptome data
Why doesn't Alibaba recommend MySQL use the text type?
Analysis on the development status and direction of China's cultural tourism real estate industry in 2021: the average transaction price has increased, and cultural tourism projects continue to innova
Glibc memory management model frees C library memory cache
Learning record [email protected] understand canvas
The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence
redis 通用命令
Escape rules and examples of go
2022.02.28 - SX11-05. The largest rectangle in the histogram
Development practice of ag1280q48 in domestic CPLD
Redis string type common commands
Go Net Library (to be continued)
作业提交说明 上传作业到网盘中
联通网管协议框图
面试:‘==‘与equals()之间的区别
[thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions