CF BUDDY
← Problems·

1278D · Segment Tree

2100 · data structures, dsu, graphs

Problem: As the name of the task implies, you are asked to do some work with segments and trees.

Recall that a tree is a connected undirected graph such that there is exactly one simple path between every pair of its vertices.

You are given nn segments [l1,r1],[l2,r2],,[ln,rn][l_1, r_1], [l_2, r_2], \dots, [l_n, r_n], li<ril_i < r_i for every ii. It is guaranteed that all segments' endpoints are integers, and all endpoints are unique — there is no pair of segments such that they start in the same point, end in the same point or one starts in the same point the other one ends.

Let's generate a graph with nn vertices from these segments. Vertices vv and uu are connected by an edge if and only if segments [lv,rv][l_v, r_v] and [lu,ru][l_u, r_u] intersect and neither of it lies fully inside the other one.

For example, pairs ([1,3],[2,4])([1, 3], [2, 4]) and ([5,10],[3,7])([5, 10], [3, 7]) will induce the edges but pairs ([1,2],[3,4])([1, 2], [3, 4]) and ([5,7],[3,10])([5, 7], [3, 10]) will not.

Determine if the resulting graph is a tree or not.

Input Format: The first line contains a single integer nn (1n51051 \le n \le 5 \cdot 10^5) — the number of segments.

The ii-th of the next nn lines contain the description of the ii-th segment — two integers lil_i and rir_i (1li<ri2n1 \le l_i < r_i \le 2n).

It is guaranteed that all segments borders are pairwise distinct.

Output Format: Print "YES" if the resulting graph is a tree and "NO" otherwise.

Note: The graph corresponding to the first example:

The graph corresponding to the second example:

The graph corresponding to the third example:

Sample Cases

Case 1

Input

6
9 12
2 11
1 3
6 10
5 7
4 8

Output

YES

Case 2

Input

5
1 3
2 4
5 9
6 8
7 10

Output

NO

Case 3

Input

5
5 8
3 6
2 9
7 10
1 4

Output

NO

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback