Checkerboard Summation (Hard)

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type

You are given a checkerboard with M rows and N columns (1 \le M,N \le 3\,000). Each square initially has the number zero written in it. For top-secret strategic reasons which are given out on a need-to-know basis only (and you do not, at the present moment, need to know) you must write a program that will perform two types of operation:
Modify: Given the row and column of a square on the checkerboard, change the number written in it to a new value.
Query: Given the coordinates of two squares on the checkerboard, find the alternating sum of all of the numbers within the rectangle delimited by those two squares. By alternating sum what is meant is that we add all numbers in squares with the same colour as the first square given, and subtract all numbers with the opposite colour.

Input Specification

The first line of the input file contains the integers M and N.
A number of input lines then follow. The integer at the beginning of the line signifies:
0: End of input.
1: A modify operation. Three integers follow: R, C, and X (1 \le R \le M,
1 \le C \le N, -2000 \le X \le 2000), indicating that the value X is to be written in row R and column C.
2: A query. Four integers follow: R1, C1, R2, and C2 (1 \le R1 \le R2 \le M,
1 \le C1 \le C2 \le N). Output the alternating sum of all squares contained within the box [R1,R2] \times [C1,C2], as described.
There will never be more than 100\,000 commands given.

Output Specification

For each query operation, print the answer on its own line.

Sample Input

3 3
1 1 2 5
1 3 1 -2
2 1 1 2 3
1 2 3 11
2 2 1 3 3
0

Sample Output

-5
13

Sample Explanation

The checkerboard is three cells by three cells. When the first query is executed, the board looks like this:

0  5  0
0  0  0
-2 0  0

We are asked to find the alternating sum of the first two rows. Since the 5 is on a square of opposite colour to the first square, it is subtracted to obtain an answer of -5. For the second query, the board looks like:

 0  5  0
 0  0 11
-2  0  0

Now, we take the alternating sum of the second and third rows. 11 is added, whereas -2 is subtracted, to give the answer 13.


Comments


  • 4
    Kirito  commented on Sept. 13, 2020, 9:49 p.m. edit 2

    Edit 2: Test data has been fixed. You can ignore the comments below.


    • 3
      ADMathNoob  commented on Sept. 16, 2020, 7:47 p.m.

      What does it mean when R = M + 1 or C = N + 1?


      • 3
        Kirito  commented on Sept. 16, 2020, 10:27 p.m.

        You should clip the values to R = M and C = N, respectively.

        (This is the off-by-one errors the previous edit of the above comment was referring to).