WA on #6
查看原帖
WA on #6
255169
__LePetitPrince__楼主2023/10/23 23:37

cry

#include <stdio.h>
#include <algorithm>
using namespace std;
const int S = 1e6 + 5;
const int MOD = 92084931;
typedef long long LL;
struct N {
    LL v, id;
} s[S];
int n, m;
LL a[S], sum[S], b[S], c[S];
LL ans;
bool cmp(N a, N b) {
    if (a.v != b.v) {
        return a.v < b.v;
    } else {
        return a.id < b.id;
    }
}
LL lowbit(LL x) {
    return x & (-x);
}
void update(LL x) {
    for ( ; x <= n; x += lowbit(x)) {
        c[x] = c[x] + 1;
    }
}
LL query(LL x) {
    LL ret = 0;
    for ( ; x; x -= lowbit(x)) {
        ret = c[x] + ret;
    }
    return ret;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        a[i] -= m;
        sum[i] = sum[i - 1] + a[i];
        s[i].v = sum[i];
        s[i].id = i;
    }
    scanf("%d", &m);
	for (int i = 1; i <= n; i++) {
        a[i] -= m;
        sum[i] = sum[i - 1] + a[i];
        s[i].v = sum[i];
        s[i].id = i;
    }
    s[0].v = 0;
    s[0].id = 0;
    sort(s, s + n + 1, cmp);
    int cnt = 1;
    for (int i = 0; i <= n; i++) {
        if (i == 0) {
            b[s[i].id + 1] = cnt;
        } else {
            if (s[i].v != s[i - 1].v) {
                cnt++;
            }
            b[s[i].id + 1] = cnt;
        }
    }
    for (int i = 1; i <= n + 1; i++) {
        LL t = query(b[i]);
        ans = ans + t;
        update(b[i]);
    }
    printf("%lld", ans);
    return 0;
}
2023/10/23 23:37
加载中...