为什么re

P1908 逆序对

limbus @ 2024-01-13 11:26:46

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=500005;
int n;
int a[N],b[N],c[N];
#define lowbit(x) x&(-x)
void update(int p){
    while(p>=n){
        c[p]++;
        p+=lowbit(p);
    }
}
int getsum(int x){
    int sum=0;
    while(x>=1){
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
bool cmp(int x,int y){
    if(a[x]==a[y]) return x>y;
    return a[x]>a[y];
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=i;
    }
    sort(b+1,b+n+1,cmp);
    long long ans=0;
    for(int i=1;i<=n;i++){
        update(b[i]);
        ans+=getsum(b[i]-1);
    }
    cout<<ans;
    return 0;
}

by 2huk @ 2024-01-13 11:32:57

@Sythergy 离散化假了吧,需要去重


by 2huk @ 2024-01-13 11:34:20

@Sythergy 不是离散化,是 update 里面应该是 while (p <= n)


by 2huk @ 2024-01-13 11:34:45

https://www.luogu.com.cn/record/142659045


by limbus @ 2024-01-13 11:38:26

现在还是只有60分


by limbus @ 2024-01-13 11:38:35

@2huk


by limbus @ 2024-01-13 11:42:39

哦过了谢谢


|