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

#FT0000041. Cây cân bằng thông thường (Normal Balanced Tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cây cân bằng thông thường (Normal Balanced Tree)

Normal Balanced Tree

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Maintain an initially empty multiset MM of integers. A value can occur more than once; each occurrence counts separately in order statistics. Process nn operations:

  • 1 x: insert one occurrence of xx.
  • 2 x: erase one occurrence of xx.
  • 3 x: output the rank of xx, defined as the number of elements strictly less than xx, plus one, even when xx is absent.
  • 4 k: output the value at rank kk in the sorted multiset, counting repeated values separately.
  • 5 x: output the strict predecessor of xx, i.e., the greatest value in MM strictly less than xx.
  • 6 x: output the strict successor of xx, i.e., the smallest value in MM strictly greater than xx.

For queries of types 4, 5, and 6, a valid answer is guaranteed. Query operations do not modify the multiset.

Input

  • Line 1: integer nn, the operation count.
  • Next nn lines: two integers op,xop,x per line; xx has the meaning defined by op. For op=4, xx is the rank kk.

Output

For each operation of type 3, 4, 5, or 6, print its integer answer on a separate line, preserving query order.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤301\le n\le 30; 1≤op≤61\le op\le 6; −107≤x≤107-10^7\le x\le 10^7 for types 1,2,3,5,6; for type 4, 1≤x≤∣M∣1\le x\le |M|; every deletion targets an existing occurrence; answers to types 4,5,6 always exist.

Subtask 2 (30 points):

  • 1≤n≤30001\le n\le 3000; 1≤op≤61\le op\le 6; −107≤x≤107-10^7\le x\le 10^7 for types 1,2,3,5,6; for type 4, 1≤x≤∣M∣1\le x\le |M|; every deletion targets an existing occurrence; answers to types 4,5,6 always exist.

Subtask 3 (50 points):

  • 1≤n≤1000001\le n\le 100000; 1≤op≤61\le op\le 6; −107≤x≤107-10^7\le x\le 10^7 for types 1,2,3,5,6; for type 4, 1≤x≤∣M∣1\le x\le |M|; every deletion targets an existing occurrence; answers to types 4,5,6 always exist.

Examples

Examples 1

Input

10
1 5
1 3
1 5
3 5
4 2
5 5
6 3
2 5
4 2
3 4

Output

2
5
3
5
5
2

Explanation

The first three operations produce sorted multiset [3,5,5][3,5,5]. 3 5 returns 1+1=21+1=2; 4 2 returns 55; strict predecessor 5 5 returns 33; strict successor 6 3 returns 55. Deleting one 55 leaves [3,5][3,5], so 4 2 returns 55. Finally, 3 4 returns rank 22.

Examples 2

Input

6
1 -2
1 0
1 -2
3 0
4 2
5 0

Output

3
-2
-2

Explanation

The sorted multiset is [−2,−2,0][-2,-2,0]. Two occurrences are less than 00, hence its rank is 33. The second smallest value is −2-2, and the greatest value strictly below 00 is also −2-2.

The two insertions of −2-2 remain as two separate copies. Operation 4 2 only queries the second element of [−2,−2,0][-2,-2,0]; it does not remove it. The multiset is therefore unchanged before 5 0, whose predecessor of 00 is −2-2.