import
java.util.*;
public
class
Main {
private
static
TreeMap<Integer, Integer>
createMap(
int
n,
int
[] arr)
{
TreeMap<Integer, Integer> pref =
new
TreeMap<>();
Arrays.sort(arr);
int
count =
0
;
for
(
int
i =
0
; i < n; i++) {
pref.put(arr[i], ++count);
}
return
pref;
}
private
static
int
pointsOfIntersection(
int
m,
int
[][] segments,
int
n,
TreeMap<Integer, Integer> pref)
{
int
ans =
0
;
for
(
int
i =
0
; i < m; i++) {
int
x1 = segments[i][
0
];
int
x2 = segments[i][
2
];
if
(x1 == x2) {
continue
;
}
if
(x1 > x2) {
int
temp = x1;
x1 = x2;
x2 = temp;
}
Map.Entry<Integer, Integer> it1
= pref.ceilingEntry(x1 +
1
);
Map.Entry<Integer, Integer> it2
= pref.floorEntry(x2 -
1
);
int
intersections =
0
;
if
(it1 ==
null
) {
intersections =
0
;
}
else
if
(it2 ==
null
) {
intersections = n - it1.getValue() +
1
;
}
else
{
intersections
= it2.getValue() - it1.getValue() +
1
;
}
ans += intersections;
}
return
ans;
}
public
static
void
main(String[] args)
{
int
N =
4
;
int
M =
8
;
int
[] lines = { -
5
, -
3
,
2
,
3
};
int
[][] segments
= { { -
2
,
5
,
5
, -
6
}, { -
5
, -
2
, -
3
, -
5
},
{ -
2
,
3
, -
6
,
1
}, { -
1
, -
3
,
4
,
2
},
{
2
,
5
,
2
,
1
}, {
4
,
5
,
4
, -
5
},
{ -
2
, -
4
,
5
,
3
}, {
1
,
2
, -
2
,
1
} };
TreeMap<Integer, Integer> pref
= createMap(N, lines);
System.out.println(
pointsOfIntersection(M, segments, N, pref));
}
}