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

#FT0000222. Truy vấn phần tử lớn thứ k trên các đa tập (K-th Largest Number Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn phần tử lớn thứ k trên các đa tập (K-th Largest Number Query)

K-th Largest Number Query

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Maintain nn multisets of integers, numbered from 11 through nn. Initially, all multisets are empty. Unlike ordinary sets, a multiset may contain multiple copies of the same value.

Process mm operations in the given order:

  • 1 l r c: Insert one copy of integer cc into each multiset numbered ll through rr. An existing copy does not prevent another insertion.
  • 2 l r k: Form the multiset union of multisets ll through rr, keeping all copies from every contributing multiset. Sort these elements in nonincreasing order, treating repeated copies as occupying separate positions, and output the value of the kk-th largest element.

For example, the multiset union of {1,1,4}\{1,1,4\} and {5,1,4}\{5,1,4\} has the sorted representation {5,4,4,1,1,1}\{5,4,4,1,1,1\}; its third-largest value is 44.

Every type 2 query is guaranteed to have an existing kk-th largest element. Values may be negative, and repeated values must be preserved. Type 1 insertions affect all subsequent queries.

Input

  • The first line contains integers n,mn,m, the number of multisets and operations.
  • Each of the next mm lines contains four integers, either 1 l r c or 2 l r k.
  • For either type, 1≤l≤r≤n1\le l\le r\le n. In type 1, cc is the possibly negative value to insert. In type 2, kk is the one-based rank in descending order.

Output

For each type 2 query, print one line containing the value of the kk-th largest element in the multiset union at that moment. Repeated copies occupy distinct ranks.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; 1≤m≤401\le m\le 40; every operation has 1≤l≤r≤n1\le l\le r\le n; type 1 l r c has −n≤c≤n-n\le c\le n; type 2 l r k has 1≤k<2631\le k<2^{63} and the kk-th largest element is guaranteed to exist.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤m≤10001\le m\le 1000; every operation has 1≤l≤r≤n1\le l\le r\le n; type 1 l r c has −n≤c≤n-n\le c\le n; type 2 l r k has 1≤k<2631\le k<2^{63} and the kk-th largest element is guaranteed to exist.
  • Subtask 3 (50 points): 1≤n≤500001\le n\le 50000; 1≤m≤500001\le m\le 50000; every operation has 1≤l≤r≤n1\le l\le r\le n; type 1 l r c has −n≤c≤n-n\le c\le n; type 2 l r k has 1≤k<2631\le k<2^{63} and the kk-th largest element is guaranteed to exist.

Examples

Example 1

Input

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

Output

1
2
1

Explanation

Both multisets start empty. The first insertion puts 11 into each; the second puts 22 into each. They both become {1,2}\{1,2\}. Querying multiset 1 for rank 2 gives 11; querying it for rank 1 gives 22. Their multiset union is (2,2,1,1)(2,2,1,1) in descending order, so its third-largest value is 11.

Example 2

Input

3 6
1 1 3 -2
1 2 3 4
2 1 3 3
1 1 2 4
2 1 2 4
2 3 3 2

Output

-2
-2
-2

Explanation

After the first two insertions, S1={−2}S_1=\{-2\} and S2=S3={−2,4}S_2=S_3=\{-2,4\}. The union of all three is (4,4,−2,−2,−2)(4,4,-2,-2,-2) in descending order, so its third-largest value is −2-2. Inserting 44 into multisets 1 and 2 gives a sorted union (4,4,4,−2,−2)(4,4,4,-2,-2) for those two multisets; its fourth-largest value is −2-2. Finally, S3S_3 alone is (4,−2)(4,-2), so its second-largest value is −2-2. All outputs are -2.