#P6186. [NOI Online #1 提高组] 冒泡排序

[NOI Online #1 提高组] 冒泡排序

Problem Description

You are given a permutation pip_i of 1n1 \sim n. Then there are mm operations, of two types:

  1. Swap operation: given xx, swap the xx-th number and the (x+1)(x+1)-th number in the current permutation.
  2. Query operation: given kk, find the number of inversions in the current permutation after performing kk rounds of bubble sort.

The pseudocode for performing one round of bubble sort on a permutation pip_i of length nn is:

for i = 1 to n-1:
  if p[i] > p[i + 1]:
    swap(p[i], p[i + 1])

Input Format

The first line contains two integers nn and mm, representing the length of the permutation and the number of operations.

The second line contains nn integers representing the permutation pip_i.

The next mm lines each contain two integers tit_i and cic_i, describing an operation:

  • If ti=1t_i = 1, then this operation is a swap operation, and x=cix = c_i.
  • If ti=2t_i = 2, then this operation is a query operation, and k=cik = c_i.

Output Format

For each query operation, output one line with one integer representing the answer.

3 6
1 2 3
2 0
1 1
1 2
2 0
2 1
2 2
0
2
1
0

Hint

Explanation for Sample 1

First operation: the permutation is {1,2,3}\{1,2,3\}. After 00 rounds of bubble sort it is {1,2,3}\{1,2,3\}, with 00 inversions.

Second operation: the permutation becomes {2,1,3}\{2,1,3\}.

Third operation: the permutation becomes {2,3,1}\{2,3,1\}.

Fourth operation: after 00 rounds of bubble sort the permutation is {2,3,1}\{2,3,1\}, with 22 inversions.

Fifth operation: after 11 round of bubble sort the permutation becomes {2,1,3}\{2,1,3\}, with 11 inversion.

Sixth operation: after 22 rounds of bubble sort the permutation becomes {1,2,3}\{1,2,3\}, with 00 inversions.


Constraints and Notes

For testdata 1 \sim 2: n,m100n, m \leq 100.

For testdata 3 \sim 4: n,m2000n, m \leq 2000.

For testdata 5 \sim 6: the number of swap operations does not exceed 100100.

For all testdata: 2n,m2×1052 \leq n, m \leq 2 \times 10^5, ti{1,2}t_i \in \{1,2\}, 1x<n1 \leq x < n, 0k<2310 \leq k < 2^{31}.

Translated by ChatGPT 5