406991: GYM102651 D Bookshelf Sorting

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

D. Bookshelf Sortingtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Irma works in a library. Every day she watches visitors take a couple of books from the bookshelf, read them, and put books back in the same places they took them. Usually people mess up the order and swap two books they read. Let's take a look at one specific bookshelf with $$$n$$$ books in some order, numbered from $$$1$$$ to $$$n$$$ from left to right. The $$$i$$$-th visitor takes books from positions $$$x_i$$$ and $$$y_i$$$ and puts them back on the same positions, but in the wrong order. After the $$$i$$$-th visitor, the book that was at $$$x_i$$$ is now at position $$$y_i$$$ and vice versa.

In the evening, after the library is closed, Irma wants to put all the books back on their places. For each book there is a number $$$p_i$$$ — a position where that book should end up in the end. To rearrange books, Irma can take any book from the shelf and insert it in the beginning or in the end (so it ends up in the first or in the last place on the shelf).

What is the minimum number of moves Irma can do to put all books in order? Answer this question for some initial placement of books, determined by $$$p_i$$$, and after each visitor that swapped places of some two books.

Input

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$0 \le q \le 2 \cdot 10^5$$$) — the number of books on the shelf and the number of visitors. The next line contains $$$n$$$ distinct integers $$$p_i$$$ ($$$1 \le p_i \le n$$$), meaning that the book in the $$$i$$$-th position must end up in position $$$p_i$$$.

Next $$$q$$$ lines describe library's visitors. Each line contains two integers $$$x_i$$$, $$$y_i$$$ ($$$1 \le x_i < y_i \le n$$$), that mean that the $$$i$$$-th visitor swapped two books on positions $$$x_i$$$ and $$$y_i$$$.

Output

Print $$$q + 1$$$ integers — the minimum number of moves required to sort all books for the initial order, then for the order the after the first visitor, ..., after all $$$q$$$ visitors.

Scoring
{Subtask}{Score}{Constraints}
110$$$n, q \le 8$$$
215$$$n, q \le 200$$$
315$$$n \le 2000$$$; $$$q = 0$$$
415$$$n, q \le 2000$$$
520$$$n \le 2 \cdot 10^5$$$; $$$q = 0$$$
625$$$n, q \le 2 \cdot 10^5$$$
ExampleInput
5 2
5 1 2 4 3
4 5
1 4
Output
2
1
3
Note

The initial order of books is $$$(5, 1, 2, 4, 3)$$$. To sort these books out, first, put the book $$$4$$$ in the end, then do the same with book $$$5$$$. After the first visitor, the bookshelf now looks like $$$(5, 1, 2, 3, 4)$$$; it's enough to just move the book $$$5$$$ to the end. After the second visitor the order of books is $$$(3, 1, 2, 5, 4)$$$. For this order the minimum number of moves is $$$3$$$, there are several ways to achieve the final order in $$$3$$$ moves.

加入题单

上一题 下一题 算法标签: