結果

問題 No.1300 Sum of Inversions
ユーザー Manuel1024Manuel1024
提出日時 2022-01-11 03:22:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 2,260 bytes
コンパイル時間 946 ms
コンパイル使用メモリ 79,896 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-04-26 20:29:26
合計ジャッジ時間 6,900 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 137 ms
10,496 KB
testcase_04 AC 134 ms
10,240 KB
testcase_05 AC 110 ms
9,088 KB
testcase_06 AC 157 ms
11,264 KB
testcase_07 AC 147 ms
11,136 KB
testcase_08 AC 162 ms
12,032 KB
testcase_09 AC 168 ms
11,776 KB
testcase_10 AC 87 ms
8,064 KB
testcase_11 AC 88 ms
8,064 KB
testcase_12 AC 135 ms
10,368 KB
testcase_13 AC 132 ms
10,240 KB
testcase_14 AC 179 ms
12,416 KB
testcase_15 AC 163 ms
11,648 KB
testcase_16 AC 138 ms
10,624 KB
testcase_17 AC 84 ms
7,936 KB
testcase_18 AC 99 ms
8,704 KB
testcase_19 AC 118 ms
9,472 KB
testcase_20 AC 116 ms
9,600 KB
testcase_21 AC 120 ms
9,600 KB
testcase_22 AC 108 ms
9,088 KB
testcase_23 AC 161 ms
11,264 KB
testcase_24 AC 113 ms
9,216 KB
testcase_25 AC 95 ms
8,448 KB
testcase_26 AC 95 ms
8,320 KB
testcase_27 AC 106 ms
8,960 KB
testcase_28 AC 172 ms
12,160 KB
testcase_29 AC 123 ms
9,600 KB
testcase_30 AC 165 ms
11,648 KB
testcase_31 AC 110 ms
9,216 KB
testcase_32 AC 115 ms
9,344 KB
testcase_33 AC 111 ms
12,544 KB
testcase_34 AC 162 ms
12,544 KB
testcase_35 AC 114 ms
12,800 KB
testcase_36 AC 140 ms
12,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;

template <typename T>
struct FenwickTree{
    const int n;
    vector<T> arr;
    FenwickTree() = default;
    FenwickTree(int n): n(n), arr(vector<T>(n+1, 0)){}

    void add(int ind, T x){
        for(int i = ind+1; i <= n; i += i & (-i)){
            arr[i] += x;
        }
    }

    T sum_sub(int end){
        T res = 0;
        for(int i = end; i > 0; i -= i & (-i)){
            res += arr[i];
        }
        return res;
    }

    T sum(int start, int end){
        return sum_sub(end) - sum_sub(start);
    }

    int lower_bound(T w){
        if(w <= 0) return 0;
        int x = 0;
        int k = 1 << 30;
        while(k > n) k /= 2;
        for(; k > 0; k /= 2){
            if(x+k <= n && arr[x+k] < w){
                w -= arr[x+k];
                x += k;
            }
        }
        return x;
    }
};

template <typename T>
vector<T> compress(vector<T> &x){
    vector<T> vals = x;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    for(auto &p: x){
        p = lower_bound(vals.begin(), vals.end(), p) - vals.begin();
    }
    return vals;
}

int main(){
    int n;
    cin >> n;
    vector<ll> a(n);
    for(auto &it: a) cin >> it;
    auto val = compress(a);

    ll ans = 0;
    FenwickTree<ll> cntl(n+1), cntr(n+1);
    FenwickTree<ll> totl(n+1), totr(n+1);
    for(int i = 0; i < n; i++){
        cntr.add(a[i], 1);
        totr.add(a[i], val[a[i]]);
    }
    for(int i = 0; i < n; i++){
        cntr.add(a[i], -1);
        totr.add(a[i], -val[a[i]]);

        ans += (cntl.sum(a[i]+1, n+1)*cntr.sum(0, a[i]))%MOD*val[a[i]];
        ans %= MOD;
        // cout << ans << " " << val[a[i]] << " ";
        if(cntl.sum(a[i]+1, n+1)*cntr.sum(0, a[i]) > 0){
            ans += totr.sum(0, a[i])%MOD*cntl.sum(a[i]+1, n+1);
            ans %= MOD;
            ans += totl.sum(a[i]+1, n+1)%MOD*cntr.sum(0, a[i]);
            ans %= MOD;
        }
        // cout << cntl.sum(a[i]+1, n+1) << " " << cntr.sum(0, a[i]) << " " << ans << endl;
        cntl.add(a[i], 1);
        totl.add(a[i], val[a[i]]);
    }

    cout << ans << endl;
    return 0;
}
0