Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/kesishma-yuzi

Intersection area

Medium 1000 ms 64 MB 42%
Share

Two rectangles with sides parallel to the coordinate axes are given on the plane. The first has its lower-left corner at $(x_{1}; y_{1})$ and upper-right corner at $(x_{2}; y_{2})$; the second at $(x_{3}; y_{3})$ and $(x_{4}; y_{4})$. Find the area of their common part. If there is no common part, or it is only a segment or a point, the area is $0$.

Two overlapping rectangles on the coordinate plane; their common part highlighted

Input

The first line has four integers: $x_{1}$, $y_{1}$, $x_{2}$ and $y_{2}$.
The second line has four integers: $x_{3}$, $y_{3}$, $x_{4}$ and $y_{4}$.

Output

One integer — the area of the common part.

Note

$-10^{6} \le x_{1} \lt x_{2} \le 10^{6}$
$-10^{6} \le y_{1} \lt y_{2} \le 10^{6}$
$-10^{6} \le x_{3} \lt x_{4} \le 10^{6}$
$-10^{6} \le y_{3} \lt y_{4} \le 10^{6}$

Examples

Input #1
0 0 4 3
2 1 6 5
Output #1
4
Input #2
0 0 2 2
3 0 5 2
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.