当前位置:网站首页>子矩阵的和
子矩阵的和
2022-07-23 23:54:00 【Ding Jiaxiong】
题目
输入一个 n 行 m 列的整数矩阵,再输入 q 个询问,每个询问包含四个整数 x1,y1,x2,y2,表示一个子矩阵的左上角坐标和右下角坐标。
对于每个询问输出子矩阵中所有数的和。
输入格式
第一行包含三个整数 n,m,q。
接下来 n 行,每行包含 m 个整数,表示整数矩阵。
接下来 q 行,每行包含四个整数 x1,y1,x2,y2,表示一组询问。
输出格式
共 q 行,每行输出一个询问的结果。
数据范围
1≤n,m≤1000,
1≤q≤200000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤矩阵内元素的值≤1000
输入样例:
3 4 3
1 7 2 4
3 6 2 8
2 1 2 3
1 1 2 2
2 1 3 4
1 3 3 4
输出样例:
17
27
21
思路分析



题解
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1010;
int n,m,q;
int a[N][N]; //原数组
int s[N][N]; //前缀和数组
int main(){
scanf("%d%d%d",&n,&m,&q);
//读入原矩阵
for(int i = 1;i <= n; i++){
for(int j = 1;j <= m; j ++){
scanf("%d",&a[i][j]);
//计算前缀和数组
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
while(q -- ){
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%d\n",s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);
}
return 0;
}

边栏推荐
- No wonder the application effect of ERP in domestic enterprises is generally not ideal
- 深度学习之 9 前馈神经网络 基本概念
- Is it safe to find an account manager to open a fund account online??
- Analysis and resolution of slot conflict in solid delegatecall
- ciscn_ 2019_ n_ one
- [computer three-level information security] access control model
- cmake 编译工具小记
- Space shooting lesson 07: add graphics
- 第三章、组织代码
- 最长递增子序列变种[深刻理解最长递增序列]
猜你喜欢
随机推荐
[for loop if conditional statement] summary
BUUCTF -rip
Copy the customer service wechat, go to wechat to add, and make a call
树形DP
云原生的概念
BUUCTF -rip
链表——206. 反转链表(这题很重要)
pwn1_sctf_2016
Operating system not found solution after importing ISO into virtual machine
关于使用 Jackson 解析 JSON 你需要知道的一切
warmup_ csaw_ two thousand and sixteen
第五章、实现Web适配器
太空射击 第07课: 添加图形
Data driven excel reading and writing
Windows软件:如何安装Mysql5.7并配置环境变量
DDD thinking structure learning
DGS之文件上传
Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework
[Fifth space 2019 finals]pwn5
ACM模式的输入输出









![[OGeek2019]babyrop](/img/7a/18e8b985629488346e596cdf2a215c.png)