AT_abc476_e.Min-Max Swap

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

给你一个 (1,2,…,N)(1,2,\ldots,N) 的排列 P=(P1,P2,…,PN)P=(P_1,P_2,\ldots,P_N),以及两个长度为 MM 的整数序列 L=(L1,L2,…,LM)L=(L_1,L_2,\ldots,L_M) 和 R=(R1,R2,…,RM)R=(R_1,R_2,\ldots,R_M)。

对这个排列 PP,按 i=1,2,…,Mi=1,2,\ldots,M 的顺序依次执行以下操作:

  • 在子数组 PLi,PLi+1,…,PRiP_{L_i},P_{L_i+1},\ldots,P_{R_i} 中,将最小值元素与最大值元素的位置互换。

求经过 MM 次操作后 PP 的每个元素。

输入格式

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

输入从标准输入中按以下格式给出:

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

输出格式

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

在执行 MM 次操作后,按从前到后的顺序输出 PP 的每个元素,元素之间用空格分隔。

输入输出样例

  • 输入#1

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

    输出#1

    3 4 2 5 1
  • 输入#2

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

    输出#2

    3 5 4 6 2 1

说明/提示

Sample 1 Explanation:
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).

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.

样例 1 解释:
初始时,P=(3,1,4,2,5)P=(3,1,4,2,5)。

  • 对于 i=1i=1:在 P1,P2,P3P_1,P_2,P_3 中,最小值为 P2P_2,最大值为 P3P_3。交换 P2P_2 和 P3P_3 后得到 P=(3,4,1,2,5)P=(3,4,1,2,5)。
  • 对于 i=2i=2:在 P1,P2,P3,P4,P5P_1,P_2,P_3,P_4,P_5 中,最小值为 P3P_3,最大值为 P5P_5。交换 P3P_3 和 P5P_5 后得到 P=(3,4,5,2,1)P=(3,4,5,2,1)。
  • 对于 i=3i=3:在 P1,P2,P3,P4P_1,P_2,P_3,P_4 中,最小值为 P4P_4,最大值为 P3P_3。交换 P3P_3 和 P4P_4 后得到 P=(3,4,2,5,1)P=(3,4,2,5,1)。

经过三次操作后,P=(3,4,2,5,1)P=(3,4,2,5,1)。

约束条件

  • 2≤N≤2×1052\le N\le 2\times 10^5
  • 1≤M≤2×1051\le M\le 2\times 10^5
  • PP 是 (1,2,…,N)(1,2,\ldots,N) 的一个排列。
  • 1≤Li<Ri≤N1\le L_i < R_i\le N
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页