马蜂良好有注释,35pts求条,玄关

P1908 逆序对

DX3906_ourstar @ 2024-12-23 12:59:19

rt,Code:

#include<iostream>
#include<algorithm>
#define int long long
#define lowbit(x) x&(-x)
using namespace std;

const int N=1e6+5;

int n,sum;
int c[N];

namespace OIfast{//快读快写 
    inline int read(){
        int n=0,f=1;
        char c=getchar();
        while(c<'0'||c>'9'){
            if(c=='-')f=-1;
            c=getchar();
        }
        while(c>='0'&&c<='9')n=n*10+c-'0',c=getchar();
        return n*f;
    }

    inline void print(int n){
        if(n<0)putchar('-'),n=-n;
        if(n>=10)print(n/10);
        putchar(n%10+'0');
        return ;
    }

    inline void write(int n,char c){
        print(n),putchar(c);
    }
}using namespace OIfast;

namespace treeArray{//树状数组的基本操作 
    struct node{//节点定义 
        int v/*点权*/,id/*点号*/;
    }a[N];

    inline void add(int x,int k){//添加 
        for(int i=x;i<=n;i+=lowbit(i))c[i]+=k;
        return ;
    }

    inline int query(int x){//查询 
        int s=0;
        for(int i=x;i>0;i-=lowbit(i))s+=c[i];
        return s;
    }
}using namespace treeArray;

inline bool cmp(node a,node b){//按点权降序排序 
    return a.v>b.v;
}

signed main(){
    n=read();
    for(int i=1;i<=n;i++)a[i].v=read(),a[i].id=i;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++){
        add(a[i].id,1);
        sum+=query(a[i].id-1);
    }
    write(sum,'\n');
    return 0;
}

by jianhe @ 2024-12-23 13:42:53

@DX3906_ourstar 用 stable_sort


by jianhe @ 2024-12-23 13:45:21

或者 cmp 里判一下重复的


by DX3906_ourstar @ 2024-12-23 13:48:02

@jianhe用了 stable_sort 却变成了 30pts...


by DX3906_ourstar @ 2024-12-23 13:49:29

@jianheOK已 A,鉴定为 cmp 函数里少加了一个等号。

thx,已关


|