Help me please, It’s my university task and I don’t know how to solve it, because I need fast solution.
Given two arrays a and b of length N consisting of integers. Consider a set of segments connecting points (0,ai) and (1,bi) for
1≤i≤N. Find the number of segments in this set that do not intersect with other segments.
Note that segments are considered intersecting if they have at least one common point. Therefore, segments with the same endpoint are considered intersecting.
Input Format:
The first line of the input contains a single integer
N (
1≤N≤3⋅10^5
) — the number of segments.
The next
N lines each contain two integers separated by a space —
ai
and
bi
(
1≤ai
,bi
≤2⋅N), defining the coordinates of the
i-th segment.
It is guaranteed that all segments given in the input are distinct, meaning that for
i
=j, at least one of the conditions
ai
=aj
and
bi!=bj
holds.
Output Format:
Output a single integer — the number of segments that do not intersect with any others.
Example 1:
Input:
5
1 4
2 5
3 1
4 5
5 6
Output
1
Example 2:
Input:
5
2 6
3 9
4 2
6 9
9 10
Output:
1
I tried some stupid method with a lot of “for” but it’s to long
Handcrafting Country is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.