AcWing 796. 子矩阵的和

这个题的重点是仿照一维的数组,所以a[N][N]也是从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]);
        }
    }
}
相关推荐
Code_Shark5 分钟前
AtCoder Beginner Contest 424 题解
数据结构·c++·算法·数学建模·青少年编程
CS创新实验室11 分钟前
深入解析快速排序(Quicksort):从原理到实践
数据结构·算法·排序算法·快速排序
Theodore_10222 小时前
深度学习(3)神经网络
人工智能·深度学习·神经网络·算法·机器学习·计算机视觉
地平线开发者2 小时前
大模型 | VLA 初识及在自动驾驶场景中的应用
算法·自动驾驶
py有趣2 小时前
LeetCode学习之实现strStr()
学习·算法·leetcode
夏鹏今天学习了吗2 小时前
【LeetCode热题100(52/100)】课程表
算法·leetcode·职场和发展
祁思妙想2 小时前
【LeetCode100】--- 101.重排链表【思维导图+复习回顾】
算法·leetcode·链表
文火冰糖的硅基工坊3 小时前
[人工智能-大模型-69]:模型层技术 - 计算机处理问题的几大分支:数值型性问题、非数值型问题?
算法·决策树·机器学习
OG one.Z4 小时前
05_逻辑回归
算法·机器学习·逻辑回归
lzptouch4 小时前
逻辑斯蒂回归(Logistic Regression)算法
算法·数据挖掘·回归