Problems > Crossing Segments

Crossing Segments

Difficulty: 1700 6000 ms 128 MB

geometry

Count the pairs of line segments that intersect (touching at an endpoint or overlapping counts as intersecting).

Input: the first line has N; each of the next N lines has a segment x1 y1 x2 y2.

Output: the number of intersecting pairs.

Constraints:

  • 1 ≤ N ≤ 103
  • |coordinates| ≤ 104

Sample Tests

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

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.