結果

問題 No.1300 Sum of Inversions
ユーザー ooaiu
提出日時 2024-12-16 13:57:02
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,205 bytes
コンパイル時間 3,623 ms
コンパイル使用メモリ 260,532 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-12-16 13:57:14
合計ジャッジ時間 10,527 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#include <bits/stdc++.h>

using namespace std;

#define debug(...) (void(0))
#else
#include "algo/debug.h"
#endif

#include <atcoder/modint>
using mint = atcoder::modint998244353;
#include <atcoder/fenwicktree>
void solve() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto& x : A) cin >> x;
    //
    vector<int> B(A.begin(), A.end());
    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());
    for (auto& x : A) x = distance(B.begin(), lower_bound(B.begin(), B.end(), x));
    //
    atcoder::fenwick_tree<mint> L(N), R(N);
    atcoder::fenwick_tree<int64_t> cntL(N), cntR(N);
    for (const auto& x : A) R.add(x, B[x]), cntR.add(x, 1);

    mint ans = 0;
    for (const auto& x : A) {
        R.add(x, -B[x]);
        cntR.add(x, -1);

        ans += R.sum(0, x) * cntL.sum(x + 1, N);
        ans += L.sum(x + 1, N) * cntR.sum(0, x);
        ans += mint(cntL.sum(x + 1, N) * cntR.sum(0, x)) * B[x];

        L.add(x, +B[x]);
        cntL.add(x, 1);
    }
    cout << ans.val() << endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int tt = 1;
    // std::cin >> tt;
    while (tt--) {
        solve();
    }
}
0