50分求助

P1908 逆序对

hhyj @ 2022-01-29 11:38:32

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

const int N = 500005;
int a[N], b[N], l[N], tr[N], n;

void add(int x, int c) {
    for (int i = x; i <= n; i += lowbit(i))
        tr[i] += c;
}

int sum(int x) {
    int res = 0;
    for (int i = x; i; i -= lowbit(i))
        res += tr[i];
    return res;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++) scanf("%d", &a[i]);

    memcpy(b, a, sizeof a);
    sort(b, b + n);
    int len = unique(b, b + n) - b;
    for (int i = 0; i < n; i ++)
        l[i] = lower_bound(b, b + len, a[i]) - b + 1;

    int ans = 0;
    for (int i = n-1; i >= 0; i--) {
        ans += sum(l[i]-1);
        add(l[i], 1);
    }

    cout << ans;

    return 0;
}

by QcpyWcpyQ @ 2022-02-01 15:15:35

十年OI一场空,不开____见祖宗。

ans要开long long


|