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

#FT0000029. Tập hợp có thứ tự – Sáu thao tác (Ordered Set)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tập hợp có thứ tự – Sáu thao tác (Ordered Set)

Ordered Set

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a set SS of NN distinct non-negative integers, initially listed in strictly increasing order. The set changes after updates, but no value can be present more than once.

Process QQ queries, each containing two integers t,xt,x:

  • 0 x: insert xx if absent.
  • 1 x: erase xx if present.
  • 2 x: output the xx-th smallest element, with ranks starting at 11, or -1 if ∣S∣<x|S|<x.
  • 3 x: count elements less than or equal to xx. Equal values count.
  • 4 x: output the largest element not exceeding xx, or -1 if none exists.
  • 5 x: output the smallest element not below xx, or -1 if none exists.

Queries are processed in order. Query operations inspect the current state without changing it.

Input

  • Line 1: integers N,QN,Q: initial set size and number of queries.
  • Line 2: NN strictly increasing distinct integers a0,a1,…,aN−1a_0,a_1,\ldots,a_{N-1}; this line is empty if N=0N=0.
  • Next QQ lines: exactly two integers t,xt,x, with query type tt in {0,1,2,3,4,5}\{0,1,2,3,4,5\}.

Output

For each type 2,3,4,52,3,4,5 query, print one integer on a separate line: the requested value or count; use -1 for missing answers to types 2,4,52,4,5. Types 0,10,1 produce no output.

Subtasks

Subtask 1 (20 points):

  • 0≤N≤120\le N\le 12.
  • 1≤Q≤201\le Q\le 20.
  • 0≤a0<a1<⋯<aN−1≤1090\le a_0<a_1<\cdots<a_{N-1}\le 10^9 if N>0N>0.
  • t∈{0,1,2,3,4,5}t\in\{0,1,2,3,4,5\} for all queries.
  • 0≤x≤1090\le x\le 10^9 for each query.
  • 1≤x≤1091\le x\le 10^9 when t=2t=2.
  • The initial set has NN distinct elements, without repetitions.

Subtask 2 (30 points):

  • 0≤N≤30000\le N\le 3000.
  • 1≤Q≤30001\le Q\le 3000.
  • 0≤a0<a1<⋯<aN−1≤1090\le a_0<a_1<\cdots<a_{N-1}\le 10^9 if N>0N>0.
  • t∈{0,1,2,3,4,5}t\in\{0,1,2,3,4,5\} for all queries.
  • 0≤x≤1090\le x\le 10^9 for each query.
  • 1≤x≤1091\le x\le 10^9 when t=2t=2.
  • The initial set has NN distinct elements, without repetitions.

Subtask 3 (50 points):

  • 0≤N≤5000000\le N\le 500000.
  • 1≤Q≤5000001\le Q\le 500000.
  • 0≤a0<a1<⋯<aN−1≤1090\le a_0<a_1<\cdots<a_{N-1}\le 10^9 if N>0N>0.
  • t∈{0,1,2,3,4,5}t\in\{0,1,2,3,4,5\} for all queries.
  • 0≤x≤1090\le x\le 10^9 for each query.
  • 1≤x≤1091\le x\le 10^9 when t=2t=2.
  • The initial set has NN distinct elements, without repetitions.

Examples

Input

3 12
10 20 30
3 19
3 20
4 19
4 20
5 21
2 2
0 0
2 1
0 0
1 20
3 20
2 4

Output

1
2
10
20
30
20
0
2
-1

Explanation

Initially S={10,20,30}S=\{10,20,30\}.

  1. 3 19 counts only 1010, producing 1.
  2. 3 20 includes 1010 and 2020, producing 2.
  3. 4 19 returns the greatest value at most 1919: 10.
  4. 4 20 includes equality, returning 20.
  5. 5 21 returns the least value at least 2121: 30.
  6. 2 2 selects the second value of [10,20,30][10,20,30]: 20.
  7. 0 0 inserts 00: now S={0,10,20,30}S=\{0,10,20,30\}.
  8. 2 1 returns the smallest element, 0.
  9. Another 0 0 is ignored because 00 is already present.
  10. 1 20 removes 2020, leaving S={0,10,30}S=\{0,10,30\}.
  11. 3 20 counts 00 and 1010, producing 2.
  12. 2 4 requests a fourth element of a three-element set, so output -1.