Matrix multiplication 2 (codevs3147)

Source: Internet
Author: User

Title Description Description
A matrix of two n*n is given, and M is queried for the values of the given sub-matrices in their product.

Enter a description input Description
The first row of two positive integers n,m.
The next n rows, each row n nonnegative integers, represent the first matrix.
The next n rows, each row n nonnegative integers, represent the second matrix.
Next M line, four positive integers per line a,b,c,d, asks for the product of the first matrix and the second matrix,
The element and in the sub-matrix of the vertex as a row b in column A and section C of Line D.

Outputs description Output Description
For each query, the output line is an integer that represents the answer to the query.
Sample input to sample
3 2
1 9 8
3 2 0
1 8 3
9 8 4
0 5 15
1 9 6
1 1 3 3
2 3 1 2

Sample output Sample Outputs
661
388
Data Size & Hint
"Data size and conventions"

For 30% of the data met, N <= 100.
For 100% of data satisfied, n <= 2000,m <= 50000, input data matrix elements < 100,a,b,
C,d <= N.
Exercises
Although the title is matrix multiplication, there is no relationship between the fast power and the matrix.
30% procedure:
Directly two matrix violence is multiplied, and then the violence is questioned. (PS: Training team's problem to give so much violence points)
100% procedure:
If you know enough about matrix multiplication, you can see that we can actually process each query in the complexity of O (n).
The matrix is a (X1,X2,Y1,Y2), the first matrix is a, the second matrix is B then the matrix A can be calculated by branch
Suppose the element of the first row (x1) of a matrix is p[i];
So according to the law of matrix multiplication
P[i] equals the elements of the X1 line of the A matrix and the elements of column I of the b matrix are multiplied, then added.
Sigma{p[i]} (y1<=i<=y2) is multiplied by the elements of line X1 of the A matrix, respectively, and the elements of the B matrix Y1 through the Y2 column, and then added.
Then we can see that this formula can be used to propose the element of the X1 line of the A matrix using the law of multiplication, then the sum of the resulting product is multiplied by the first and second elements of the B matrix.
The second line is the same as the third row,
We will find that each column in the B matrix can be extracted from the same
Then the sum of the matrices of the last query is multiplied by the elements of each row of the x1-x2 line of the A matrix and the elements and correspondence of each column of column y1-y2 of the B matrix.
So as long as the prefix of each row of the A matrix is preprocessed and
Each column of the B-matrix is prefixed and can be
Time complexity O (n^2+n*m);
Note Use scanf or read-in optimizations.
Code:

#include <iostream>#include <cstdio>using namespace Std;#define FOR (i,n) for (int i=1;i<=n;i++)#define READ (x) scanf ("%d", &x);int n,m,x,y,xx,yy,aa[2001][2001]={0},bb[2001][2001]={0},a,b;int Main () {read (n); read (m);For (i,n) for (j,n)    {read (a);Aa[i][j]=aa[i-1][j]+a;    }For (i,n) for (j,n)        {read (b);bb[i][j]=bb[i][j-1]+b;        }For (i,m)       {Read (x); Read (y); Read ( xx); Read (yy) ;long long ans=0;if (x>xx) swap (X,XX);if (y>yy) swap (Y,YY);For (j,n)ans+= (Long Long) (Aa[xx][j]-aa[x-1][j]) * (Long Long) (bb[j][yy]-bb[j][y-1]);printf ("%lld\n", ans);       }}

Matrix multiplication 2 (codevs3147)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.