DCEPC12E - End of Fun

Its holiday time after exams and Kappi and Pushap are enjoying to its fullest. But the sadist Sid, their teacher, can’t see them enjoy. Sad isn’t it? To ruin their fun, he gave them a programming problem to be solved within next 5 hours. He gives them 2 square matrices of equal dimension, Matrix A and a Matrix B and asks multiple queries on these matrices. The queries will be to change a particular element of one of the matrix and output the sum of elements of product of their matrix. Sid asks a lot of such queries. Kappi and Pushap don’t want the end of fun and so they ask you to solve this problem on behalf of them.

Input

First line consists of N, the dimension of matrix.

Each of the next N lines contains N space separated integers. This is matrix A.

Each of the next N lines contains N space separated integers. This is matrix B.

Next line contains Q, the number of queries asked by Sid.

Each of the next Q lines consists of queries of the form “A i j K” or “B i j K” (quotes for clarity), meaning change the element in ith row and jth column of matrix A or B to value K.

Output

Output exactly Q lines corresponding to Q queries, each containing the sum of the elements of the matrix A*B.

Example

Input:
2
1 2
3 4
4 3
2 1
3
A 1 1 2
B 0 1 3
A 0 0 10

Output:
40
40
103

Constraints

1 <= N <= 100
1 <= Q <= 100000
0 <= i, j < N
-10^6 <= A[i][j], B[i][j] <= 10^6


Added by:dce coders
Date:2013-12-07
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:C CSHARP C++ 4.3.2 CPP C99 HASK JAVA PAS-GPC PAS-FPC PYTHON PYTHON3 PY_NBC

hide comments
2022-08-05 17:41:37
One more test case:
3
13 -1 4
7 23 3
-5 2 23
4 5 -6
9 7 2
7 -11 3
3
A 1 1 2
B 0 2 3
A 2 0 10
69
204
384
2021-07-16 08:08:01
Enjoyed solving the problem ;-)
2016-07-03 22:42:58
take everything in long long int..int costed me 3 WA's
2016-06-11 11:00:17 sunil
O(N*N+Q).....easy one
2016-05-19 16:16:51 AD
I am again and again getting time limit exceeded, can anyone please help. I am just trying to calculate the product matrix , just finding the elements of the row affected, Here is the link to my code : <snip>

Last edit: 2022-06-17 23:12:01
2015-12-27 17:19:40 Emruz Hossain
Why time limit? My codes work on (N*N+Q).
<snip>

Last edit: 2022-06-17 23:12:16
2015-11-16 14:04:47 kejriwal
O(N*N + Q*N) ;).. nice question !!
2015-07-30 11:49:43 :.Mohib.:
Awsm que...!!
2015-06-26 17:09:12 Akshat Mathur
Minimize the time to solve each query.
2015-06-21 22:11:54 kartikay singh
WTF :P
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.