【zzulioj-1731】矩阵
题目描述
输入
输出
样例输入
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
提示
【题意】
给定一个矩阵,元素是行纵之和,Q a b c d 是查询以这4个数对角的矩阵和,M a b c是将a b坐标的元素赋值为c。
【思路】
记录前缀和
#include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { int x=0,c=getchar(),f=0; for(; c>'9'||c<'0'; f=c=='-',c=getchar()); for(; c>='0'&&c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; return f?-x:x; } int s[1010][1010]; int a[1010][1010]; int main() { int cas = read(); while(cas -- ) { int n = read() , m = read() , q = read(); for (int i=1; i<=n; i++) { a[i][0] = 0; for (int j=1; j<=m; j++) { s[i][j] = i + j; a[i][j] = a[i][j-1] + s[i][j]; } } while (q -- ) { char b[11]; scanf("%s", b); if (b[0] == 'Q') { int x1 = read() , y1 = read() , x2 = read() , y2 = read(); ll ans = 0; for (int i=x1; i<=x2; i++) ans += (ll)(a[i][y2]-a[i][y1-1]); printf("%lld\n", ans); } else if (b[0] == 'M') { int x = read() , y = read() , v = read(); s[x][y] = v; for (int j=y; j<=m; j++) a[x][j] = a[x][j-1] + s[x][j]; } } } return 0; }