結果

問題 No.1300 Sum of Inversions
コンテスト
ユーザー otcohtc
提出日時 2026-05-13 05:52:24
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,316 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,351 ms
コンパイル使用メモリ 382,120 KB
実行使用メモリ 14,856 KB
最終ジャッジ日時 2026-05-13 05:52:36
合計ジャッジ時間 8,691 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 3 WA * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

#define ll long long

using mint = modint998244353;


mint op(mint a, mint b) { return a + b; }
mint e() { return mint(0); }

void solve()
{
    int n; cin >> n;
    vector<mint> a(n);
    for(int i=0; i<n; ++i) 
    {
        ll x; cin >> x;
        a[i] = mint(x);
    }

    segtree<mint, op, e> rsum(n), rcnt(n), lsum(n), lcnt(n);
    vector<mint> m_sum(n, 0), m_cnt(n, 0);
    vector<int> id(n);
    iota(id.begin(), id.end(), 0);

    sort(id.begin(), id.end(), [&](int i, int j) { return a[i].val() < a[j].val(); });
    for (auto &i: id)
    {
        m_sum[i] = rsum.prod(i, n);
        m_cnt[i] = rcnt.prod(i, n);
        rsum.set(i, a[i]);
        rcnt.set(i, mint(1));
    }

    mint answer = mint(0);
    reverse(id.begin(), id.end());
    for (auto &i: id)
    {
        mint s = lsum.prod(0, i);
        mint c = lcnt.prod(0, i);

        answer += s * m_cnt[i];
        answer += c * m_sum[i];
        answer += (c * m_cnt[i]) * a[i];

        lsum.set(i, a[i]);
        lcnt.set(i, mint(1));
    }

    cout << answer.val() << "\n";
}


int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t = 1;
    // std::cin >> t;
    while (t--)
        solve();

    return 0;
}
0