Checkerboard Summation (Easy)

View as PDF

Submit solution

Points: 12 (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). Up to 100\,000 of these cells have specified values written in them, and the rest are zeroes. 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 answer up to 100\,000 queries of the form:

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. Each line contains three space-separated integers R, C, and X (1 \le R \le M, 1 \le C \le N, -1\,000 \le X \le 1\,000), indicating that the value X is written in row R and column C. No cell will be given twice in the input. The last line is followed by a line containing three zeroes, signifying the end of this section of input.
Following this are a number of lines containing queries. Each line contains four space-separated integers: R_1, C_1, R_2, and C_2 (1 \le R_1 \le R_2 \le M, 1 \le C_1 \le C_2 \le N). Output the alternating sum of all squares contained within the box [R_1,R_2] \times [C_1,C_2], as described.

Output Specification

Print the answer to each query on its own line, in order.

Sample Input

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

Sample Output


Sample Explanation

The checkerboard is three cells by three cells. Here's what it looks like:

 0  5  0
 0  0 11
-2  0  0

We are asked to find the alternating sum of the second and third rows. The 11 is in a square of the same colour as (2,1), so it is added; -2 is on a square of opposite colour, so it is subtracted. 11-(-2) = 13.


There are no comments at the moment.