当前位置:网站首页>acwing796 子矩阵的和
acwing796 子矩阵的和
2022-06-12 16:08:00 【_刘小雨】
输入一个 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
二维差分的求法: 减去两个矩阵的面积,然后加上重复减去的面积
S(i,j) 如何计算: S(i ,j) = S(i-1, j) + S(i, j - 1) - S(i-1, j -1) + a(i , j)
#include <iostream>
using namespace std;
const int N = 1010;
int a[N][N], s[N][N];
int n,m,q;
int main()
{
cin >> n >> m >> q;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
cin >> 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;
cin >> x1 >> y1 >> x2 >> y2;
int re = s[x2][y2]-s[x1-1][y2] - s[x2][y1-1] + s[x1-1][y1-1]; // 子矩阵和
cout << re <<endl;
}
return 0;
}
边栏推荐
- Step by step steps to create an ABAP program with a custom screen
- Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]
- Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
- 联通网管协议框图
- 从斐波那契数列求和想到的俗手、本手和妙手
- 连续八年包装饮用水市占率第一,这个品牌DTC是如何持续增长的?
- (四)GoogleNet复现
- Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)
- Decision tree classification and examples
- The nohup command uses
猜你喜欢

当编程纳入到高考。。。

RTOS rt-thread裸机系统与多线程系统

办公室VR黄片,骚操作!微软HoloLens之父辞职!

With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough

Explore the Apache shardingsphere SQL parse format function

Apache kylin Adventure

鼻孔插灯,智商上升,风靡硅谷,3万就成

Application of postman-rest client plug-in

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)

Multimix:从医学图像中进行的少量监督,可解释的多任务学习
随机推荐
Instructions de soumission des tâches télécharger les tâches sur le disque réseau
Understanding of dart typedef
TS 22.011
RTOS rt-thread裸机系统与多线程系统
办公室VR黄片,骚操作!微软HoloLens之父辞职!
How to analyze the running time and CPU utilization of Go programs?
Difference between tinyint and int
nohup 命令使用
2022.02.28 - SX11-05. The largest rectangle in the histogram
[practical case of light source] UV-LED curing innovation makes the production line more smooth
Go Net Library (to be continued)
C packing and unpacking
go net库(待续)
FPGA (III) trigger and latch
Global and Chinese markets of automatic glue applicators 2022-2028: Research Report on technology, participants, trends, market size and share
Why doesn't Alibaba recommend MySQL use the text type?
【工具推荐】个人本地 markdown 知识图谱软件 Obsidian
Defer learning in golang
Application of postman-rest client plug-in
Axure RP 9 for MAC (interactive product prototyping tool) Chinese version