#t138. 区间排序
区间排序
Range Sorting
Problem Description
Xiao Yang has a sequence of positive integers.
He plans to sort the sequence in ascending order several times. Each time, he chooses an interval () and sorts the numbers within that interval in ascending order. Each sort is performed on top of the result of the previous sort.
Xiao Yang asks you to compute the sequence after all the ascending sorts.
Input Format
The first line contains a positive integer , as described.
The second line contains positive integers , the sequence .
The third line contains a positive integer , the number of sorts.
Each of the following lines contains two positive integers , meaning that all numbers in the interval are sorted in ascending order.
Output Format
Output one line containing positive integers: the sequence after all the ascending sorts.
Sample Input
5
3 4 5 2 1
3
4 5
3 4
1 3
Sample Output
1 3 4 5 2
Sample Explanation
For the sample, the initial sequence is .
- First sort: interval , sort the 4th and 5th numbers in ascending order → sequence becomes .
- Second sort: interval , sort the 3rd and 4th numbers in ascending order → sequence becomes .
- Third sort: interval , sort the 1st to 3rd numbers in ascending order → sequence becomes .
The final output sequence is .
Constraints
For all test data, and are guaranteed.