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.
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 q lines. The i-th line of the output is the number of inversions in the Anton's permutation after the i-th operation.
5 4 4 5 2 4 2 5 2 2
1 4 3 3
2 1 2 1
1
6 7 1 4 3 5 2 3 3 3 3 6 2 1 5 1
5 6 7 7 10 11 8
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.
思路:以前就听过分块这种数据结构,不过一直没写过。这回遇见了就查一下别人怎么写的然后再自己写一遍。。。思路就是把n个数分成sqrt(n)块。。然后每一块都是升序的。那么查找的时候可以用二分,每一块查找的复杂度是log(sqrt(n))。插入的复杂度一样。那么这道题就很容易解决了。下面给代码:
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<utility>
#include<map>
#include<vector>
#define maxn 200005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-8;
vector<int>v[maxn];
int Left[maxn], Right[maxn], belong[maxn], a[maxn];
void build(int n){
int num = sqrt(n);
int block = n / num;
if (n%num)
block++;
for (int i = 1; i <= block; i++){
Left[i] = (i - 1)*num + 1;
Right[i] = i*num;
}
for (int i = 1; i <= n; i++){
belong[i] = (i - 1) / num + 1;
}
for (int i = 1; i <= block; i++){
for (int j = Left[i]; j <= Right[i]; j++){
v[i].push_back(j);
}
}
}
LL query(int l, int r, int now){
if (l > r)
return 0;
LL ans = 0;
if (belong[l] == belong[r]){
for (int i = l; i <= r; i++){
if (a[i] < now)
ans++;
}
}
else{
for (int i = l; i <= Right[belong[l]]; i++){
if (a[i] < now)
ans++;
}
for (int i = belong[l] + 1; i < belong[r]; i++){
int pos = upper_bound(v[i].begin(), v[i].end(), now) - v[i].begin();
ans += pos;
}
for (int i = Left[belong[r]]; i <= r; i++){
if (a[i] < now)
ans++;
}
}
return ans;
}
void update(int x, int y){
int id = belong[x];
v[id].erase(lower_bound(v[id].begin(), v[id].end(), a[x]));
v[id].insert(upper_bound(v[id].begin(), v[id].end(), a[y]), a[y]);
id = belong[y];
v[id].erase(lower_bound(v[id].begin(), v[id].end(), a[y]));
v[id].insert(upper_bound(v[id].begin(), v[id].end(), a[x]), a[x]);
swap(a[x], a[y]);
}
int main(){
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++){
a[i] = i;
}
build(n);
LL ans = 0;
while (q--){
int x, y;
scanf("%d%d", &x, &y);
if (x > y)
swap(x, y);
if (x == y)
printf("%lld\n", ans);
else{
LL sub = query(x + 1, y - 1, a[x]);
LL add = y - 1 - x - sub;
ans -= sub;
ans += add;
add = query(x + 1, y - 1, a[y]);
sub = y - 1 - x - add;
ans += add;
ans -= sub;
if (a[x] < a[y])
ans++;
else
ans--;
printf("%lld\n", ans);
update(x, y);
}
}
}