We consider a $n\times m$ matrix data, in order to read some of the data in the matrix, such as to find the elements of a $a\times b$ sub-matrix, and usually we can think of the $o (AB) $ traversal that sub-matrix, the various elements of its summation. However, when the $a$ or $b$ is very large, such calculations appear too slow, if the maximum element of the sub-matrix is required and (such as Ural 1146), it is simply too slow to endure. so I thought, can I read the data at the same time, I will first do some preprocessing, so that the calculation of the back, the time to simplify some of the steps? The answer is yes. There are usually two types of preprocessing for matrices:one is to flatten the matrix, which is to add the previous or previous row to the following column or line:
Press column compression ↑, purple ones and = brown ones and-orange ones andcompression by row ↑, purple ones and = brown ones and-orange ones andopen a two-dimensional array to hold the matrix, the $0$ row and the $0$ column are all placed $0$, our real matrix in the array subscript from the $1$ start. to pressIn the case of row compression, after reading the current element $t=a_{ij}$, we make $b_{ij}=a_{ij}+b_{i-1,j}$, and so on, when all the data is read, preprocessing is complete, $B _{ij}$ represents the $j$ column for the Matrix $a$, from the $1$ line to the $i $ line of the and. So if we want to know the matrix $a$ on column $j$, from line $p$ to row $q$ and, directly with $b_{qj}-b_{p-1,j}, (P<=Q) $ one step to find out, without the need for $q-p+1$ step calculation, then from the upper left corner $a_{ab}$ to the lower right corner $a_ {ij}$ sub-matrix element and is \ $sum =\SUM_{P=A}^{I}\SUM_{Q=B}^{J}A_{PQ}=\sum_{q=b}^{j}b_{iq}-\sum_{q=b}^{j}b_{a-1,q}\$, greatly reduces the computational amount and reduces the complexity of time.
read-in preprocessing ↑, right figure purple block =4+24+30-18=40calculation area and ↑, purple area and =60-12-15+3=36and the other is compressed onto an element, with $b_{ij}$ represents the $a_{11}$ from the leftmost element to the element $a_{ij}$ and $\sum_{p=1}^{i}\sum_{q=1}^{j}a_{ij}, (I \geq 1, J \geq 1) $, in order to maintain this nature, After we read into the current element $t=a_{ij}$, we make $b_{ij}=a_{ij}+b_{i,j-1}+b_{i-1,j}-b_{i-1,j-1}$. When all data is read in, preprocessing is complete. If we want to ask for afrom the upper-left corner of the $a_{ab}$ to the lower-right corner of the $a_{ij}$ sub-matrix elements and, only three steps to calculate: sum=$\SUM_{P=A}^{I}\SUM_{Q=B}^{J}A_{PQ}$= $B _{ij}-b_{i,b-1}-b_{a-1,j}+b_{a-1,b-1}$ to reduce the complexity of time to constant.
by Black Storm (using for Notes)
Preprocessing of matrix data in ACM