问一个玄学的问题
查看原帖
问一个玄学的问题
207812
Chouquet楼主2021/1/28 12:53
#include <cstdio>
#include <cstring>
#define lowbit(x) (x & -x)
int n, a[20003], MAXA = -(1 << 30), tr1[100003], tr2[100003], t;
long long c[100003], d[100003], ans;
void update(int c[], int x, int k) {
    while (x <= MAXA)
        c[x] += k, x += lowbit(x);
}
int query(int c[], int x) {
    int s = 0;
    while (x > 0)
        s += c[x], x -= lowbit(x);
    return s;
}
int main() {
    scanf("%d", &t);
    while (t--) {
        ans = 0;
        memset(tr1, 0, sizeof tr1), memset(tr2, 0, sizeof tr2);
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            ++a[i];
            if (MAXA < a[i])
                MAXA = a[i];
        }
        for (int i = 1; i <= n; i++)
            update(tr2, a[i], 1);
        for (int i = 1; i <= n; i++) {
            update(tr1, a[i], 1), c[i] = query(tr1, a[i] - 1);
            update(tr2, a[i], -1), d[i] = query(tr2, a[i] - 1);
        }
        for (int i = 1; i <= n; i++)
            ans += c[i] * (n - i - d[i]) + d[i] * (i - 1 - c[i]);
        printf("%lld\n", ans);
    }
    return 0;
}

这是这道题我写的代码,在洛谷上过了。但是用P1637的样例测试,第二个样例输出的是8,为什么会这样啊?

注:本题跟P1637题面叙述一样

2021/1/28 12:53
加载中...