P1908逆序对 35分求助!!!

P1908 逆序对

TNZBM @ 2022-01-28 00:05:31

#include <bits/stdc++.h>
using namespace std;
const int MAXN=500010;
typedef long long ll;

ll n,m;
ll c[MAXN],a[MAXN],b[MAXN];

inline void add(register int x){
    while(x<=n){
       c[x]++;
       x+=(x&-x);
}
}

inline int sum(register int x){
    register int s=0;
    while(x>0){
        s+=c[x];
        x-=x&(-x);
    }
    return s;
}

bool cmp(const int &x,const int &y){
    return b[x]>b[y];
}

int main(){
    std::ios::sync_with_stdio(false);

    ll ans=0;
    cin>>n;

    for(int i=1;i<=n;i++){
        cin>>b[i];
        a[i]=i;
    }

    sort(a+1,a+n+1,cmp);

    for(int i=1;i<=n;i++){
        add(a[i]);
        ans+=sum(a[i]-1);
    }

    cout<<ans;
}

by sprads @ 2022-01-28 07:41:29

@TNZBM

会出现重复数字,排序需要注意


by Skies @ 2022-01-28 09:33:42

@TNZBM


by Skies @ 2022-01-28 09:33:58

离散化


by VectorLi @ 2022-03-02 15:43:05

需要离散化


|