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

#FT0000228. Cây lồng cây – thống kê thứ tự trên đoạn (Tree of Trees)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cây lồng cây – thống kê thứ tự trên đoạn (Tree of Trees)

Tree of Trees

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n of nonnegative integers, indexed from 11. Elements may be modified as operations are performed.

Support the following five operation types, processing them in input order:

  • 1 l r k: Output the rank of value kk within [l,r][l,r], defined as the number of elements strictly smaller than kk in that interval plus 11. The value kk need not occur in the interval.
  • 2 l r k: Output the kk-th smallest value in [l,r][l,r], with duplicate values counted as separate entries.
  • 3 pos k: Set apos=ka_{pos}=k.
  • 4 l r k: Output the largest value strictly less than kk in [l,r][l,r] (the predecessor). If it does not exist, output exactly -2147483647.
  • 5 l r k: Output the smallest value strictly greater than kk in [l,r][l,r] (the successor). If it does not exist, output exactly 2147483647.

Each interval is inclusive at both ends. Distinguish the rank of a value (type 1) from the value of a given rank (type 2).

Input

  • The first line contains two integers n,mn,m, the array length and operation count.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in index order.
  • Each of the next mm lines starts with opt from {1,2,3,4,5}\{1,2,3,4,5\}.
  • If opt is 1, 2, 4 or 5, it is followed by three integers l r k.
  • If opt is 3, it is followed by two integers pos k.

Output

For each operation of type 1, 2, 4 or 5, output one integer on its own line, in operation order. Use the exact sentinel values -2147483647 and 2147483647 for missing predecessors and successors respectively. A type-3 assignment prints nothing.

Subtasks

  • Subtask 1 (20 points): 1≤n≤501\le n\le 50; 1≤m≤801\le m\le 80; 0≤ai≤1080\le a_i\le10^8 for every ii and after every assignment; for types 1, 2, 4, 5: 1≤l≤r≤n1\le l\le r\le n; for types 1, 4, 5: 0≤k≤1080\le k\le10^8; for type 2: 1≤k≤r−l+11\le k\le r-l+1; for type 3: 1≤pos≤n1\le pos\le n and 0≤k≤1080\le k\le10^8.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤m≤30001\le m\le 3000; 0≤ai≤1080\le a_i\le10^8 for every ii and after every assignment; for types 1, 2, 4, 5: 1≤l≤r≤n1\le l\le r\le n; for types 1, 4, 5: 0≤k≤1080\le k\le10^8; for type 2: 1≤k≤r−l+11\le k\le r-l+1; for type 3: 1≤pos≤n1\le pos\le n and 0≤k≤1080\le k\le10^8.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤m≤2000001\le m\le 200000; 0≤ai≤1080\le a_i\le10^8 for every ii and after every assignment; for types 1, 2, 4, 5: 1≤l≤r≤n1\le l\le r\le n; for types 1, 4, 5: 0≤k≤1080\le k\le10^8; for type 2: 1≤k≤r−l+11\le k\le r-l+1; for type 3: 1≤pos≤n1\le pos\le n and 0≤k≤1080\le k\le10^8.

Examples

Input

9 6
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5

Output

2
4
3
4
9

Explanation

The initial array is [4,2,2,1,9,4,0,1,1][4,2,2,1,9,4,0,1,1].

  • 2 1 4 3: Sorting [4,2,2,1][4,2,2,1] gives [1,2,2,4][1,2,2,4]; the third value is 22.
  • 3 4 10: Set the fourth element to 1010, resulting in [4,2,2,10,9,4,0,1,1][4,2,2,10,9,4,0,1,1].
  • 2 1 4 3: Sorting the new prefix [4,2,2,10][4,2,2,10] gives [2,2,4,10][2,2,4,10], with third value 44.
  • 1 2 5 9: In [2,2,10,9][2,2,10,9], exactly two elements are strictly less than 99, so its rank is 33.
  • 4 3 9 5: In the interval [2,10,9,4,0,1,1][2,10,9,4,0,1,1], the greatest value below 55 is 44.
  • 5 2 8 5: In the interval [2,2,10,9,4,0,1][2,2,10,9,4,0,1], the smallest value above 55 is 99.

The five answers are therefore 2, 4, 3, 4, 9.

Example 2

Input

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

Output

-2147483647
2147483647
2
2

Explanation

In [1,2,3][1,2,3], no element is smaller than 11, so type 4 must print -2147483647. No element is larger than 33, so type 5 prints 2147483647. The rank of 22 is 22 because only one element is strictly smaller; the second-smallest value is also 22.