很简单:c++题解 | #【模板】二维前缀和#

【模板】二维前缀和

https://www.nowcoder.com/practice/99eb8040d116414ea3296467ce81cbbc

  • 求二维前缀和矩阵
    公式:mat[i][j] = mat[i-1][j]+mat[i][j-1]-mat[i-1][j-1] +
  • 求解给定矩形范围的数字和
    公式:ans(x1,y1,x2,y2) = mat[x2][y2]-mat[x2][y1-1]-mat[x1-1][y2]+mat[x1-1][y1-1]
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1010;
typedef long long LL ;
int n,m,q;
LL mat[N][N];
int main() {
    scanf("%d%d%d", &n,&m,&q);
    int tmp;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            scanf("%d", &tmp);
            mat[i][j] = mat[i-1][j]+mat[i][j-1]-mat[i-1][j-1]+tmp;
            
        }
    }
    int x1, y1, x2, y2;
    for(int i = 1; i <= q; i++)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        printf("%lld\n", mat[x2][y2]-mat[x2][y1-1]-mat[x1-1][y2]+mat[x1-1][y1-1]);
    }


    return 0;
}
// 64 位输出请用 printf("%lld")
全部评论
大佬,我有点搞不懂为什么是[x][y]不是二维数组前面的[]代表行吗也就是y轴,后面[]列也就是x轴
点赞 回复 分享
发布于 2023-10-19 15:47 陕西

相关推荐

2025-12-14 11:43
黑龙江大学 Java
用微笑面对困难:确实比较烂,可以这么修改:加上大学的qs排名,然后大学简介要写一些,然后硕士大学加大加粗,科研经历第一句话都写上在复旦大学时,主要负责xxxx,简历左上角把学校logo写上,建议用复旦大学的简历模板
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务