2023-8-14 子矩阵的和

题目链接:子矩阵的和

c++ 复制代码
#include <iostream>

using namespace std;

int n, m, q;
const int N = 1010;

int a[N][N], s[N][N];

int main ()
{
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m; j++)
            scanf("%d", &a[i][j]);
    // 求前缀和
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m; j ++)
            s[i][j] = s[i -1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
    // 求部分和
    while(q -- )
    {
        int x1, y1, x2, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        printf("%d\n", s[x2][y2] - s[x2][y1 - 1] - s[x1 -1][y2] + s[x1 - 1][y1 - 1]);
    }
    return 0;
            
}
相关推荐
老四啊laosi8 小时前
[C++进阶] 24. 哈希表封装unordered_map && unordered_set
c++·哈希表·封装·unordered_map·unordered_set
妙为9 小时前
银河麒麟V4下编译Qt5.12.12源码
c++·qt·国产化·osg3.6.5·osgearth3.2·银河麒麟v4
史迪仔011212 小时前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
会编程的土豆13 小时前
【数据结构与算法】再次全面了解LCS底层
开发语言·数据结构·c++·算法
低频电磁之道13 小时前
解决 Windows C++ DLL 导出类不可见的编译错误
c++·windows
君义_noip15 小时前
信息学奥赛一本通 4150:【GESP2509七级】⾦币收集 | 洛谷 P14078 [GESP202509 七级] 金币收集
c++·算法·gesp·信息学奥赛·csp-s
superior tigre15 小时前
NumPy 基础使用方法(基础+矩阵运算+Attention)
线性代数·矩阵·numpy
Ricky_Theseus15 小时前
静态链接与动态链接
c++
澈20716 小时前
双指针,数组去重
c++·算法
小辉同志16 小时前
207. 课程表
c++·算法·力扣·图论