human
I found an interesting problem on https://codeforces.com/problemset/problem/1278/D:
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 $n$ segments $[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]$, $l_i < r_i$ for every $i$. 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 $n$ vertices from these segments. Vertices $v$ and $u$ are connected by an edge if and only if segments $[l_v, r_v]$ and $[l_u, r_u]$ intersect and neither of it lies fully inside the other one.

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

Determine if the resulting graph is a tree or not.

-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of segments.

The $i$-th of the next $n$ lines contain the description of the $i$-th segment — two integers $l_i$ and $r_i$ ($1 \le l_i < r_i \le 2n$).

It is guaranteed that all segments borders are pairwise distinct. 

-----Output-----

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

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

Output
YES

Input
5
1 3
2 4
5 9
6 8
7 10

Output
NO

Input
5
5 8
3 6
2 9
7 10
1 4

Output
NO


-----Note-----

The graph corresponding to the first example:

[Image]

The graph corresponding to the second example:

[Image]

The graph corresponding to the third example:

[Image]

I tried it in Python, but could not do it. Can you solve it?

 Give me a Python code to determine if the resulting graph is a tree or not?
assistant
#Here is Python code to determine if the resulting graph is a tree or not.

import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

class BIT_RSQ():
    def __init__(self, n):
        self.n = n
        self.data = [0]*(n+2)

    def add(self, i, v):
        while i <= self.n:
            self.data[i] += v
            i += i & -i

    def sum(self, i):
        ret = 0
        while(i > 0):
            ret += self.data[i]
            i -= i & -i
        return ret

    def query(self, l, r):
        return self.sum(r) - self.sum(l-1)

    def lowerBound(self, w):
        if w <= 0: return 0
        x, k = 0, 2**self.n.bit_length()
        while k:
            if x+k <= self.n and self.data[x+k] < w:
                w -= self.data[x+k]
                x += k
            k >>= 1
        return x + 1

n = int(input())
edges = [0]*(2*n)
c = [0]*(2*n)
BIT = BIT_RSQ(2*n)

uf = [-1]*n
def root(x):
    if uf[x] < 0:
        return x
    uf[x] = root(uf[x])
    return uf[x]
def unite(x,y):
    rx, ry = root(x), root(y)
    if rx == ry:
        return False
    if uf[rx] > uf[ry]:
        rx, ry = ry, rx
    uf[rx] += uf[ry]
    uf[ry] = rx
    return True

for i in range(n):
    a,b = list(map(int, input().split()))
    a,b = a-1,b-1
    c[a] = c[b] = i
    edges[a] = b
    edges[b] = b

for i in reversed(list(range(2*n))):
    j = edges[i]
    if j == i:
        BIT.add(j+1, 1)
    else:
        BIT.add(j+1, -1)
        cnt = BIT.sum(j+1)
        while cnt:
            k = BIT.lowerBound(cnt)
            if not unite(c[j], c[k-1]):
                print("NO")
                return
            cnt -= 1
if sum(i<0 for i in uf) == 1:
    print("YES")
else:
    print("NO")