树状数组35pts全WA

P1908 逆序对

libmwlmgrimpl @ 2022-03-22 22:02:32

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

int n, ans;
int c[10000005];

struct node {
    int x, y;
} a[10000005];

int lowbit(int x) {
    return x & (-x);
}

void upd(int x, int y) {
    while (x <= n) c[x] += y, x += lowbit(x);
}

int sum(int x) {
    int t = 0;
    while (x > 0) t += c[x], x -= lowbit(x);
    return t;
}

bool cmp(node x, node y) {
    return x.x > y.x;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].x;
        a[i].y = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
        upd(a[i].y, 1);
        ans += sum(a[i].y - 1);
    }
    cout << ans << endl;
    return 0;
}

各位大佬帮忙看一下


by Rad_Forever @ 2022-03-22 22:08:45

@dotodot 你 a 不离散化一下吗?


by Rad_Forever @ 2022-03-22 22:09:30

只是按照排序后的顺序离散化,相同的数字就离散出了不同的值。


by libmwlmgrimpl @ 2022-03-26 21:58:33

@Rad_Forever 知道了,感谢


|