#ABC476E. 最值交换 / Min-Max Swap

最值交换 / Min-Max Swap

Problem Statement

You are given a permutation P=(P1,P2,…,PN)P=(P_1,P_2,\ldots,P_N) of (1,2,…,N)(1,2,\ldots,N) and length-MM integer sequences L=(L1,L2,…,LM)L=(L_1,L_2,\ldots,L_M) and R=(R1,R2,…,RM)R=(R_1,R_2,\ldots,R_M).

For this permutation PP, perform the following operation for i=1,2,…,Mi=1,2,\ldots,M in this order:

  • Among PLi,PLi+1,…,PRiP_{L_i},P_{L_i+1},\ldots,P_{R_i}, swap the positions of the element with the minimum value and the element with the maximum value.

Find each element of PP after the MM operations.

Constraints

  • 2≤N≤2×1052\le N\le 2\times 10^5
  • 1≤M≤2×1051\le M\le 2\times 10^5
  • PP is a permutation of (1,2,…,N)(1,2,\ldots,N).
  • 1≤Li<Ri≤N1\le L_i < R_i\le N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

  • NN MM
  • P1P_1 P2P_2 …\ldots PNP_N
  • L1L_1 R1R_1
  • L2L_2 R2R_2
  • ⋮\vdots
  • LML_M RMR_M

Output

Output each element of PP after the MM operations, in order from front to back, separated by spaces.

5 3
3 1 4 2 5
1 3
1 5
1 4
3 4 2 5 1

Initially, P=(3,1,4,2,5)P=(3,1,4,2,5).

  • For i=1i=1: among P1,P2,P3P_1,P_2,P_3, the minimum value is P2P_2 and the maximum value is P3P_3. Swapping P2P_2 and P3P_3 gives P=(3,4,1,2,5)P=(3,4,1,2,5).
  • For i=2i=2: among P1,P2,P3,P4,P5P_1,P_2,P_3,P_4,P_5, the minimum value is P3P_3 and the maximum value is P5P_5. Swapping P3P_3 and P5P_5 gives P=(3,4,5,2,1)P=(3,4,5,2,1).
  • For i=3i=3: among P1,P2,P3,P4P_1,P_2,P_3,P_4, the minimum value is P4P_4 and the maximum value is P3P_3. Swapping P3P_3 and P4P_4 gives P=(3,4,2,5,1)P=(3,4,2,5,1).

After the three operations, P=(3,4,2,5,1)P=(3,4,2,5,1).

6 7
3 6 5 2 4 1
4 5
2 3
3 5
4 6
3 4
1 6
3 5
3 5 4 6 2 1