蒟蒻样例过了但全RE?
  • 板块P1908 逆序对
  • 楼主OcTar
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/7/19 08:00
  • 上次更新2023/11/3 08:59:32
查看原帖
蒟蒻样例过了但全RE?
594916
OcTar楼主2023/7/19 08:00

rt

用树状数组求的

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 2000100;
int n, a[maxn];
ll bit[maxn];

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

ll query(int x) {
    ll res = 0;
    while (x > 0) {
        res += bit[x];
        x -= lowbit(x);
    }
    return res;
}
void add(int pos, int x) {
    while (pos <= n) {
        bit[pos] += x;
        pos += lowbit(pos);
    }
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += (query(n) - query(a[i]));
        add(a[i], 1);
    }
    printf("%lld\n", ans);
    return 0;
}
2023/7/19 08:00
加载中...