#B4216. Calculate the Total Covered Area of Two Rectangles on a Grid
Calculate the Total Covered Area of Two Rectangles on a Grid
Calculate the Total Covered Area of Two Rectangles on a Grid
You are given a huge grid with \(10^9\) rows and \(10^9\) columns, where each cell is a unit square. Two rectangular paper pieces are placed on this grid:
- The first rectangle has dimensions \(a \times b\) (length \(a\) and width \(b\)) and is placed with its top-left corner at the top-left cell of the grid.
- The second rectangle has dimensions \(c \times d\) (length \(c\) and width \(d\)) and is placed with its top-left corner at the cell on the \(x\)-th row and \(y\)-th column of the grid. Note that the paper is placed without any rotation or flipping.
Your task is to compute the total area covered by both rectangles. If the rectangles overlap, the overlapping area should only be counted once.
The first rectangle covers the cells from row \(1\) to row \(a\) and column \(1\) to column \(b\). The second rectangle covers the cells from row \(x\) to row \(x+c-1\) and column \(y\) to column \(y+d-1\). The overlapping area, if any, is given by:
\[ \text{overlap} = \max(0, \min(a, x+c-1) - \max(1, x) + 1) \times \max(0, \min(b, y+d-1) - \max(1, y) + 1). \]Thus, the total covered area is calculated as:
\[ \text{area} = a \times b + c \times d - \text{overlap}. \]</p>inputFormat
The input consists of a single line containing six space-separated integers: (a), (b), (c), (d), (x), and (y).
outputFormat
Output a single integer representing the total area covered by the two rectangles on the grid.
sample
2 4 5 3 2 3
21