Hide

Problem I
Manhattan Interference

In LOneLand, citizens have many options for telecom companies. Due to government regulations, each telecom company must have exactly two cell phone towers. A cell phone registered with a company will find the closest tower by Manhattan distance (also known as the L1-norm) and connect to that tower. The Manhattan distance between two points $(x_1, y_1)$ and $(x_2, y_2)$ is $|x_1 - x_2| + |y_1 - y_2|$.

If a cell phone is equidistant from both of its cell towers by Manhattan distance, then the device experiences Manhattan Interference and cannot connect to either tower. To improve the reliability of their phones, the citizens of LOneLand equip their devices with SIM cards from two different companies. Unfortunately, if a cell phone is ever at a point where both SIM cards experience Manhattan interference, the phone will explode!

Your task is to count the number of pairs of telecom companies for which equipping SIM cards from both companies is dangerous. That is, count the number of pairs of telecom companies for which there exists at least one point (not necessarily with integer coordinates) where a phone equipped with SIM cards from both companies would explode. Two pairs are considered different if there is a company in one pair that is not in the other.

\includegraphics[width=0.6\textwidth ]{sample1.png}
Figure 1: Illustration of the sample case.

Input

The first line of input contains a single integer $n$ ($2 \leq n \leq 1.5 \cdot 10^5$), the number of telecom companies.

Each of the next $n$ lines contains four integers $x_1$, $y_1$, $x_2$, $y_2$ ($-10^8 \le x_1, y_1, x_2, y_2 \le 10^8$), representing the coordinates of the two towers of one telecom company located at $(x_1, y_1)$ and $(x_2, y_2)$. All tower locations are distinct.

Output

Output a single integer, the number of pairs of telecom companies for which equipping SIM cards from both companies is dangerous.

Explanation of Sample Case 1

There are two pairs of telecom companies that should be counted:

  1. Companies $1$ and $3$: A phone will explode at point $(6, 1)$ if it connects to both companies, because it is $7$ units away from both towers of company $1$ and $6$ units away from both towers of company $3$.

  2. Companies $2$ and $3$: A phone will explode at point $(7, 12)$ if it connects to both companies.

Sample Input 1 Sample Output 1
3
0 0 1 3
10 10 11 13
4 5 9 4
2

Please log in to submit a solution to this problem

Log in