为什么全都MLE了?(线段树)

P1908 逆序对

NaN_HQJ2007_NaN @ 2021-02-09 22:17:06

#include <bits/stdc++.h>
using namespace std;

const int N = 5e5 + 5;
int n, p;
int a[N], tmp[N];
long long sum = 0, s[4 * N];
long long pushup(int id) {
    s[id] = s[id << 1] + s[id << 1 | 1];
}
void update(int id, int l, int r, int x) {
    if (l == x && r == x) {
        ++s[id];
        return;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) update(id << 1, l, mid, x);
    else update(id << 1 | 1, mid +1, r, x);
    pushup(id);
}
long long query(int id, int l, int r, int x, int y) {
    if (x <= l && r <= y) return s[id];
    int mid = (l + r) >> 1;
    long long ans = 0;
    if (x <= mid) ans += query(id << 1, l, mid, x, y);
    if (y > mid) ans += query(id << 1 | 1, mid + 1, r, x, y);
    return ans;
}

int main() {
    register int i;
    scanf("%d", &n);
    for (i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        tmp[i] = a[i];
    }
    sort(tmp + 1, tmp + n + 1);
    p = unique(tmp + 1, tmp + n + 1) - (tmp + 1);
    for (i = 1; i <= n; ++i) a[i] = lower_bound(tmp + 1, tmp + p + 1, a[i]) - tmp;
    for (i = n; i > 1; --i) {
        update(1, 1, p, a[i]);
        sum += query(1, 1, p, 1, a[i - 1] - 1);
    }
    printf("%lld\n", sum);
    return 0;
}

by NaN_HQJ2007_NaN @ 2021-02-09 22:28:13

哦,不用了,我知道了


|