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]);
        }
    }
}
相关推荐
0+1117 分钟前
算法 --二分查找
c++·算法·leetcode
Omics Pro38 分钟前
新型条件传输模型!虚拟细胞扰动预测
数据库·人工智能·算法·机器学习·自然语言处理
黄金龙PLUS1 小时前
5个800比特大状态置换算法的设计与分析
算法·网络安全·密码学·哈希算法·同态加密
政企项目老覃1 小时前
大模型 Agent 自主任务编排:电商客服地址解析从 12% 失败率到 2.1% 的落地复盘
人工智能·程序人生·算法
kukubuzai1 小时前
双指针系列二--末尾篇(3道题)
c++·算法·leetcode
杜 硕2 小时前
单链表经典算法题
数据结构·算法
小O的算法实验室2 小时前
IEEE TCYB,着色旅行商问题:模型、求解与应用
算法
h_a_o777oah2 小时前
【图论】网络流:Dinic 算法模板实现原理及解题技巧
c++·算法·图论·acm·网络流·最小割·dinic算法
我不会起名字3222 小时前
一天一道力扣Hot100(37):深度优先算法--括号生成
java·数据结构·c++·后端·python·算法·go
foolishlee3 小时前
SCRAM-SHA-256
数据库·算法·postgresql