当前位置:网站首页>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;
}
边栏推荐
- Understanding of dart typedef
- leetcode-54. Spiral matrix JS
- Escape analysis of golang compiler
- 为什么阿里巴巴不建议MySQL使用Text类型?
- Glibc memory management model frees C library memory cache
- Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment
- Keep an IT training diary 067- good people are grateful, bad people are right
- < 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(六)
- 面试:什么是浅拷贝、深拷贝?
- C regular expression
猜你喜欢

Why doesn't Alibaba recommend MySQL use the text type?

Let's talk about events. Listen to those things. - Part one

Analysis on the development status and direction of China's cultural tourism real estate industry in 2021: the average transaction price has increased, and cultural tourism projects continue to innova

Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment

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

Decision tree classification and examples

读取mhd、raw图像并切片、归一化、保存

为什么阿里巴巴不建议MySQL使用Text类型?

Project training of Software College of Shandong University rendering engine system basic renderer (6)

leetcode-54. Spiral matrix JS
随机推荐
Global and Chinese market of soft capsule manufacturing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of three-phase induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
Getting started with JMeter
What is fintech? How fintech can help small businesses succeed
Divide training set, test set and verification set
Job submission instructions upload jobs to network disk
学习记录[email protected]一文搞懂canvas
Data analysis | kmeans data analysis
[tool recommendation] personal local markdown knowledge map software
File uploading and downloading in SSM
Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)
Introduction and download website of common data of GIS, remote sensing, hydrology and Geography (2), supplementary~
超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群
Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)
任务 输出密雪冰城主题曲 0612
[automation] kolla Based Automated Deployment CEPH cluster
From K-means to capsule
Huawei equipment is configured with CE dual attribution
Install RHEL 7/8 (red hat) virtual machine (Reprint)
Global and Chinese market of medical ECG telemetry equipment 2022-2028: Research Report on technology, participants, trends, market size and share