結果

問題 No.1300 Sum of Inversions
コンテスト
ユーザー otcohtc
提出日時 2026-05-13 06:04: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
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 1,554 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,786 ms
コンパイル使用メモリ 381,356 KB
実行使用メモリ 25,364 KB
最終ジャッジ日時 2026-05-13 06:04:42
合計ジャッジ時間 14,583 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using namespace std;
using namespace atcoder;

#define ll long long

using mint = modint998244353;
ll mod = 998244353;


ll op(ll a, ll b) { return (a + b) % mod; }
ll e() { return 0; }

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

    segtree<ll, op, e> rsum(n), rcnt(n), lsum(n), lcnt(n);
    vector<ll> 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) {
        if (a[i] != a[j]) return a[i] < a[j];
        return i < j;
    });
    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, 1);
    }

    ll answer = 0;
    sort(id.begin(), id.end(), [&](int i, int j) {
        if (a[i] != a[j]) return a[i] > a[j];
        return i > j;
    });
    for (auto &i: id)
    {
        auto s = lsum.prod(0, i);
        auto c = lcnt.prod(0, i);

        if (c > 0 && m_cnt[i] > 0)
        {
            answer += s * m_cnt[i] % mod;
            answer %= mod;
            answer += c * m_sum[i] % mod;
            answer %= mod;
            answer += (c * m_cnt[i] % mod) * a[i] % mod;
            answer %= mod;
        }

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

    cout << answer << "\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