性感代码,在线求调

P1908 逆序对

XuwuWorld @ 2025-01-11 15:36:50

30pts,求调

#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL n,sum[2145141],ans,m;
LL lowbit(LL x){
    return x&(-x);
}
void add(int l,int x){
    for(int i=l;i<=m;i+=lowbit(i)){
        sum[i]+=x;
    }
}
LL query(LL x){
    LL ans=0;
    for(int i=x;i>=1;i-=lowbit(i)){
        ans+=sum[i];
    }
    return ans;
}
struct qwq{
    LL x,y;
}a[114514];
bool cmp(qwq X,qwq Y){
    return X.y<Y.y;
}
bool cmp2(qwq X,qwq Y){
    return X.x<Y.x;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i].x,a[i].y=i;
    sort(a+1,a+1+n,cmp2);
    for(int i=1,j=1;i<=n;i++){
        if(a[i].x!=a[i+1].x){
            a[i].x=j;
            j++;
        }
        else a[i].x=j;
        m=max(a[i].x,m);
    }
    sort(a+1,a+1+n,cmp);
    for(int i=n;i>=1;i--){
        add(a[i].x,1);
        ans+=query(a[i].x)-1;
    }
    cout<<ans<<endl;
} 

|