Codeforces Round 875 (Div. 1)
E
Bully Sort
Codeforces
3500
原题
← DF →
翻译中…

On a permutation $p$ of length $n$, we define a bully swap as follows:

翻译中…
  • Let $i$ be the index of the largest element $p_i$ such that $p_i \neq i$.
  • Let $j$ be the index of the smallest element $p_j$ such that $i \lt j$.
  • Swap $p_i$ and $p_j$.
翻译中…

We define $f(p)$ as the number of bully swaps we need to perform until $p$ becomes sorted. Note that if $p$ is the identity permutation, $f(p)=0$.

翻译中…

You are given $n$ and a permutation $p$ of length $n$. You need to process the following $q$ updates.

翻译中…

In each update, you are given two integers $x$ and $y$. You will swap $p_x$ and $p_y$ and then find the value of $f(p)$.

翻译中…

Note that the updates are persistent. Changes made to the permutation $p$ will apply when processing future updates.

翻译中…

Input

翻译中…

The first line of the input contains two integers $n$ and $q$ ($2 \le n \le 5 \cdot 10^5$, $1 \le q \le 5 \cdot 10^4$) — the length of the permutation and the number of updates.

翻译中…

The second line of input contains $n$ integer $p_1,p_2,\ldots,p_n$ ($1 \leq p_i \leq n$) — the permutation $p$. All elements of $p$ are distinct.

翻译中…

The $i$-th of the next $q$ lines of input contains two integers $x_i$ and $y_i$ ($1 \le x_i \lt y_i \le n$) — describing the $i$-th update.

翻译中…

Output

翻译中…

After each update, output $f(p)$.

翻译中…

Note

翻译中…

After the first update, we have $f(p)=5$. The $5$ bully swaps are illustrated below.

翻译中…
  • $[\mathbf{1}, 2, \mathbf{8}, 5, 3, 4, 7, 6]$,
  • $[1, 2, \mathbf{3}, 5, \mathbf{8}, 4, 7, 6]$,
  • $[1, 2, 3, 5, \mathbf{4}, \mathbf{8}, 7, 6]$,
  • $[1, 2, 3, 5, 4, \mathbf{6}, 7, \mathbf{8}]$,
  • $[1, 2, 3, \mathbf{4}, \mathbf{5}, 6, 7, 8]$.
翻译中…

样例

Input #1
8 5
6 2 1 5 3 4 7 8
1 8
2 3
4 7
7 8
3 6
Output #1
5
6
9
8
7
InputCopy OutputCopy
题目信息
题号
1830E
时间限制
10 s
内存限制
1024 MB
标签
数据结构
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。