PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000078. Đếm giao điểm các đoạn ngang dọc (Intersection Points)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Đếm giao điểm các đoạn ngang dọc (Intersection Points)

Intersection Points

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn horizontal or vertical segments, count their intersection points. No two parallel segments intersect, and no segment endpoint is an intersection point.

Input

The first line contains nn; each following line contains x1,y1,x2,y2x_1,y_1,x_2,y_2 describing a segment.

Output

Print the number of intersection points.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; −106≤x1≤x2≤106-10^6\le x_1\le x_2\le10^6; −106≤y1≤y2≤106-10^6\le y_1\le y_2\le10^6; endpoints differ; every segment is horizontal or vertical; parallel segments do not intersect; no endpoint is an intersection.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; −106≤x1≤x2≤106-10^6\le x_1\le x_2\le10^6; −106≤y1≤y2≤106-10^6\le y_1\le y_2\le10^6; endpoints differ; every segment is horizontal or vertical; parallel segments do not intersect; no endpoint is an intersection.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; −106≤x1≤x2≤106-10^6\le x_1\le x_2\le10^6; −106≤y1≤y2≤106-10^6\le y_1\le y_2\le10^6; endpoints differ; every segment is horizontal or vertical; parallel segments do not intersect; no endpoint is an intersection.

Examples

Input

3
2 3 7 3
3 1 3 5
6 2 6 6

Output

2

Explanation

The horizontal segment at y=3y=3 intersects the vertical segments at x=3x=3 and x=6x=6. Both intersections are strictly inside the segments; the answer is 22.