luogu#P8592. 『JROI-8』颅脑损伤 2.0(加强版)

『JROI-8』颅脑损伤 2.0(加强版)

Background

Note the special time limit of this problem.

Normal Version.

Problem Description

Given nn line segments, where the ii-th segment is [li,ri][l_i, r_i], color each segment either red or black, such that:

  1. Any two red segments do not intersect.
  2. Any black segment intersects with at least one red segment.

Minimize the total length of the red segments, and output this total length.

The length of a segment [li,ri][l_i, r_i] is defined as rilir_i - l_i. Two segments [li,ri][l_i, r_i] and [lj,rj][l_j, r_j] intersect if and only if there exists k[li,ri]k \in [l_i, r_i] and k[lj,rj]k \in [l_j, r_j].

Input Format

The first line contains a positive integer nn.

The next nn lines each contain two integers li,ril_i, r_i, separated by a space.

Output Format

Output one non-negative integer: the minimum possible total length of the red segments.

5
-6 5
1 3
-4 9
-1 10
6 8

4

Hint

Constraints

Test Point ID nn \le
1101 \sim 10 5×1055 \times 10^5

For all testdata, 109li<ri109-10^9 \le l_i < r_i \le 10^9.

This problem uses bundled tests.

Translated by ChatGPT 5