离谱,线段树开4.19264倍数组正好能过,少1都不行

P1908 逆序对

StillEmpty @ 2021-11-14 15:57:12

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

int n, tar;
unsigned long long sum = 0;
int ranking[MAXN+1];
pair<int, int> a[MAXN+1];
int cnt[MAXN*4+1], l[MAXN*4+1], r[MAXN*4+1];

void init(int k, int lp, int rp) {
    l[k] = lp;
    r[k] = rp;
    if(lp < rp) {
        int mid = (lp+rp)/2;
        init(k*2, lp, mid);
        init(k*2+1, mid+1, rp);
    }
}

int query(int k) { // return the cnt of bucket(tar, maxa]
    if(r[k] <= tar) {
        return 0;
    }
    if(l[k] > tar) {
        return cnt[k];
    }
    return query(k*2)+query(k*2+1);
}

void update(int k) {
    if(l[k] > tar || r[k] < tar) {
        return;
    }
    cnt[k]++;
    update(k*2);
    update(k*2+1);
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a+1, a+n+1);
    for(int i = 1, j = 0; i <= n; i++) {
        if(a[i].first != a[i-1].first) {
            j++;
        }
        ranking[a[i].second] = j;
    }
    init(1, 1, n);
    for(int i = 1; i <= n; i++) {
        tar = ranking[i];
        sum += query(1);
        update(1);
    }
    cout << sum << endl;
    return 0;
}

但凡改成#define MAXN 524079就会re 50pts


by StillEmpty @ 2021-11-14 15:59:29

MAXN 524079

MAXN 524080


by MeowScore @ 2021-11-14 16:01:25

您是二分试出来的吗/jk


by StillEmpty @ 2021-11-14 16:05:52

@Liu_Kevin yep


by Lynkcat @ 2021-11-26 13:42:55

@Kobayashi 实测不是线段树数组的问题,而是你没有在线段树 update 的时候判断走到叶子就退出,使得你的线段树空间需求大于 4 倍。建议规范线段树写法(


by StillEmpty @ 2021-11-26 19:56:39

@LYC_music 虽然是一个脑瘫失误(但我没看出来),但没想到死贴竟然还有大佬指点,对洛谷的环境又多了一分乐观

我是sb


|