Halve & Merge
시간 제한2초메모리 제한512 MB
배열을 두 부분으로 나눠 병합하는 연산을 처리하면서 특정 위치의 값을 출력하는 문제로, 병합이 두 부분을 정렬한다는 성질을 이용한다.
문제
You have an array that initially contains a permutation of numbers through . You have to process queries of two types:
- " " (): find in the current array ;
- " " (): replace by the result of the function merge applied to arrays and .
Function merge can be written in the following way.
func merge(var a as array, var b as array)
var c as array
while (a and b have elements)
if (a[0] > b[0])
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
while (a has elements)
add a[0] to the end of c
remove a[0] from a
while (b has elements)
add b[0] to the end of c
remove b[0] from b
return c
입력
The first line contains two integers and --- the length of the array and the number of queries ().
The second line contains distinct integers ().
Each of the next lines contains two integers and --- the description of the -th query (, satisfies the constraints given in the format description above).
출력
For each query of type 1, print the answer on a separate line.