Acwing798.差分矩阵

前缀和与差分 图文并茂 超详细整理(全网最通俗易懂)_前缀和差分_林小鹿@的博客-CSDN博客

代码展示:

cpp 复制代码
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 1e3 + 10;
int a[N][N], b[N][N];
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()
{
    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];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            insert(i, j, i, j, a[i][j]);      //构建差分数组
        }
    }
    while (q--)
    {
        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++)
        {
            printf("%d ", b[i][j]);
        }
        printf("\n");
    }
    return 0;
}
相关推荐
LYFlied几秒前
【每日算法】LeetCode 22. 括号生成
数据结构·算法·leetcode·面试·职场和发展
fengche19151 分钟前
【无标题】keilC编译器版本问题,低版本
c++
桓琰1 分钟前
非线性滤波——基于EKF的INS/GPS松组合算法的研究(直接法|EKF|欧拉角)
算法·matlab·卡尔曼滤波算法
想自律的露西西★2 分钟前
js.39. 组合总和
前端·javascript·数据结构·算法
johnny2332 分钟前
Raft算法理解
算法
zore_c6 分钟前
【数据结构】栈——超详解!!!(包含栈的实现)
c语言·开发语言·数据结构·经验分享·笔记·算法·链表
Chen--Xing7 分钟前
LeetCode 15.三数之和
c++·python·算法·leetcode·rust
月明长歌10 分钟前
【码道初阶】【Leetcode105&106】用遍历序列还原二叉树:前序+中序、后序+中序的统一套路与“先建哪边”的坑
java·开发语言·数据结构·算法·leetcode·二叉树
iAkuya13 分钟前
(leetcode)力扣100 16除自身以外数组的乘积(预处理前项后项积)
数据结构·算法·leetcode
fantasy5_519 分钟前
C++ 智能指针深度解析:原理、实现与实战避坑
java·开发语言·c++