codeforces 785E Anton and Permutation

18 篇文章 0 订阅

http://www.elijahqi.win/2018/03/02/codeforces-785e/
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

Copy
5 4
4 5
2 4
2 5
2 2
Output
1
4
3
3
Input

Copy
2 1
2 1
Output
1
Input

Copy
6 7
1 4
3 5
2 3
3 3
3 6
2 1
5 1
Output
5
6
7
7
10
11
8
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.

分块求 因为交换两个数字 只会影响中间的那一部分内容 所以只需要统计中间的那部分有多少比左端点大 多少比左端点小即可 所以每个块内 排序 log2(sqrt(n))的时间二分一下即可

做不是整块的时候取出来 暴力算即可注意交换之后重建维护序列

#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 220000
#define ll long long
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
ll sum;
int ta[500][500],q[500],n,nn,m,b[N],left[N],right[N],a[N],top;
inline void update(int *qq,int l,int r,int len){
        sum-=lower_bound(qq+1,qq+len+1,a[l])-qq-1;sum+=lower_bound(qq+1,qq+len+1,a[r])-qq-1;
        sum+=len-(upper_bound(qq+1,qq+len+1,a[l])-qq-1);sum-=len-(upper_bound(qq+1,qq+len+1,a[r])-qq-1);
}
inline void qr(int l,int r){
    if (b[l]==b[r]){top=0;
        for (int i=l+1;i<r;++i) q[++top]=a[i];sort(q+1,q+top+1);update(q,l,r,top);
        sum-=a[l]>a[r];sum+=a[l]<a[r];swap(a[l],a[r]);return;
    }
    for (int i=b[l]+1;i<b[r];++i)update(ta[i],l,r,right[i]-left[i]+1);top=0;
    for (int i=l+1;i<=right[b[l]];++i) q[++top]=a[i];
    for (int i=left[b[r]];i<r;++i) q[++top]=a[i];sort(q+1,q+top+1);
    update(q,l,r,top);sum-=a[l]>a[r];sum+=a[l]<a[r];swap(a[l],a[r]);
    for (int i=left[b[l]];i<=right[b[l]];++i) ta[b[l]][i-left[b[l]]+1]=a[i];
    sort(ta[b[l]]+1,ta[b[l]]+right[b[l]]-left[b[l]]+2);
    for (int i=left[b[r]];i<=right[b[r]];++i) ta[b[r]][i-left[b[r]]+1]=a[i];
    sort(ta[b[r]]+1,ta[b[r]]+right[b[r]]-left[b[r]]+2);
}
int main(){
//  freopen("cf785e.in","r",stdin);
    n=read();nn=sqrt(n);m=read();
    for (int i=1;i<=(n-1)/nn+1;++i) left[i]=(i-1)*nn+1,right[i]=min(n,left[i]+nn-1);
    for (int i=1;i<=n;++i) {
        b[i]=(i-1)/nn+1,ta[b[i]][i-left[b[i]]+1]=a[i]=i;
    }
    for (int i=1;i<=m;++i){
        int l=read(),r=read();if (l==r) {printf("%I64d\n",sum);continue;}
        if(l>r) swap(l,r);qr(l,r);printf("%I64d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值