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]);
        }
    }
}
相关推荐
sel_93 分钟前
【强化学习】Hands-on Modern RL项目实践|OPD 算法完整解析
人工智能·深度学习·算法·机器学习·语言模型
Navigator_Z42 分钟前
LeetCode //C - 1209. Remove All Adjacent Duplicates in String II
c语言·算法·leetcode
ShineWinsu3 小时前
对于 C++:C++14中从变量模板、泛型 Lambda 到并发与字面量的解析
c++·算法
土司大王3 小时前
LeetCode hot100——合并两个有序链表
算法·leetcode·链表
码行山野赴时序归途3 小时前
算法效率的度量(上):时间复杂度中的对数思维
c语言·数据结构·算法
算AI4 小时前
基于LLM的无人机仿真测试:新方法竞赛夺佳绩
人工智能·深度学习·算法·机器学习·ai
kaixin_啊啊5 小时前
专用优化算法LKH
算法
_Narcissus_6 小时前
二分算法笔记及例题
数据结构·c++·笔记·算法·蓝桥杯·查找·二分算法
tachibana26 小时前
RAGAS 指标解读
数据库·人工智能·算法·机器学习·架构·大模型·llm