P8237 [AGM 2022 Qualification Round] Crossing the River

Description

A person comes to cross a river. The river can be viewed as a shape on a 2D coordinate system. The left bank where the person starts is the $y$-axis, and the right bank is the line $x=10^9+5$. There are $n$ horizontal logs on the river. The $i$-th log has $x$-coordinate $x_i$, and it covers points whose $y$-coordinate ranges from $y_{i,1}$ to $y_{i,2}$. The person can jump back and forth between logs and the banks. The $i$-th log can jump to the $j$-th log if and only if $x_i \not= x_j$ and there exists a real number $y$ such that $y_{i,1}

Input Format

The first line contains a positive integer $n$. The next $n$ lines each contain four integers $x_i,y_{i,1},y_{i,2},c_i$.

Output Format

Output one number representing the answer. It is guaranteed that there exists a way to reach the right bank.

Explanation/Hint

#### Constraints For $100\%$ of the testdata, $1\leq n\leq 2.5\times 10^5$, $1\leq x_i,c_i\leq 10^9$, $1\leq y_{i,1}\leq y_{i,2}\leq 10^9$. The testdata guarantees that logs do not overlap: there do not exist $i,j$ such that $x_i=x_j$ and $y_{i,2}>y_{j,1}$. #### Notes Translated from [AGM 2022 Qualification Round I River](https://judge.agm-contest.com/public/problems/14/text)。 Translated by ChatGPT 5