树状数组全wa

P1908 逆序对

hehe123313 @ 2023-11-23 11:45:55

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N=5*1e5+10;
vector<int> v;
int a[N];
int n;
long long num;
int cnt[N];
int lowbit(int x)
{
    return x&-x;

}
void add(int k,int c)
{
    for(int i=k;i<=N;i+=lowbit(i))
        cnt[i]+=c;

}
int query(int k)
{
    int sum=0;
    for(int i=k;i>=1;i-=lowbit(i))
    sum+=cnt[i];
    return sum;
}
int find(int x)
{
    int l=0,r=v.size();
    while(l<r)
    {
        int mid=l+r>>1;
        if(v[mid]>=x)
        r=mid;
        else
        l=mid+1;
    }
    return l+1;
}
int main()
{

    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    for(int i=0;i<v.size();i++)
    v[i]=i+1;
    for(int i=1;i<=n;i++)
    {
    int k=find(a[i]);
    num+=i-1-query(k);
    add(k,1);
    }
    printf("%lld",num);

    return 0;
}

by hehe123313 @ 2023-11-23 11:53:25

过了过了,原来我离散化错了哈哈哈


|