PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000189. Số thứ K trong hợp các đoạn (K-th Number in the Union of Segments)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số thứ K trong hợp các đoạn (K-th Number in the Union of Segments)

K-th Number in the Union of Segments

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn closed integer segments [li,ri][l_i,r_i]. Segment ii contains every integer xx with li≤x≤ril_i\le x\le r_i, including both endpoints.

Write every integer from every segment into one list and sort the list in nondecreasing order. An integer belonging to multiple segments appears once for each such segment. The list has ∑i=1n(ri−li+1)\sum_{i=1}^{n}(r_i-l_i+1) elements.

Find the element at zero-based index kk in the sorted list. In particular, k=0k=0 refers to its smallest element.

Input

  • The first line contains integers nn and kk.
  • Each of the next nn lines contains lil_i, rir_i, describing segment ii.

Output

  • Print one integer: the value at zero-based position kk.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; −109≤li≤ri≤109-10^9\le l_i\le r_i\le 10^9 for every 1≤i≤n1\le i\le n; 0≤k<∑i=1n(ri−li+1)0\le k<\sum_{i=1}^n(r_i-l_i+1).
  • Subtask 2 (30 points): 1≤n≤50001\le n\le 5000; −109≤li≤ri≤109-10^9\le l_i\le r_i\le 10^9 for every 1≤i≤n1\le i\le n; 0≤k<∑i=1n(ri−li+1)0\le k<\sum_{i=1}^n(r_i-l_i+1).
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; −109≤li≤ri≤109-10^9\le l_i\le r_i\le 10^9 for every 1≤i≤n1\le i\le n; 0≤k<∑i=1n(ri−li+1)0\le k<\sum_{i=1}^n(r_i-l_i+1).

Examples

Input

3 4
-1 1
0 2
2 2

Output

1

Explanation

The segments are [−1,1][-1,1], [0,2][0,2], and [2,2][2,2]. They contribute −1,0,1,0,1,2,2-1,0,1,0,1,2,2. Sorting gives [−1,0,0,1,1,2,2][-1,0,0,1,1,2,2]. At zero-based index k=4k=4 (the fifth position), the value is 11.