不吸氧就T?

P1908 逆序对

xiaozhangma @ 2023-10-10 23:23:21

RT

//code by xiaozhangma
#include<bits/stdc++.h>
#define LL long long
#define F(x,s,t) for(int x=s;x<=t;x++)
#define lowbit(x) (x & -x)
using namespace std;
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void write(LL x){
    if(x < 0)putchar('-'),x = -x;
    if(x > 9)write(x / 10);
    putchar(x % 10 + '0');
}
const int N = 5e5 + 10;
LL n, a[N], b[N], tr[N];
map <int, int> m;
void add(int x,int k){
    while(x <= n){
        tr[x] += k;
        x += lowbit(x);
    }
}
LL ask(int x){
    LL ans = 0;
    while(x){
        ans += tr[x];
        x -= lowbit(x);
    }
    return ans;
}
int main(){
    n = read();
    F(i, 1, n){
        a[i] = read();
        b[i] = a[i];
    }

    sort(b + 1, b + n + 1);
    int idx = 0;
    F(i, 1, n){
        if(!m[b[i]])m[b[i]] = ++ idx ;
    }

    LL ans = 0; 
    /*
    F(i, 1, n){
        ans += ask(idx);
        ans -= ask(m[a[i]] - 1);
        add(m[a[i]], 1);
    }
    */
    for(int i = n; i; i --){
        ans += ask(m[a[i]] - 1);
        add(m[a[i]], 1);
    }
    write(ans);
    puts("");
    return 0;
}

不吸氧 吸氧


by zhouzihang3 @ 2023-10-10 23:25:19

O2的好处,但是还是要优化,万一考场上没有O2还得用#define int long long


by Kuio @ 2023-10-10 23:35:57

你真猜这是什么 map <int, int> m;


by Doubleen @ 2023-10-10 23:58:14

用unordered_map试试


|