Emission Spectrum
Time limit2sMemory limit512 MB
Maintain an integer array under adjacent swaps and answer k-th smallest value queries over subarrays online.
- Level
Hard8 of 10
- Topics
- Binary search, Divide and conquer, Segment tree, Sorting
- Solved
- No attempts yet
Problem
Scientists at the Interstellar Consortium of Planets and Constellations (ICPC) study what celestial objects are made of by analyzing their emission spectrum. The emission spectrum of a celestial object is the set of frequencies of electromagnetic radiation emitted by its atomic energy transitions, together with the intensity of that radiation. It records how strong each color of the emitted light is.
By the postulates of quantum mechanics the emission spectrum of a celestial object is always discrete, so the ICPC stores a spectrum as a sequence of integers. Each position of the sequence is the intensity of one wavelength, a larger number means a stronger emission, and neighboring positions of the sequence are neighboring wavelengths of the spectrum.
An emission spectrum is the result of very complex physical processes, so it changes over the lifetime of the object. In particular, because of atomic reactions that are not yet fully understood, the intensities of two neighboring wavelengths can be swapped at some moment.
The ICPC is watching the spectrum of one celestial object very closely. Given a range of wavelengths and an integer , the scientists want the intensity of the wavelength with the -th smallest intensity in that range. You are given the observation log, which mixes the questions asked by the scientists with the intensity swaps that happen in the spectrum. Answer every question.
Input
The first line contains two integers and , the number of measured wavelengths and the number of events (). The second line contains integers , where is the initial intensity of the -th wavelength ().
Each of the next lines describes one event and starts with a character giving the type of the event. A question from the scientists starts with the character Q, and an atomic swap reaction starts with the character S.
A question line has three integers , and after that character. The scientists ask for the -th smallest intensity among the wavelengths at positions through , inclusive (, ).
A swap line has one integer after that character. The intensities at positions and are exchanged ().
Output
For each question print one line with a single integer, the -th smallest intensity among the wavelengths at positions through , inclusive. Answer the questions in the order they appear in the input.