Geometry
Geometry-19
Geometry-20
Combin-01
~/algor $ cat problems/kesmalar-kesishishlari-soni
Number of segment intersections
Hard
2000 ms
128 MB
95%
$n$ segments are given on the plane, each either horizontal or vertical (of positive length). How many pairs consisting of a horizontal and a vertical segment have at least one common point (touching at endpoints also counts)? Pairs of two horizontal or two vertical segments are not counted.
Input
The first line contains a positive integer $n$. Each of the next $n$ lines contains four integers: $x_{1}$ $y_{1}$ $x_{2}$ $y_{2}$ — the endpoints of a segment ($x_{1} = x_{2}$ or $y_{1} = y_{2}$).
Output
One integer — the number of intersecting pairs.
Note
$1 \le n \le 10^{5}$
$-10^{9} \le x_{i}, y_{i} \le 10^{9}$
Examples
Input #1
4 0 2 6 2 2 0 2 5 5 1 5 2 1 4 4 4
Output #1
3
Input #2
2 0 0 3 0 5 -1 5 1
Output #2
0