結果

問題 No.1300 Sum of Inversions
ユーザー trineutrontrineutron
提出日時 2020-11-27 22:16:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 1,808 bytes
コンパイル時間 2,706 ms
コンパイル使用メモリ 208,644 KB
実行使用メモリ 14,792 KB
最終ジャッジ日時 2023-10-01 06:25:17
合計ジャッジ時間 8,707 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 136 ms
12,068 KB
testcase_04 AC 133 ms
11,868 KB
testcase_05 AC 107 ms
10,488 KB
testcase_06 AC 155 ms
13,008 KB
testcase_07 AC 149 ms
12,796 KB
testcase_08 AC 164 ms
13,656 KB
testcase_09 AC 163 ms
13,668 KB
testcase_10 AC 89 ms
9,116 KB
testcase_11 AC 89 ms
9,092 KB
testcase_12 AC 134 ms
11,768 KB
testcase_13 AC 132 ms
11,700 KB
testcase_14 AC 183 ms
14,672 KB
testcase_15 AC 165 ms
13,744 KB
testcase_16 AC 142 ms
12,524 KB
testcase_17 AC 87 ms
8,792 KB
testcase_18 AC 99 ms
9,532 KB
testcase_19 AC 120 ms
10,848 KB
testcase_20 AC 124 ms
10,904 KB
testcase_21 AC 121 ms
10,896 KB
testcase_22 AC 108 ms
10,508 KB
testcase_23 AC 154 ms
13,016 KB
testcase_24 AC 112 ms
10,692 KB
testcase_25 AC 95 ms
9,576 KB
testcase_26 AC 94 ms
9,368 KB
testcase_27 AC 105 ms
10,180 KB
testcase_28 AC 169 ms
13,804 KB
testcase_29 AC 121 ms
11,100 KB
testcase_30 AC 168 ms
13,592 KB
testcase_31 AC 110 ms
10,324 KB
testcase_32 AC 113 ms
10,636 KB
testcase_33 AC 93 ms
14,716 KB
testcase_34 AC 143 ms
14,644 KB
testcase_35 AC 119 ms
14,792 KB
testcase_36 AC 136 ms
14,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    constexpr int64_t mod = 998244353;
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a.at(i);
    }
    reverse(a.begin(), a.end());
    vector<int> b = a;
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), b.end());
    vector<int> c(n);
    for (int i = 0; i < n; i++) {
        c.at(i) = lower_bound(b.begin(), b.end(), a.at(i)) - b.begin();
    }
    vector<int64_t> s(n + 1), t(n + 1);
    vector<int64_t> s0(n), t0(n);
    for (int i = 0; i < n; i++) {
        int x = c.at(i);
        while (x) {
            (s0.at(i) += s.at(x)) %= mod;
            t0.at(i) += t.at(x);
            x -= x & -x;
        }
        x = c.at(i) + 1;
        while (x <= n) {
            (s.at(x) += b.at(c.at(i))) %= mod;
            t.at(x)++;
            x += x & -x;
        }
    }
    for (int i = 0; i <= n; i++) {
        s.at(i) = 0;
        t.at(i) = 0;
    }
    reverse(c.begin(), c.end());
    vector<int64_t> s1(n), t1(n);
    int64_t ssum = 0;
    for (int i = 0; i < n; i++) {
        int x = c.at(i) + 1;
        s1.at(i) = ssum;
        t1.at(i) = i;
        (ssum += b.at(c.at(i))) %= mod;
        while (x) {
            (s1.at(i) -= s.at(x)) %= mod;
            t1.at(i) -= t.at(x);
            x -= x & -x;
        }
        x = c.at(i) + 1;
        while (x <= n) {
            (s.at(x) += b.at(c.at(i))) %= mod;
            t.at(x)++;
            x += x & -x;
        }
    }
    int64_t ans = 0;
    for (int i = 0; i < n; i++) {
        int p = n - 1 - i;
        ans += a.at(i) * t0.at(i) % mod * t1.at(p) % mod;
        ans += s0.at(i) * t1.at(p) % mod;
        ans += t0.at(i) * s1.at(p) % mod;
        ans %= mod;
    }
    cout << (ans + mod) % mod << endl;
}
0