#include

1731: 矩阵(前缀和)

1731: 矩阵

时间限制: 3 Sec  内存限制: 128 MB
提交: 920  解决: 173
[提交][状态][讨论版][命题人:admin]

题目描述

输入

输出

样例输入

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

样例输出

2
21
55

提示

/*
M x y value 修改 第 x 行 y 列  的值为 value
Q x1 , y1 , x2 , y2 查询 x1 , y1 , x2 , y2 围成的子矩阵的所有元素的和

先对每一行计算一下前缀和
查询的时候只需枚举行
修改的时候只需针对某一行枚举列
*/

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std ;

#define maxn 1100
#define LL long long
int t ; 
int n , m  , Q; 

int num[maxn][maxn] , sum[maxn][maxn] ; 

void init(){
    memset(num , 0 , sizeof(num)) ; 
    memset(sum , 0 , sizeof(sum)) ; 
    for(int i=1 ; i<=n ; i++){
        for(int j=1 ; j<=m ; j++){
            num[i][j] = i+j ; 
            sum[i][j] = num[i][j] + sum[i][j-1] ; 
        }
    }
    return;
}

int main(){
    cin >> t ; 

    while(t--){
        cin >> n >> m >> Q ; 
        init() ; 
        char select ; 
        while(Q--){
            cin >> select ;  
            
            if(select == 'Q'){
                int  x1 , y1 , x2 , y2 ;
                cin >> x1 >> y1 >> x2 >> y2 ;
                LL result = 0 ; 
                for(int i=x1 ; i<=x2 ; i++){
                    result += sum[i][y2] - sum[i][y1-1] ; 
                }
                cout << result << endl ; 
            }else if(select == 'M'){
                int x , y , value ;
                cin >> x >> y >> value ; 
                num[x][y] = value ; 
                for(int j=y ; j<=m ; j++){
                    sum[x][j] = num[x][j] + sum[x][j-1] ; 
                }
            }
        }
    }
    return 0 ; 
} 

 

posted @ 2018-04-25 13:00  0一叶0知秋0  阅读(228)  评论(0编辑  收藏  举报