"Codevs 3147" Matrix multiplication 2 2012 (WTF)

Source: Internet
Author: User
Tags min time limit

3147 Matrix multiplication 2 2012
Time limit: 1 s space limit: 64000 KB title level: Master Master
Title Description Description
gives two n*n matrices, M asks the number of the given sub-matrix in their product Values and.
* To prevent card evaluation, the data range has been reduced and the time limit is lowered. The
input describes the first line of input Description
with two positive integer n,m.
the next n rows, with n non-negative integers per row, represent the first matrix.
the next n rows, with n non-negative integers per row, represent the second matrix. The
Next M line, four positive integers per line a,b,c,d, indicates that the first matrix is queried for the product of the second matrix, and the element in the sub-matrix of row B and column C of line A is the vertex of the node. The
output describes output Description
for each query, outputting an integer line that represents the answer to the query.
Sample Input
3 2
1 9 8
3 2 0
1 8 3
9 8 4
0 5 All
1 9 6
1 1 3 3
2 3 1 2
Sample Output
661
388
data range and hints for the database size & Hint
"Data size and conventions"
for 40% data satisfaction, n <= 100,m <= 1000. The
satisfies 100% of the data, n <= 800,m <= 10000, the matrix element in the input data < 100,a,b,c,d <= N. The
data has gradients.

"This problem is very deceptive, I just began to obediently write the moment multiplication, however, because the simple matrix multiplication is already O (n^3), so add O (n^2) query on T, and then after the moment multiply to maintain the entire matrix prefix and, the query operation to the O (n), but still T"
"This problem is not actually a matrix multiplication, it can be pushed through the formula."
"By pushing the formula, you can see that this problem only need to calculate a, b two array prefix and (a array for each column prefix and b array for each row prefix and), and finally in the loop m, with the prefix and subtract, the interval is calculated and multiplied, so that the computation sub-matrix and the time of the calculation of O (n), The complexity of the core program segment is O (nm), and it can be over. 】
"Note: Be careful with MLE"

∑I=X1X2∑J=Y1Y2C (i,j) \sum_{i={x_1}}^{x_2}\sum_{j={y_1}}^{y_2} C (I,j)
=∑I=X1X2∑J=Y1Y2∑K=1NA[I][K]XB[K][J] =\sum_{i={x_1}}^{x_2}\sum_{j={y_1}}^{y_2}\sum_{k=1}^n A[i][k]xb[k][j]
=∑K=1N∑I=X1X2A[I][K]∑J=Y1Y2B[K][J] =\sum_{k=1}^n\sum_{i={x_1}}^{x_2}a[i][k] \sum_{j={y_1}}^{y_2} B[k][j]

 #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
int a[2001][2001],b[2001][2001];
int n,m,x1,y1,x2,y2;
    int main () {int i,j;
    scanf ("%d%d", &n,&m);
          for (I=1;i<=n;++i) for (j=1;j<=n;++j) {scanf ("%d", &a[i][j]);    
        A[I][J]+=A[I-1][J];
        } for (I=1;i<=n;++i) for (j=1;j<=n;++j) {scanf ("%d", &b[i][j]);
      B[I][J]+=B[I][J-1];
      } for (I=1;i<=m;++i) {scanf ("%d%d%d%d", &x1,&y1,&x2,&y2); 
      Long Long s1,s2;
      Long Long ans=0;
            for (j=1;j<=n;j++) {S1=a[max (x1,x2)][j]-a[min (x1,x2) -1][j];
            S2=b[j][max (y1,y2)]-b[j][min (Y1,y2)-1];
        ANS=ANS+S1*S2; 
    } printf ("%lld\n", ans);      
} return 0; } 

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.