您的位置:首页 > 其它

Codeforces Round #404 (Div. 2) E. Anton and Permutation(分块+二分)

2017-03-18 23:12 776 查看
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

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

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.

Examples

input
5 4
4 5
2 4
2 5
2 2


output
1
4
3
3


input
2 1
2 1


output
1


input
6 7
1 4
3 5
2 3
3 3
3 6
2 15 1


output
5
6
7
7
10
11
8

分析:分块,每个块内单独排序,然后每次询问先处理块内的贡献(扫一遍),然后再处理块外的贡献(二分).这个复杂度居然能卡过去.

#include <bits/stdc++.h>
#define N 200005
using namespace std;
int n,q,l,r,block,f
,Block
;
long long ans;
struct thing
{
int l,r;
vector<int> val;
}Bl
;
void deal(int x,int y)
{
int lab = Block[x];
for(int i = Bl[lab].l;i <= Bl[lab].r;i++)
if((f[i] > f[x] && i < x) || (f[i] < f[x] && i > x)) ans += y;
for(int i = 1;i <= Block
;i++)
if(i != lab)
{
int num = lower_bound(Bl[i].val.begin(),Bl[i].val.end(),f[x]) - Bl[i].val.begin() + 1;
if(i < Block[x]) ans += (Bl[i].r - Bl[i].l + 2 - num)*y;
else ans += (num-1)*y;
}
}
void up_data(int x,int y)
{
int lab = Block[x];
Bl[lab].val.erase(lower_bound(Bl[lab].val.begin(),Bl[lab].val.end(),f[x]));
Bl[lab].val.insert(lower_bound(Bl[lab].val.begin(),Bl[lab].val.end(),y),y);
}
int main()
{
scanf("%d%d",&n,&q);
block = (int)sqrt(1ll*n*n/q) + 1;
for(int i = 1;i <= n;i++)
{
f[i] = i;
Block[i] = (i-1)/block + 1;
if(!Bl[Block[i]].l) Bl[Block[i]].l = i;
Bl[Block[i]].r = i;
}
for(int i = 1;i <= Block
;i++)
for(int j = Bl[i].l;j <= Bl[i].r;j++)
Bl[i].val.push_back(f[j]);
for(int i = 1;i <= q;i++)
{
scanf("%d%d",&l,&r);
if(l == r)
{
cout<<ans<<endl;
continue;
}
if(l > r) swap(l,r);
if(f[l] > f[r]) ans++;
else ans--;
int b1 = Block[l],b2 = Block[r];
if(b1 == b2)
{
for(int i = Bl[b1].l;i <= Bl[b1].r;i++)
if((f[i] > f[l] && i < l) || (f[i] < f[l] && i > l)) ans--;
for(int i = Bl[b1].l;i <= Bl[b1].r;i++)
if((f[i] > f[r] && i < r) || (f[i] < f[r] && i > r)) ans--;
swap(f[l],f[r]);
for(int i = Bl[b1].l;i <= Bl[b1].r;i++)
if((f[i] > f[l] && i < l) || (f[i] < f[l] && i > l)) ans++;
for(int i = Bl[b1].l;i <= Bl[b1].r;i++)
if((f[i] > f[r] && i < r) || (f[i] < f[r] && i > r)) ans++;
}
else
{
deal(l,-1),deal(r,-1);
up_data(l,f[r]),up_data(r,f[l]);
swap(f[l],f[r]);
deal(l,1),deal(r,1);
}
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: