#C8209. Sum of Unique Integers in a Subgrid

    ID: 52166 Type: Default 1000ms 256MiB

Sum of Unique Integers in a Subgrid

Sum of Unique Integers in a Subgrid

You are given a rectangular grid (matrix) of integers with dimensions \(m \times n\). The grid is 1-indexed, meaning that the top-left cell is at \( (1,1) \) and the bottom-right cell is at \( (m,n) \).

You are also given four integers \(x_1, y_1, x_2, y_2\) which represent the coordinates of the top-left and bottom-right corners, respectively, of a subgrid. Your task is to compute the sum of all unique integers within this subgrid.

Note: The coordinates satisfy \(1 \le x_1 \le x_2 \le m\) and \(1 \le y_1 \le y_2 \le n\). The subgrid includes all elements \(grid[i][j]\) where \(x_1 \le i \le x_2\) and \(y_1 \le j \le y_2\).

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two integers \(m\) and \(n\) representing the number of rows and columns in the grid.
  • The next \(m\) lines each contain \(n\) space-separated integers representing the rows of the grid.
  • The last line contains four space-separated integers: \(x_1\), \(y_1\), \(x_2\), \(y_2\), defining the subgrid.

outputFormat

Output a single integer to stdout, which is the sum of the unique integers in the specified subgrid.

## sample
3 4
1 2 3 4
2 1 2 3
3 2 1 4
1 1 2 3
6