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]);
        }
    }
}
相关推荐
hetao173383727 分钟前
2026-09-27~28 hetao1733837 的刷题记录
c++·算法
Godspeed Zhao35 分钟前
现代智能雷达技术15——算法(1)
人工智能·算法·雷达
代码什么用38 分钟前
LeetCode-42. 接雨水
算法·leetcode
lengxuenong42 分钟前
枚举及其优化学习手册
c++·学习·算法·青少年编程
小O的算法实验室1 小时前
IEEE TAI,RoCo:基于角色的LLM协作实现自动启发式设计
算法
小范的技术工坊1 小时前
大模型蒸馏
人工智能·算法·数据挖掘·大模型
Benny_Tang1 小时前
P9527 [JOIST 2022] 洒水器 / Sprinkler 题解
c++·算法
kiracrimson2 小时前
希尔排序:让插入排序不再怕逆序
数据结构·算法·排序算法
yi0112 小时前
DAY17: LeetCode 139|单词拆分:从“把单词删掉”到用 DP 记录合法切口
人工智能·笔记·python·算法·leetcode·动态规划
陈希瑞2 小时前
LongCat-2.5-Preview 的web逆向能力实测
人工智能·算法·wasm