Anton and Permutation
Contest: Codeforces Round 404 (Div. 2)
Difficulty rating: 2200
Tags: brute force, data structures

Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers {a1, a2, ..., an}, in which every number from 1 to n appears exactly once.

One day Anton got a new permutation and started to play with it. He does the following operation q times: he takes two elements of the permutation and swaps these elements. After each operation he asks his friend Vanya, how many inversions there are in the new permutation. The number of inversions in a permutation is the number of distinct pairs (i, j) such that 1 ≤ i < j ≤ n and ai > aj.

Vanya is tired of answering Anton's silly questions. So he asked you to write a program that would answer these questions instead of him.

Initially Anton's permutation was {1, 2, ..., n}, that is ai = i for all i such that 1 ≤ i ≤ n.

Input format:
The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 50 000) — the length of the permutation and the number of operations that Anton does.

Each of the following q lines of the input contains two integers li and ri (1 ≤ li, ri ≤ n) — the indices of elements that Anton swaps during the i-th operation. Note that indices of elements that Anton swaps during the i-th operation can coincide. Elements in the permutation are numbered starting with one.

Output format:
Output q lines. The i-th line of the output is the number of inversions in the Anton's permutation after the i-th operation.

Constraints:
Time limit: 4.0 s
Memory limit: 512.0 MB

Note:
Consider the first sample.

After the first Anton's operation the permutation will be {1, 2, 3, 5, 4}. There is only one inversion in it: (4, 5).

After the second Anton's operation the permutation will be {1, 5, 3, 2, 4}. There are four inversions: (2, 3), (2, 4), (2, 5) and (3, 4).

After the third Anton's operation the permutation will be {1, 4, 3, 2, 5}. There are three inversions: (2, 3), (2, 4) and (3, 4).

After the fourth Anton's operation the permutation doesn't change, so there are still three inversions.

Example input:
5 4
4 5
2 4
2 5
2 2

Example output:
1
4
3
3

Example input:
2 1
2 1

Example output:
1

Editorial:
At first observe that there is 0 inversions in the initial permutation.
Let's divide our queries in $$\sqrt{q}$$ blocks. Now learn how to answer all the queries in one block in $${\mathcal O}(n+q+\sqrt{nq})$$.
At first, let's divide our positions in the permutation in fixed and mobile positions. Mobile positions are all the positions that are changed in the current block. Fixed positions are the rest of the positions. Observe that the number of mobile positions is not more than $$2 \cdot \sqrt{q}$$.
Now all the inversions are divided into three types:
1. Inversions only between fixed positions;
2. Inversions only between mobile positions;
3. Inversions between fixed and mobile positions.
To keep inversions of the first type is the easiest of all: their number doesn't change. So we can precalcualte them in the beginning of the block. How can we do it? Let's remember the answer for the query that was directly before the beginning of the block (if the block starts with the first query, this number is equal to 0). This number is equal to the total number of inversions in the beginning of the block. To get the number of fixed inversions, we can just subtract from this number the number of inversions of the second and the third types.
It's also easy to calculate the number of inversions of the second type. In the beginning their number can be counted even using a naive algorithm in $${ \cal O } ( { \sqrt { q ^ { 2 } } } ) = { \cal O } ( q )$$. How to keep this number? We can recalculate each time not all the inversions but only these ones that contain changed elements. Totally we can count them in $${ \cal O } ( { \sqrt { q } } \cdot { \sqrt { q } } ) = { \cal O } ( q )$$ for a block.
It's a little bit harder to keep inversions of the third type. To count them we'll use the similar approach as with inversions of the second type. We'll also recount the number of inversions only for changed elements. So, we must learn how to count the number of inversions between fixed elements and some mobile element on the position x. What fixed elements make an inversion with it? It's obvious that these are the elements which are earlier than the x-th and wherein bigger than it (denote this query countLower(x, px)) or elements which are later than the x-th and wherein smaller than it (denote this query countUpper(x, px)). Here px denotes the x-th element of permutation. Observe that there are $${\mathcal O}(\sqrt{q})$$ such queries.
How can we calculate the answers for these queries quickly? At first note that we can count them offline, because fixed elements doesn't change and mobile elements are not counted in these queries. Consider how we can count answers for countLower(x, y) (for countUpper(x, y) we can use the same approach). Let's sort all the countLower queries by non-decreasing x. Now if we have some structure that can add a value to an element and count a sum on a segment, we can easily do it. Let we added the fixed elements that stay earlier than x (it can be easily done because all the queries are sorted by non-decreasing x). So the answer is the sum on the segment (y + 1, n).
What data structure can we use? We can use sqrt-decomposition, because it takes $${\mathcal{O}}(1)$$ for adding and $$O(\sqrt{n})$$ for sum query. Totally all the $${\mathcal O}(\sqrt{q})$$ countLower and countUpper queries in a block are processed in $${\mathcal O}(n+{\sqrt {nq}})$$.
Time complexity is $$O(\sqrt{q} \cdot (n + q + \sqrt{nq})) = O(n\sqrt{q} + q\sqrt{q} + q\sqrt{n})$$.
There also exists an $${ \mathcal { O } } ( ( n + q ) \cdot \log ^ { 2 } n )$$ solution. You can find it by yourself as an exercise. Such solution should be written carefully, otherwise it doesn't fit to the time limit or memory limit.