当前位置:网站首页>子矩阵的和
子矩阵的和
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;
}

边栏推荐
- jarvisoj_level0
- Practical learning of SQL statements
- [OGeek2019]babyrop
- ciscn_2019_c_1
- PHP(2)
- Splicing of.Net distribution with outlook mail format and table
- 473-82(40、662、31、98、189)
- Problems encountered in pytorch
- The world's smallest material ranking, Lingzi, Xianzi, quark
- Chapter 6: implement a persistence adapter
猜你喜欢

DGS的错误处理

【计算机三级信息安全】访问控制模型

Operating system not found solution after importing ISO into virtual machine
![Longest increasing subsequence variant [deep understanding of the longest increasing sequence]](/img/73/1480ec319a2860fec5667d6f2fb2ba.png)
Longest increasing subsequence variant [deep understanding of the longest increasing sequence]
QT | set part size sizehint, minimumsizehint, sizepolicy, stretch factor
solo 文章正文含有 &lt;&gt; 标签会影响到页面样式

BUUCTF -rip

No wonder the application effect of ERP in domestic enterprises is generally not ideal
![[CTF] Tiange team writeup - the first digital space security attack and defense competition (Preliminary)](/img/61/5547822b782043672b626f6b86d304.png)
[CTF] Tiange team writeup - the first digital space security attack and defense competition (Preliminary)

How to migrate databases in the flask framework
随机推荐
ciscn_2019_n_8
链表——206. 反转链表(这题很重要)
Network security class assignment
Chapter 6: implement a persistence adapter
DGS first acquaintance
ciscn_2019_n_1
STM32 can initialization details
pwn1_ sctf_ two thousand and sixteen
【三年面试五年模拟】算法工程师的独孤九剑秘籍(前六式汇总篇)V1版
Joinable and detached of pthread
云原生的概念
logback
正则表达式及绕过案例
PyTorch 中遇到的问题
DGS的错误处理
ubtun 更新源
网络安全课堂作业
ret2shellcode
The QT creation window is blocked and cannot be displayed in time
工具推荐-语雀