#FT0000160. Tổng hình chữ nhật với ma trận thay đổi (Range Sum Query 2D – Mutable)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng hình chữ nhật với ma trận thay đổi (Range Sum Query 2D – Mutable)
Range Sum Query 2D – Mutable
Source: LeetCode
Version: Phuoc Hung OJ – class methods converted to SET/SUM commands.
Problem Statement
You are given a mutable integer matrix with rows and columns, zero-based coordinates. Process commands in order:
SET r c v: replace the current value of cell by (this is not an addition).SUM r1 c1 r2 c2: output the sum of all matrix entries in the closed rectangle from through .
Every query is evaluated after all preceding updates.
Input
The first line contains . The next lines each contain integers. Then follow lines of SET r c v or SUM r1 c1 r2 c2.
Output
For each SUM command, output the rectangle sum on a separate line in query order.
Subtasks
- Subtask 1 (20 points): ; ; ; ;
SETcoordinates , ;SUMbounds , . - Subtask 2 (30 points): ; ; ; ;
SETcoordinates , ;SUMbounds , . - Subtask 3 (50 points): ; ; ; ;
SETcoordinates , ;SUMbounds , .
Examples
Example 1
Input
5 5 3
3 0 1 4 2
5 6 3 2 1
1 2 0 1 5
4 1 0 1 7
1 0 3 0 5
SUM 2 1 4 3
SET 3 2 2
SUM 2 1 4 3
Output
8
10
Explanation
The first query sums columns 1 through 3 of rows 2, 3 and 4: . Updating cell (3,2) from 0 to 2 increases the queried sum by 2, yielding .
Example 2
Input
2 3 5
1 -2 3
4 0 -1
SUM 0 0 1 2
SET 0 1 6
SUM 0 1 1 2
SET 1 2 -5
SUM 1 0 1 2
Output
5
8
-1
Explanation
The initial total is . Setting (0,1) to 6 gives over columns 1–2. The last update makes row 1 equal to (4,0,-5), whose sum is .