← 返回 linkedin 的题目列表SumTable: 2D Matrix Range Sum with Point Updates
类型:online_judge
Problem: SumTable (2D range sum with point updates)
You are given an integer matrix matrix and must process two kinds of operations:
set r c v: update matrix[r][c] to v.
get r1 r2 c1 c2: return the sum of a submatrix.
Input (stdin)
Line 1: integer R, number of rows.
Line 2: integer C, number of columns.
Next R lines: C integers per line for the initial matrix.
Next line: integer Q, number of queries.
Next Q lines: each is one query:
get r1 r2 c1 c2
set r c v
Based on the provided example, get r1 r2 c1 c2 sums rows in [r1, r2) and columns in [c1, c2) (inclusive-exclusive).
Output (stdout)
For each get query, print one integer (the submatrix sum) on its own line.
Example
Input:
2
3
1 2 3
4 5 6
5
get 0 2 1 3
set 0 1 10
get 0 2 1 3
set 0 0 -3
get 0 1 0 2
Output:
16
24
7
Constraints
Not specified in the post; typically you are expected to optimize for many updates and queries.
Example
Input
2
3
1 2 3
4 5 6
5
get 0 2 1 3
set 0 1 10
get 0 2 1 3
set 0 0 -3
get 0 1 0 2
Output
16
24
7