#FT0000222. Truy vấn phần tử lớn thứ k trên các đa tập (K-th Largest Number Query)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 multisets of integers, numbered from through . Initially, all multisets are empty. Unlike ordinary sets, a multiset may contain multiple copies of the same value.
Process operations in the given order:
1 l r c: Insert one copy of integer into each multiset numbered through . An existing copy does not prevent another insertion.2 l r k: Form the multiset union of multisets through , 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 -th largest element.
For example, the multiset union of and has the sorted representation ; its third-largest value is .
Every type 2 query is guaranteed to have an existing -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 , the number of multisets and operations.
- Each of the next lines contains four integers, either
1 l r cor2 l r k. - For either type, . In type
1, is the possibly negative value to insert. In type2, is the one-based rank in descending order.
Output
For each type 2 query, print one line containing the value of the -th largest element in the multiset union at that moment. Repeated copies occupy distinct ranks.
Subtasks
- Subtask 1 (20 points): ; ; every operation has ; type
1 l r chas ; type2 l r khas and the -th largest element is guaranteed to exist. - Subtask 2 (30 points): ; ; every operation has ; type
1 l r chas ; type2 l r khas and the -th largest element is guaranteed to exist. - Subtask 3 (50 points): ; ; every operation has ; type
1 l r chas ; type2 l r khas and the -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 into each; the second puts into each. They both become . Querying multiset 1 for rank 2 gives ; querying it for rank 1 gives . Their multiset union is in descending order, so its third-largest value is .
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, and . The union of all three is in descending order, so its third-largest value is . Inserting into multisets 1 and 2 gives a sorted union for those two multisets; its fourth-largest value is . Finally, alone is , so its second-largest value is . All outputs are -2.