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

#FT0000031. Truy vấn dãy số theo ngưỡng (Sequence Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn dãy số theo ngưỡng (Sequence Query)

Sequence Query

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Initially, sequence AA is empty. You may append positive integers to it; duplicate values are allowed, and every occurrence contributes separately to order-statistic queries.

Process QQ commands of three types:

  • 1 x: add an element of value xx to AA.
  • 2 x k: among existing elements at most xx, report the kk-th largest value, counting duplicates. Print -1 if fewer than kk such elements exist.
  • 3 x k: among existing elements at least xx, report the kk-th smallest value, counting duplicates. Print -1 if there are not enough such elements.

All commands are executed in input order. Types 2,32,3 do not change the sequence.

Input

  • Line 1: integer QQ.
  • Next QQ lines: an operation in one of the formats 1 x, 2 x k, or 3 x k; for the last two formats, kk is the rank within the values satisfying threshold xx.

Output

For every type 22 or 33 query, print one integer on a separate line: the requested value, or -1 if too few elements exist, in query order. Type 11 produces no output.

Subtasks

Subtask 1 (20 points):

  • 1≤Q≤201\le Q\le 20.
  • 1≤x≤10181\le x\le 10^{18} in all commands.
  • t∈{1,2,3}t\in\{1,2,3\} is the command type.
  • 1≤k≤51\le k\le 5 in every type 22 or 33 command.
  • The sequence is initially empty and permits repeated values.

Subtask 2 (30 points):

  • 1≤Q≤30001\le Q\le 3000.
  • 1≤x≤10181\le x\le 10^{18} in all commands.
  • t∈{1,2,3}t\in\{1,2,3\} is the command type.
  • 1≤k≤51\le k\le 5 in every type 22 or 33 command.
  • The sequence is initially empty and permits repeated values.

Subtask 3 (50 points):

  • 1≤Q≤2000001\le Q\le 200000.
  • 1≤x≤10181\le x\le 10^{18} in all commands.
  • t∈{1,2,3}t\in\{1,2,3\} is the command type.
  • 1≤k≤51\le k\le 5 in every type 22 or 33 command.
  • The sequence is initially empty and permits repeated values.

Examples

Input

11
1 20
1 10
1 30
1 20
3 15 1
3 15 2
3 15 3
3 15 4
2 100 5
1 1
2 100 5

Output

20
20
30
-1
-1
1

Explanation

After four insertions, the sequence contains [20,10,30,20][20,10,30,20], or [10,20,20,30][10,20,20,30] in sorted order.

  • The three queries 3 15 1, 3 15 2, 3 15 3 consider only values at least 1515, namely [20,20,30][20,20,30], producing 20, 20, and 30. The two copies of 2020 count separately.
  • 3 15 4 requests a fourth element from a three-element group, producing -1.
  • 2 100 5 includes all four values but asks for rank five, so print -1.
  • 1 1 adds 11. The five values in descending order are now [30,20,20,10,1][30,20,20,10,1], and 2 100 5 returns 1.