#FT0000162. Tổng các hình chữ nhật (Matrix Summation)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng các hình chữ nhật (Matrix Summation)
Matrix Summation
Source: SPOJ
Version: Phuoc Hung OJ – the test-case count is removed; the END command is retained.
Problem Statement
An initially zero-filled integer matrix has zero-based coordinates. Process commands in the given order:
SET x y v: assign the cell the value , replacing its previous value.SUM x1 y1 x2 y2: output the sum of all cells in the inclusive rectangle , .END: terminate the command sequence, without producing output.
A SET operation is an assignment, not an increment.
Input
The first line gives . Subsequent lines contain SET x y v, SUM x1 y1 x2 y2 or END. The final line is END.
Output
For each SUM command, print one integer on its own line; SET and END produce no output.
Subtasks
- Subtask 1 (20 points): ; commands preceding
END; ;SETcoordinates ;SUMbounds and ; eachSUMfits signed 32-bit. - Subtask 2 (30 points): ; commands preceding
END; ;SETcoordinates ;SUMbounds and ; eachSUMfits signed 32-bit. - Subtask 3 (50 points): ; commands preceding
END; ;SETcoordinates ;SUMbounds and ; eachSUMfits signed 32-bit.
Examples
Example 1
Input
4
SET 0 0 1
SUM 0 0 3 3
SET 2 2 12
SUM 2 2 2 2
SUM 2 2 3 3
SUM 0 0 2 2
END
Output
1
12
12
13
Explanation
The matrix starts at zero. After setting (0,0) to 1 the whole-grid sum is 1. Setting (2,2) to 12 makes the next two rectangle sums equal 12. The final query contains both nonzero cells and returns .
Example 2
Input
3
SET 1 1 5
SUM 0 0 2 2
SET 1 1 2
SUM 0 0 2 2
SET 0 2 -3
SUM 0 1 1 2
END
Output
5
2
-1
Explanation
The second SET replaces 5 by 2; it does not add 2. The total changes from 5 to 2. After setting (0,2) to , the last queried rectangle contains and 2 and totals .