35分WA求助

P1908 逆序对

X2H_tato @ 2023-08-11 08:26:40

不知道哪错了,跪谢有无大佬看一下

#include <bits/stdc++.h>
using namespace std;
int tree[500005];
int n;

struct node {
    int s, id;
    bool operator <(node oth) const {
        return s > oth.s;
    }
} a[500005];

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

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

long long sol (int x) {
    int ans = 0;
    for (int i = x; i >= 1; i -= lowbit(i))
        ans += tree[i];
    return ans;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i].s);
        a[i].id = i;
    }
    sort(a + 1, a + n + 1);
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        add(a[i].id);
        ans += sol(a[i].id - 1);
    }
    cout << ans;
    return 0;
}

by xiehy @ 2023-08-11 08:57:46

struct有问题 如果两个数相等你该怎么办


by X2H_tato @ 2023-08-11 09:51:38

@xiehy 谢谢大佬,已经解决了


|