当前位置:网站首页>[9. submatrix sum]
[9. submatrix sum]
2022-06-22 02:42:00 【Little silly bird_ coding】
Submatrix and ( Two dimensional prefixes and )
Ideas :
Calculate the sum of submatrix , For example (x1,y1) It's the upper left corner ,(x2,y2) Is the sum of the submatrix in the upper right corner .
Two problems need to be solved
- s [ i , j ] How to calculate
Line by line calculationS[i,j] = S[i-1,j] + S[i,j-1] - S[i-1,j-1] + a[i,j]
- With (x1,y1) It's the upper left corner ,(x2,y2) How to calculate the sum of all the numbers in the submatrix in the lower right corner
S[x2,y2] -S[x1-1,y2]-S[s2,y1-1]+S[x1-1,y1-1]
subject
Enter a n That's ok mm The integer matrix of columns , Input again q A asked , Each query contains four integers x1,y1,x2,y2 Represents the upper left and lower right coordinates of a submatrix .
For each query, the sum of all numbers in the output submatrix .
Input format
The first line contains three integers n,m,q
Next n That's ok , Each row contains m It's an integer , Represents an integer matrix .
Next q That's ok , Each line contains four integers x1,y1,x2,y2 A group of questions .
Output format
common q That's ok , Output one query result per line .
Data range
1 ≤ n ,m ≤ 1000
1 ≤ q ≤ 200000
1≤ x1 ≤ x2 ≤ n
1 ≤ y1 ≤ y2 ≤ m
−1000 ≤ The values of the elements in the matrix ≤ 1000sample input :
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 4sample output :
17 27 21
Code
#include <iostream> using namespace std; const int N = 1010; int n, m, q; int a[N][N],s[N][N]; int main() { scanf("%d%d%d",&n ,&m, &q); for (int i = 1; i <= n; i ++) // Assign value to array for (int j = 1; j <= m; j ++) scanf("%d",&a[i][j]); // Initialize prefix and array for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; // Output prefix and while (q --) { int x1,y1,x2,y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); printf("%d\n",s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]); } return 0; }
边栏推荐
- 从数据库的分类说起,一文了解图数据库
- Huayang smart rushes to Shenzhen Stock Exchange: it plans to raise 400million Fosun Weiying as a shareholder
- Word document to markdown document?
- fatal error: png++/png. Hpp: no that file or directory
- Review of mathematical knowledge: triple integral
- Official release of ideal L9: retail price of 459800 yuan will be delivered before the end of August
- Informer有什么
- Technical exploration: 360 digital subjects won the first place in the world in ICDAR OCR competition
- Which Amazon evaluation system is better?
- 基于xposed框架hook使用
猜你喜欢

Huayang smart rushes to Shenzhen Stock Exchange: it plans to raise 400million Fosun Weiying as a shareholder

Latest release: neo4j figure data science GDS 2.0 and aurads GA

Using neo4j sandbox to learn neo4j graph data science GDS

带你区分几种并行

Introduction to Apache ActiveMQ Artemis

Li Kou today's question 1108 IP address invalidation

Graphacademy course explanation: Fundamentals of neo4j graph data science

Chapter 25 digital watermarking technology based on Wavelet Transform

Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga

【6. 高精度乘法】
随机推荐
The brand, products and services are working together. What will Dongfeng Nissan do next?
No sleep at noon, sleepy in the afternoon
fatal error: png++/png. Hpp: no that file or directory
Wechat applet film and television comment exchange platform system graduation design (3) background function
postgresql根据时间字段的大小来取数
Unicode decodeerror appears: 'ASCII' codec can't decode byte 0xe9 in position 0: ordinal not in range solution
GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
智翔金泰冲刺科创板:年营收3919万亏损超3亿 拟募资40亿
Linxu modify the permissions of the folder so that everyone can access 777
How to use tensorboard add_ histogram
Anaconda historical version download
Relative references must start with either “/“, “./“, or “../“.
【一起上水硕系列】Day Two
MySQL recursively finds the tree structure. This method is very practical!
Annual special analysis of China Mobile Banking in 2022
Global exception handling
Asemi Schottky diode 1N5819 parameters, 1N5819 replacement, 1N5819 source
理想L9正式发布:8月底前开始交付 零售价45.98万元
Introduction to excellent verilog/fpga open source project (XXVII) - small CPU
Dynamically load assetdatabase of assets



