AcWing 796. 子矩阵的和

这个题的重点是仿照一维的数组,所以aNN也是从1索引开始的。画个图举个例子就非常清晰了

之所以不好理解是因为没画格子,一个格子代表一个点,就很好理解了。

java代码:

java 复制代码
import java.io.*;
public class Main{
    static int N = 1010;
    static int[][] arr = new int[N][N];
    static int[][] s = new int[N][N];
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str1 = br.readLine().split(" ");
        int n = Integer.parseInt(str1[0]);
        int m = Integer.parseInt(str1[1]);
        int q = Integer.parseInt(str1[2]);
        for(int i = 1; i <= n; i++){
            String[] str2 = br.readLine().split(" ");
            for(int j = 1; j <= m; j++){
                arr[i][j] = Integer.parseInt(str2[j-1]);
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m;j++){
                s[i][j] = arr[i][j] + s[i-1][j] + s[i][j-1] - s[i-1][j-1];
            }
        }
        for(int i = 0 ; i < q ; i++){
        String[] str3 = br.readLine().split(" ");
        int x1 = Integer.parseInt(str3[0]);
        int y1 = Integer.parseInt(str3[1]);
        int x2 = Integer.parseInt(str3[2]);
        int y2 = Integer.parseInt(str3[3]);
        System.out.println(s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);
        }
    }
}
相关推荐
核数聚18 分钟前
【赛迪专访核数聚】深耕数据治理,打通数据孤岛夯实 AI 发展根基
大数据·人工智能·算法
小许同学记录成长36 分钟前
相对辐射定标技术文档
图像处理·算法
白白白小纯1 小时前
算法篇—返回倒数第k个节点
c语言·数据结构·算法·leetcode
小羊先生car1 小时前
RTOS-F429-HAL-(动/静态)任务的创建(2026/7/27)
开发语言·算法·c#
无忧.芙桃1 小时前
数据结构之堆
c语言·数据结构·c++·算法·
小蒋学算法2 小时前
算法-删除元素后最大固定点数目-典型最长增长序列算法
数据结构·算法
RD_daoyi2 小时前
Google偷偷给AI引用加了五个新功能:内联引用、悬停预览、品牌展示……但点击率真的回来了吗?
大数据·网络·人工智能·算法·安全·搜索引擎
船漏了就会沉2 小时前
后缀自动机(SAM):字符串处理的“万能瑞士军刀”
算法
白白白小纯3 小时前
算法篇—链表的中间节点
c语言·数据结构·算法·leetcode
Frostnova丶3 小时前
(19)LeetCode 54. 螺旋矩阵
算法·leetcode·矩阵