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
http://codeforces.com/contest/785/problem/E
题意:有一个包含n个数的数组,里面元素刚好是从1到n按从小到大顺序排列,现有m次操作,
每次操作将其中两个数互换位置,对于每一次操作后输出当前有多少个逆序对
思路:
对于每次交换位置,设交换的两个位置为x和y,ans为交换前的逆序对数,那么可以暴力区间(x,y)中有多少个数
k满足k>a[x],以及有多少个数k满足k<a[y],之后将ans加上区间(x,y)中所有>a[x]的数以及所有<a[y]的数,再减去区
间(x,y)中所有<a[x]的数以及所有>a[y]的数即是交换后逆序数的对数
但直接暴力会超时,这就需要分块解决了,将n个数分成sqrt(n)块,这样时间复杂度就由n²降为sqrt(n)*log(sqrt(n))*n
分块后的变量:
bel[i]:第i个数属于第bel[i]块
L[i]:第i个块最左边在原数列的位置
R[i]:第i个块最右边在原数列的位置
P[i]:存有第i个块中所有的数,且这些数按小到大排列
分块之后:
查询:对完全在区间(x,y)中的块通过二分每一个快速找出有多少个满足>a[x]或<a[y]的,
对区间边界上的块直接暴力找
交换:先交换块中的元素,注意一定要找到合适地方插入以保证有序(删除就不说了),
再交换原数组中的元素,直接swap即可
注意:如果a[l]>a[r]则ans--,否则ans++
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> P[1008];
int a[200005], bel[200005], L[1008], R[1008];
void init(int n)
{
int i, j, x, num;
for(i=1;i<=n;i++)
a[i] = i;
x = sqrt(n);
num = (n-1)/x+1;
for(i=1;i<=n;i++)
bel[i] = (i-1)/x+1;
for(i=1;i<=num;i++)
{
L[i] = (i-1)*x+1;
R[i] = i*x;
for(j=L[i];j<=min(R[i],n);j++)
P[i].push_back(a[j]);
}
R[num] = n;
}
int Query(int l, int r, int val)
{
int i, sum;
sum = 0;
if(l>r)
return 0;
if(bel[l]==bel[r])
{
for(i=l;i<=r;i++)
{
if(a[i]<val)
sum += 1;
}
return sum;
}
for(i=l;i<=R[bel[l]];i++)
{
if(a[i]<val)
sum += 1;
}
for(i=L[bel[r]];i<=r;i++)
{
if(a[i]<val)
sum += 1;
}
for(i=bel[l]+1;i<=bel[r]-1;i++)
sum += upper_bound(P[i].begin(), P[i].end(), val)-P[i].begin();
return sum;
}
void Update(int l, int r)
{
int u, v, x, y;
u = a[l], v = a[r];
x = bel[l], y = bel[r];
P[x].erase(lower_bound(P[x].begin(), P[x].end(), u));
P[x].insert(upper_bound(P[x].begin(), P[x].end(), v), v);
P[y].erase(lower_bound(P[y].begin(), P[y].end(), v));
P[y].insert(upper_bound(P[y].begin(), P[y].end(), u), u);
swap(a[l], a[r]);
}
int main(void)
{
int n, m, l, r;
long long ans = 0;
scanf("%d%d", &n, &m);
init(n);
while(m--)
{
scanf("%d%d", &l, &r);
if(l>r) swap(l, r);
if(l==r)
printf("%lld\n", ans);
else
{
ans += r-l-1-2*Query(l+1, r-1, a[l]);
ans -= r-l-1-2*Query(l+1, r-1, a[r]);
if(a[l]>a[r]) ans -= 1;
else ans += 1;
Update(l, r);
printf("%lld\n", ans);
}
}
return 0;
}