#P8237. [AGM 2022 资格赛] 过河

[AGM 2022 资格赛] 过河

Problem 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 yy-axis, and the right bank is the line x=109+5x=10^9+5.

There are nn horizontal logs on the river. The ii-th log has xx-coordinate xix_i, and it covers points whose yy-coordinate ranges from yi,1y_{i,1} to yi,2y_{i,2}.

The person can jump back and forth between logs and the banks. The ii-th log can jump to the jj-th log if and only if xixjx_i \not= x_j and there exists a real number yy such that yi,1<y<yi,2,yj,1<y<yj,2y_{i,1}<y<y_{i,2},y_{j,1}<y<y_{j,2}, and there does not exist a log kk such that min(xi,xj)<xk<max(xi,xj)\min(x_i,x_j)<x_k<\max(x_i,x_j) and yk,1<y<yk,2y_{k,1}<y<y_{k,2}. Similarly, if a log wants to jump to a bank, treat the yy-range covered by the bank as (inf,inf)(-inf,inf), and the same condition must be satisfied.

The cost to jump from the ii-th log to anywhere else is cic_i. Jumping from a bank to anywhere else costs nothing. The left bank cannot jump directly to the right bank.

The person wants to know the minimum total cost to reach the right bank.

Input Format

The first line contains a positive integer nn.

The next nn lines each contain four integers xi,yi,1,yi,2,cix_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.

8
1 1 4 10
4 2 5 10
2 3 5 1
3 2 4 1
2 1 3 1
5 1 3 1
6 1 2 10
6 2 3 10
4

Hint

Constraints

For 100%100\% of the testdata, 1n2.5×1051\leq n\leq 2.5\times 10^5, 1xi,ci1091\leq x_i,c_i\leq 10^9, 1yi,1yi,21091\leq y_{i,1}\leq y_{i,2}\leq 10^9.

The testdata guarantees that logs do not overlap: there do not exist i,ji,j such that xi=xjx_i=x_j and yi,2>yj,1y_{i,2}>y_{j,1}.

Notes

Translated from AGM 2022 Qualification Round I River

Translated by ChatGPT 5