matrix

Topic description

enter

output

sample input

1
3 4 4
Q 1 1 1 1
Q 1 1 3 2
M 1 1 3
Q 1 1 3 4

Sample output

2
21
55
Ideas:

sum[i][j] represents the prefix sum of the first j numbers of the i-th row in array A, and each update only updates the prefix sum of each row.

(I thought about the prefix sum when I was doing this question, but I didn't do it. It was only after my classmate said his idea. It was too stupid orz.)

The sum of each block, using the prefix of each row and adding up row by row.

Worst time complexity: 20*1000*1000.

Code:

#include <iostream>
#include <cstring>
 
using namespace std;
const int N = 1010;
long long sum[N][N];
long long a[N][N];
 
intmain()
{
    int T;
    cin >> T;
    int n, m, q;
    char on;
    int x1, y1, x2, y2;
    int x, y, v;
 
    for(int t=1; t<=T; t++) {
        cin >> n >> m >> q;
 
        memset(sum, 0, sizeof(sum));
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=m; j++) {
                sum[i][j] = (i+1+i+j)*(j)/2; // initialize sum[][]
                a[i][j] = i+j;
            }
        }
 
        for(int i=1; i<=q; i++) {
            cin >> on;
            if (op == 'Q') {
             /*   for(int j=1; j<=n; j++) {
                    for(int k=1; k<=m; k++) {
                        cout << sum[j][k] << " ";
                    }
                    cout << endl;
                }*/
                long long ans = 0;
                cin >> x1 >> y1 >> x2 >> y2;
                for(int j=x1; j<=x2; j++) {
                    ans += sum[j][y2]-sum[j][y1-1];
                }
 
                cout << ans << endl;
            }
            else {
                cin >> x >> y >> v;
 
                for(int j=y; j<=m; j++) {
                    sum[x][j] += (v-a[x][y]);
                }
               a[x][y] = v;
            }
        }
    }
    return 0;
}
 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326076657&siteId=291194637