#t138. 区间排序

区间排序

Range Sorting

Problem Description

Xiao Yang has a sequence aa of nn positive integers.

He plans to sort the sequence in ascending order several times. Each time, he chooses an interval [l,r][l, r] (l≤rl \le r) and sorts the numbers al,al+1,…,ara_l, a_{l+1}, \dots, a_r 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 nn, as described.

The second line contains nn positive integers a1,a2,…,ana_1, a_2, \dots, a_n, the sequence aa.

The third line contains a positive integer qq, the number of sorts.

Each of the following qq lines contains two positive integers l,rl, r, meaning that all numbers in the interval [li,ri][l_i, r_i] are sorted in ascending order.

Output Format

Output one line containing nn 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 [3,4,5,2,1][3, 4, 5, 2, 1].

  • First sort: interval [4,5][4, 5], sort the 4th and 5th numbers 2,12, 1 in ascending order → sequence becomes [3,4,5,1,2][3, 4, 5, 1, 2].
  • Second sort: interval [3,4][3, 4], sort the 3rd and 4th numbers 5,15, 1 in ascending order → sequence becomes [3,4,1,5,2][3, 4, 1, 5, 2].
  • Third sort: interval [1,3][1, 3], sort the 1st to 3rd numbers 3,4,13, 4, 1 in ascending order → sequence becomes [1,3,4,5,2][1, 3, 4, 5, 2].

The final output sequence is 1 3 4 5 21\ 3\ 4\ 5\ 2.

Constraints

For all test data, 1≤n,ai,q≤1001 \le n, a_i, q \le 100 and 1≤li≤ri≤n1 \le l_i \le r_i \le n are guaranteed.