PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#BS0000165. Đếm cặp đoạn giao nhau (Intersecting Intervals)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm cặp đoạn giao nhau (Intersecting Intervals)
Intersecting Intervals
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given closed real intervals. Interval is with . Two intervals intersect if they share at least one real point, including the case when they only touch at an endpoint. Count pairs satisfying whose intervals intersect.
Input
- The first line contains integer .
- Each of the next lines contains integers and , the endpoints of interval .
Output
Print one integer: the number of pairs of intervals sharing at least one point.
Subtasks
- Subtask 1 (20 points): ; for all .
- Subtask 2 (30 points): ; for all .
- Subtask 3 (50 points): ; for all .
Examples
Input
3
1 5
7 8
3 7
Output
2
Explanation
The three intervals are , and . Pair is disjoint since . Pair intersects on . Pair shares the endpoint , which counts because both intervals are closed. Thus there are two intersecting pairs; print 2.