#P1712. [NOI2016] 区间

    ID: 675 远端评测题 1000~3000ms 250MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2016线段树NOI 系列离散化O2优化排序双指针,尺取法,two-pointer

[NOI2016] 区间

Description

On the number line, there are nn closed intervals numbered from 11 to nn. The ii-th closed interval is [li,ri][l_i, r_i].

Now we need to choose mm intervals such that these mm intervals share at least one position. In other words, there exists an xx such that for every selected interval [li,ri][l_i, r_i], we have lixril_i \leq x \leq r_i.

For a valid selection, its cost is the length of the longest selected interval minus the length of the shortest selected interval.

The length of an interval [li,ri][l_i, r_i] is defined as (rili)(r_i - l_i), i.e., the value of the right endpoint minus the value of the left endpoint.

Find the minimum cost among all valid selections. If no valid selection exists, output 1-1.

Input Format

The first line contains two integers, representing nn and mm.

From line 22 to line (n+1)(n + 1), each line contains two integers representing an interval. On line (i+1)(i + 1), the integers li,ril_i, r_i denote the left and right endpoints of the ii-th interval.

Output Format

Output a single integer on one line representing the answer.

6 3
3 5
1 2
3 4
2 2
1 5
1 4
2

Hint

Explanation for Sample Input/Output 1

As shown, when n=6n = 6, m=3m = 3, the minimum-cost plan is to select the three intervals [3,5],[3,4],[1,4][3, 5], [3, 4], [1, 4]. They all contain position 44, so the selection is valid. Among them, the longest interval is [1,4][1, 4], and the shortest interval is [3,4][3, 4], so the cost is (41)(43)=2(4 - 1) - (4 - 3) = 2.

Constraints and Conventions

::cute-table{tuack}

There are 20 test points in total. See the table below for details. | Test point ID | n= n= | m= m= | li,ri l_i, r_i | |:-:|:-:|:-:|:-:| | 1 | 20 20 | 9 9 | 0liri100 0 \le l_i \le r_i \le 100 | | 2 | ^ | 10 10 | 0liri100 0 \le l_i \le r_i \le 100 | | 3 | 199 199 | 3 3 | 0liri100000 0 \le l_i \le r_i \le 100000 | | 4 | 200 200 | ^ | ^ | | 5 | 1000 1000 | 2 2 | ^ | | 6 | 2000 2000 | ^ | ^ | | 7 | 199 199 | 60 60 | 0liri5000 0 \le l_i \le r_i \le 5000 | | 8 | 200 200 | 50 50 | ^ | | 9 | ^ | ^ | 0liri109 0 \le l_i \le r_i \le 10^9 | | 10 | 1999 1999 | 500 500 | 0liri5000 0 \le l_i \le r_i \le 5000 | | 11 | 2000 2000 | 400 400 | ^ | | 12 | ^ | 500 500 | 0liri109 0 \le l_i \le r_i \le 10^9 | | 13 | 30000 30000 | 2000 2000 | 0liri100000 0 \le l_i \le r_i \le 100000 | | 14 | 40000 40000 | 1000 1000 | ^ | | 15 | 50000 50000 | 15000 15000 | ^ | | 16 | 100000 100000 | 20000 20000 | ^ | | 17 | 200000 200000 | ^ | 0liri109 0 \le l_i \le r_i \le 10^9 | | 18 | 300000 300000 | 50000 50000 | ^ | | 19 | 400000 400000 | 90000 90000 | ^ | | 20 | 500000 500000 | 200000 200000 | ^ |

For all test points, it is guaranteed that 1mn1 \le m \le n, 1n5×1051 \le n \le 5 \times 10^5, 1m2×1051 \le m \le 2 \times 10^5, 0liri1090 \le l_i \le r_i \le 10^9.

Translated by ChatGPT 5