当前位置:网站首页>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;
}
边栏推荐
- 鼻孔插灯,智商上升,风靡硅谷,3万就成
- [automation] kolla Based Automated Deployment CEPH cluster
- Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
- Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
- 借助SpotBugs将程序错误扼杀在摇篮中
- 一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤试读版
- 第一章 线性表
- 位运算例题(待续)
- Scanpy(六)空间转录组数据的分析与可视化
- Explore the Apache shardingsphere SQL parse format function
猜你喜欢
![Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]](/img/48/714f1712f4c2d727dd49cbc6631abf.jpg)
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]

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

Office VR porn, coquettish operation! The father of Microsoft hololens resigns!

The small flying page is upgraded to be intelligent and the bug repair is faster

IDEA中文棱形乱码错误解决方法--控制台中文输出棱形乱码

Understanding of dart typedef

Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)

Golang collaboration scheduling (I): Collaboration Status

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

Saga体系结构模式:微服务架构下跨服务事务的实现
随机推荐
任务 输出密雪冰城主题曲 0612
【周赛复盘】LeetCode第80场双周赛
5-5配置Mysql复制 基于日志点的复制
Using the CSDN markdown editor
【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
Apache kylin Adventure
Instructions de soumission des tâches télécharger les tâches sur le disque réseau
UE4 常用类型转换
redis 通用命令
Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment
为什么阿里巴巴不建议MySQL使用Text类型?
puppeteer入门之 Page 类
Go Net Library (to be continued)
面试:hashCode()和equals()
Task output: dense snow ice city theme song 0612
[browser principle] variable promotion
Project training of Software College of Shandong University rendering engine system basic renderer (6)
glibc 内存管理模型 释放 C库内存缓存
Getting started with JMeter
VIM installation and common commands