codeforces 785E. Anton and Permutation

来源:互联网 发布:什么字体软件最好 编辑:程序博客网 时间:2024/06/16 21:47
E. Anton and Permutation
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 44 52 42 52 2
output
1433
input
2 12 1
output
1
input
6 71 43 52 33 33 62 15 1
output
567710118
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.




又一次深切的感受到cf机子的快

做法: 分块,每一个块用一个树状数组预处理出块内元素的前缀权值个数(就是权值在1~i中的数的个数)
         
             块内交换直接暴力,不同块就扫描之间的每一个块,用树状数组统计答案,端点的小部分暴力

    复杂度 O(q*sqrt(n)*log(n))
    
            别问我这么烂的复杂度是怎么过的......

#include<algorithm>#include<iostream>#include<cstdlib>#include<cstdio>#include<cmath>#define N 505#define M 200020#define inf 1<<28#define ll long long#define lowbit(x) x&-xusing namespace std;inline int read(){    ll x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int n,m,q,B;ll ans;int C[N][M],a[M];int bel[M];void add(int u,int x,int val){    for(ll i=x;i<=n;i+=lowbit(i))        C[u][i]+=val;}int ask(int u,int x){    int ret=0;    for(ll i=x;i;i-=lowbit(i))        ret+=C[u][i];    return ret;}struct node{int l,r;}p[N];void init(){    for(ll i=1;i<=m;i++)        for(ll j=p[i].l;j<=p[i].r;j++)            add(i,j,1);}ll cal(int x,int y,int val){    ll tmp=0,txp=0;    for(ll i=x;i<=y;i++)        tmp+=(a[i]>val),txp+=(a[i]<val);    return tmp-txp;}void solve(int x,int y){    if(bel[x]==bel[y])    {        ans+=cal(x+1,y-1,a[x]);        ans-=cal(x+1,y-1,a[y]);        ans+=(a[x]<a[y]);        ans-=(a[x]>a[y]);        swap(a[x],a[y]);    }    else    {        for(int i=bel[x]+1;i<bel[y];i++)        {            ans+=ask(i,a[y]-1);            ans-=ask(i,a[x]-1);            ans+=ask(i,n)-ask(i,a[x]);            ans-=ask(i,n)-ask(i,a[y]);        }        add(bel[x],a[x],-1),add(bel[x],a[y],1);        add(bel[y],a[x],1),add(bel[y],a[y],-1);        ans+=cal(x+1,p[bel[x]].r,a[x]);        ans+=cal(p[bel[y]].l,y-1,a[x]);        ans-=cal(p[bel[y]].l,y-1,a[y]);        ans-=cal(x+1,p[bel[x]].r,a[y]);        ans+=(a[x]<a[y]);ans-=(a[x]>a[y]);        swap(a[x],a[y]);    }printf("%lld\n",ans);}int main(){    n=read(),q=read(),B=600;    m=(n-1)/B+1;    for(int i=1;i<=m;i++)        p[i].l=inf,p[i].r=0;    for(int i=1;i<=n;i++)    {        a[i]=i;        bel[i]=(i-1)/B+1;        p[bel[i]].l=min(p[bel[i]].l,i);        p[bel[i]].r=max(p[bel[i]].r,i);    }init();    for(int i=1;i<=q;i++)    {        int x=read(),y=read();        if(x>y)swap(x,y);        solve(x,y);    }}


1 0
原创粉丝点击