Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Taju Kage Bunshin No Jutsu

Limits: 3s, 512 MB

Naruto Uzumaki is one of the strongest ninja of RUET (Rasengan University of Ebisu and Tobi), possessing unnaturally huge amount of chakra (a kind of energy). Due to this, he can create a lot of shadow clones (duplicates of himself) with his Shadow Clone Jutsu. Right now, Naruto is bored so he’s just playing around with his shadow clones.

Naruto has already created N shadow clones and lined them up in a row. The i-th clone has Ci amount of chakra in it. Naruto can do the following operations in his whim.

  1. Add V– Makes a new shadow clone with V amount of chakra and adds him to the end of the line.
  2. Remove – Destroys the last shadow clone of the line.
  3. Query L R K – Finds the shadow clone with K-th largest chakra in [L, R] range i.e. the K-th shadow clone if you sort the clones with position between L and R in the line according to their chakra in descending order.

Being a big fan of Naruto, you want to make a video game on Naruto following these 3 operations. In this problem, you have to do exactly that.

Input

The first line contains one integer N- the number of shadow clones that has been already created. The next line contains N space separated integers denoting an array A. Then there will be an integer Q - the number of operation Naruto performs. Then next Q lines contains one of the following types:

  • 1 V - Add a shadow clone with V amount chakra to the end of the line
  • 2 - Destroys the last shadow clone of the line
  • 3 L R K - Finds the shadow clone with K-th largest chakra in [L, R] range i.e. the K-th shadow clone if you sort the clones with position between L and R in the line according to their chakra in descending order. It is guaranteed that shadow clones will always exist in [L, R] range.

Constraints:

  • 1 ≤ N, Q ≤ 105
  • 1 ≤ A[i], V ≤ 109
  • 1 ≤ K ≤ R-L+1
  • 1 ≤ L, R ≤ total number of current shadow clones

Output

For each 3rd type of operation - you have to print a single integer that denotes the K-th largest chakra in [L, R] range.

Samples

InputOutput
3
7 3 5 
7
3 1 3 2
1 10
3 1 4 2
2
2
1 1
3 2 3 2
5
7
1

Author
  • rohijulislam's Avatar

    rohijulislam

Discussion
Submit

Login to submit