QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 1024 MB 总分: 100 可 Hack ✓

#14503. 蟲洞

统计

在遙遠的銀河系中,有一個名為「星域秩序局」的組織,他們肩負著維護宇宙穩定的使命。為了守護銀河的和平,星域秩序局必須掌控那些潛藏在空間中的不穩定區域——蟲洞。

星域秩序局一共探索出了 $n$ 個蟲洞,每個蟲洞可以用一個一維空間坐標區間 $[l_i, r_i]$ 表示,即第 $i$ 個蟲洞的範圍從 $l_i$ 到 $r_i$。

星域秩序局需要從已知的 $n$ 個蟲洞中,選取一段連續的子序列 $[L, R]$,並且控制這個區間內的蟲洞。為了穩定地控制這些蟲洞,他們必須將其劃分為不超過 $k$ 個組,並且要求同一組內的蟲洞區間互不相交。形式化地,對於同一組內的任意兩個蟲洞 $[l_i, r_i]$ 和 $[l_j, r_j]$,必須滿足 $r_i < l_j$ 或 $r_j < l_i$。

星域秩序局希望儘可能控制更多的蟲洞。請你計算,他們所能選擇的蟲洞序列 $[L, R]$ 的最大長度(即 $R - L + 1$)。

輸入格式

本題有多組數據。第一行一個正整數 $T$ ($1 \le T \le 10^4$),表示數據組數。

對於每組數據: 第一行兩個整數 $n, k$ ($1 \le k \le n \le 2 \times 10^5$)。 接下來 $n$ 行,第 $i$ 行兩個整數 $l_i, r_i$ ($1 \le l_i \le r_i \le n$),表示第 $i$ 個蟲洞的坐標範圍。

保證 $T$ 組數據中 $n$ 的和不超過 $2 \times 10^5$。

輸出格式

對於每組數據: 輸出一行一個整數,表示最大的 $R - L + 1$。

範例

範例 1 輸入

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

範例 1 輸出

1
4

說明

對於第一組數據: 顯然只能選擇長度為 1 的蟲洞序列。

對於第二組數據: 可以選擇蟲洞序列 $[2, 5]$,將第 2 個蟲洞與第 4 個蟲洞分為一組,將第 3 個蟲洞與第 5 個蟲洞分為另一組,該蟲洞序列長度為 4。顯然不存在長度為 5 的方案。故答案為 4。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.