Square Pasture
InterviewTime limit2sMemory limit512 MB
Given two non-overlapping axis-aligned rectangles, find the smallest area of an axis-aligned square that covers both.
Problem
Farmer John wants to simplify the layout of his farm. Until now his cows grazed in two fenced rectangular pastures. Farmer John would like to replace them with a single square pasture. The new pasture has to cover every region that the two old fences enclosed, and it has to be the smallest square that does so.
The sides of the square pasture are parallel to the x and y axes, and it can be placed anywhere. Find the minimum area of a square that covers all the area the two rectangular pastures used to cover.
Input
The first line contains four space separated integers , , , describing one of the original pastures. All four values are between 0 and 10, inclusive. The lower left corner of that pasture is and the upper right corner is , with and .
The second line describes the second original pasture in the same format as the first line. The two pastures neither overlap nor touch.
Output
Print one line with the minimum area of a square pasture that covers every region the two rectangular pastures originally enclosed.
Hint
Consider the case where the first rectangle has corners and and the second one has corners and . A square fence of side length 7 with corners and covers both regions. A square of side length 6 cannot cover them, so 7 is the best possible. There are several valid placements for the square of side length 7, since it can be shifted a little up or down.