【板子】树状数组(BIT)

//lg 1908 求逆序对
//Copyright yeyou26
#include<bits/stdc++.h>
using namespace std;

#define ll long long 

const int N = (int)1e6+6;

ll sum;

int n;

struct Data
{
    int origin;
    int ls;
    int id;
}data[N];
bool cmporigin(Data x,Data y)
{
    return x.origin<y.origin;
}
bool cmpid(Data x,Data y)
{
    return x.id<y.id;
}

struct Binary_Indexed_Tree
{
    int a[N];
    void Modify(int where,int value)
    {
        while(where<=N) a[where]+=value,where+=(where&(-where));
    }
    int Query(int where)
    {
        int ans=0;
        while(where) ans+=a[where],where-=(where&(-where));
        return ans; 
    }
}Bit;

int main()
{
    // freopen("working.in","r",stdin);
    // freopen("working.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&data[i].origin);
        data[i].id=i;
    }
    sort(data+1,data+1+n,cmporigin);
    int idx=0;
    for(int i=1;i<=n;i++) 
    {
        if(data[i].origin!=data[i-1].origin) data[i].ls=++idx;
        else data[i].ls=idx;
    }
    sort(data+1,data+1+n,cmpid);
    for(int i=n;i>=1;i--)
    {
        Bit.Modify(data[i].ls,1);
        sum+=Bit.Query(data[i].ls-1);
    }
    cout<<sum;
    return 0;
}
posted @ 2024-01-26 20:49  yeyou26  阅读(16)  评论(0编辑  收藏  举报