MLE 70pts 求调
查看原帖
MLE 70pts 求调
726862
zhanglh楼主2024/7/11 12:55

由于是多倍经验,把 P2717 寒假作业和 ARC075E 可以 AC 的代码改了一下交过来的。MLE 70pts,谢谢!

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
int n;
ll k, s[1000010], a[1000010], c;
map<ll, ll> mp;
void get(ll x) {
    if (mp.count(x)) return;
    mp[x] = ++ c;
}
ll lowbit(ll x) { return x & -x; }
void update(int x, ll k) {
    for (int i = x; i <= n + 1; i += lowbit(i)) a[i] += k;
}
ll query(int x) {
    ll ans = 0;
    for (int i = x; i > 0; i -= lowbit(i)) ans += a[i];
    return ans;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%lld", &a[i]);
    scanf("%lld", &k);
    for (int i = 0; i <= n; i ++ ) {
        a[i] -= k;
        if (i > 0) s[i] = s[i - 1] + a[i];
        else s[i] = a[i];
        a[i] = s[i];
    }
    sort(a, a + n + 1);
    for (int i = 0; i <= n; i ++ ) get(a[i]);
    memset(a, 0, sizeof a);
    ll cnt = 0;
    for (int i = 0; i <= n; i ++ ) {
        cnt += query(mp[s[i]]);
        update(mp[s[i]], 1);
    }
    printf("%lld\n", cnt);
    return 0;
}
2024/7/11 12:55
加载中...