E. Anton and Permutation (树状数组+主席树)

E. Anton and Permutation
time limit per test4 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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 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.
题意和题解在之前分块做法中给出。这里只给出主席树做法的代码。
代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=900010;
int cnt,n,m,x,y,k,tmp,root[N],spt[N],a[N];
int cmp;
struct node{int l,r,sum;}T[N*40];
struct Op{int l,r,k,type;}op[N];
vector<int>Q1,Q2;
int lowbit(int x) {return -x&x;}
void update(int l,int r,int &x,int y,int pos )
{
    T[++cnt]=T[y],T[cnt].sum++,x=cnt;
    if(l==r) return ;
    int mid=(r+l)>>1;
    if(pos<=mid) update(l,mid,T[x].l,T[y].l,pos);
    else  update(mid+1,r,T[x].r,T[y].r,pos);
}
void insert(int l,int r,int &i,int x,int val)
{
    if(i==0) T[++cnt]=T[i],i=cnt;
    T[i].sum+=val;
    if(l==r) return ;
    int mid=(r+l)>>1;
    if(x<=mid)insert(l,mid,T[i].l,x,val);
    else insert(mid+1,r,T[i].r,x,val);
}
void BIT_insert(int pos,int x,int val)
{
    for(int i=pos;i<=N;i+=lowbit(i))
        insert(1,N,spt[i],x,val);
}
/*int ST_query(vector<int> Q1,vector<int> Q2,int l,int r,int k)
{
    if(l==r) return l>k?1:0;
    int c=0;
    int mid=(r+l)>>1;
    for(int i=0,sz=Q1.size();i<sz;i++) c-=T[T[Q1[i]].l].sum;
    for(int i=0,sz=Q2.size();i<sz;i++) c+=T[T[Q2[i]].l].sum;
    for(int i=0,sz=Q1.size();i<sz;i++) Q1[i]=(c>=k?T[Q1[i]].l:T[Q1[i]].r);
    for(int i=0,sz=Q2.size();i<sz;i++) Q2[i]=(c>=k?T[Q2[i]].l:T[Q2[i]].r);
    if(c>=k) return ST_query(Q1,Q2,l,mid,k);
    else return ST_query(Q1,Q2,mid+1,r,k-c);
}*/
int query(int l,int r,int rt,int k)
{
    if(!rt) return 0;
    if(l==r) return T[rt].sum;
    int mid=(r+l)>>1;
    if(k<=mid) return query(l,mid,T[rt].l,k);
    else  return T[T[rt].l].sum+query(mid+1,r,T[rt].r,k);
}
/*int query(int l,int r,int k)//xiao
{
    int x=root[l-1],y=root[r];

    Q1.clear(),Q2.clear();
    Q1.push_back(root[l-1]);
    Q2.push_back(root[r]);
    for(int i=l-1;i>0;i-=lowbit(i)) Q1.push_back(spt[i]);
    for(int i=r;i>0;i-=lowbit(i)) Q2.push_back(spt[i]);
    return ST_query(Q1,Q2,1,N,k);
}*/
int main()
{
    ll ans=0;
    cnt=0;
    memset(root,0,sizeof(root));
    memset(spt,0,sizeof(spt));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) a[i]=i;
    for(int i=1; i<=n; i++) update(1,N,root[i],root[i-1],i);
    for(int i=1; i<=m; i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        if(l==r)
        {
            printf("%I64d\n",ans);
            continue;
        }
        if(l>r) swap(l,r);
        if(r-l>1)
        {
            int t1=query(1,N,root[r-1],a[r])-query(1,N,root[l],a[r]);
            for(int i=l;i>0;i-=lowbit(i)) t1-=query(1,N,spt[i],a[r]);
            for(int i=r-1;i>0;i-=lowbit(i)) t1+=query(1,N,spt[i],a[r]);
            int t2=r-1-l-1+1-t1;
            ans+=t1;
            ans-=t2;
            t1=query(1,N,root[r-1],a[l])-query(1,N,root[l],a[l]);
            for(int i=l;i>0;i-=lowbit(i)) t1-=query(1,N,spt[i],a[l]);
            for(int i=r-1;i>0;i-=lowbit(i)) t1+=query(1,N,spt[i],a[l]);
            t2=r-1-l-1+1-t1;
            ans-=t1;
            ans+=t2;
        }
        if(a[l]<a[r]) ans++;
        else ans--;
        printf("%I64d\n",ans);
            BIT_insert(l,a[l],-1);
            BIT_insert(l,a[r],1);
            BIT_insert(r,a[r],-1);
            BIT_insert(r,a[l],1);
            swap(a[l],a[r]);
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值