Rectangular Reckonings

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 2.0s
Memory limit: 64M

Author:
Problem types

Given a square, 0-indexed, 2-d array of integers, of width and length N, and a separate set of Q subarrays, find the sum of all the elements in the original array that are a part of exactly a single subarray.

Each subarray is described using two separate indices, the "top-left" index, and the "bottom-right" index. This system makes the most sense if you think about the array like a grid with (0, 0) at the top-left and (N-1, N-1) at the bottom-right. Each subarray can then be thought of as a rectangle that fits inside the grid.

Input Specification

The first line contains a single integer N (1 \le N \le 1000), the size of both dimensions of the array.

The next N lines will each contain N integers, the contents of the array. These integers will all be between -100 and 100 inclusive.

The next line contains a single integer Q the number of subarrays.

The next Q (1 \le Q \le 1000) lines will contain four integers x_1, y_1, x_2, y_2, the top-left indices followed by the bottom-right indices of a subarray.

It can be noted that for all subarrays (0 \le x_1 \le x_2 < N) and (0 \le y_1 \le y_2 < N).

Output Specification

A single integer, the total sum of all the elements that are only in 1 subarray.

Subtasks

Sample Cases [10%]
Subtask 1 [20%]

Q = 1

Subtask 3 [70%]

No further restrictions.

Sample Input 1

4
1 2 3 4
5 6 7 8
9 1 1 0
1 3 5 1
1
1 1 2 3

Sample Output 1

23

Sample Input 2

4
1 2 3 4
5 6 7 8
9 1 1 0
1 3 5 1
2
1 1 2 3
2 1 3 3

Sample Output 2

19

Sample Visual

Here is the array that is used in both sample inputs, with the subarray 1 1 2 3 outlined in red and the subarray 2 1 3 3 outlined in green:

Visual


Comments

There are no comments at the moment.