結果

問題 No.1300 Sum of Inversions
ユーザー 37zigen37zigen
提出日時 2020-12-18 23:40:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 1,233 bytes
コンパイル時間 884 ms
コンパイル使用メモリ 88,000 KB
実行使用メモリ 11,256 KB
最終ジャッジ日時 2023-10-21 08:27:49
合計ジャッジ時間 8,465 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 126 ms
9,408 KB
testcase_04 AC 120 ms
9,144 KB
testcase_05 AC 97 ms
8,352 KB
testcase_06 AC 142 ms
10,200 KB
testcase_07 AC 138 ms
9,672 KB
testcase_08 AC 160 ms
10,464 KB
testcase_09 AC 156 ms
10,464 KB
testcase_10 AC 88 ms
7,296 KB
testcase_11 AC 84 ms
7,560 KB
testcase_12 AC 118 ms
9,144 KB
testcase_13 AC 115 ms
9,144 KB
testcase_14 AC 168 ms
10,992 KB
testcase_15 AC 159 ms
10,464 KB
testcase_16 AC 135 ms
9,408 KB
testcase_17 AC 80 ms
7,296 KB
testcase_18 AC 91 ms
7,824 KB
testcase_19 AC 105 ms
8,616 KB
testcase_20 AC 106 ms
8,616 KB
testcase_21 AC 105 ms
8,616 KB
testcase_22 AC 93 ms
8,088 KB
testcase_23 AC 145 ms
10,200 KB
testcase_24 AC 101 ms
8,352 KB
testcase_25 AC 89 ms
7,560 KB
testcase_26 AC 84 ms
7,560 KB
testcase_27 AC 96 ms
8,088 KB
testcase_28 AC 155 ms
10,728 KB
testcase_29 AC 107 ms
8,616 KB
testcase_30 AC 150 ms
10,464 KB
testcase_31 AC 97 ms
8,352 KB
testcase_32 AC 108 ms
8,352 KB
testcase_33 AC 83 ms
11,256 KB
testcase_34 AC 134 ms
11,256 KB
testcase_35 AC 107 ms
11,256 KB
testcase_36 AC 124 ms
11,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
using ll = long long;

const ll p = 998244353;

struct BIT {
    vector<ll> a;
    BIT(int n) {
        a.resize(n + 1);
    }

    void add(int k, int v) {
        v %= p;
        while (k < a.size()) {
            a[k] += v;
            a[k] %= p;
            k  += k & -k;
        }
    }

    ll sum(int k) {
        ll ret = 0;
        while (k > 0) {
            ret += a[k];
            ret %= p;
            k -= k & -k;
        }
        return ret;
    }
};

int main() {
    int N;
    cin >> N;
    vector<pair<int, int>> ps(N);
    for (int i=0;i<N;++i) {
        ll a;
        cin >> a;
        ps[i] = make_pair(-a, -i);
    }
    sort(ps.begin(), ps.end());
    BIT bit0(N);
    BIT bit1(N);
    BIT bit2(N);
    BIT bit3(N);
    ll ans = 0;
    for (auto e:ps) {
        int id = 1 + -e.second;
        ll cost = -e.first;
        bit0.add(id, 1);
        bit1.add(id, cost);
        bit2.add(id, bit1.sum(id - 1) + bit0.sum(id - 1) * cost % p);
        bit3.add(id, bit0.sum(id - 1));
        ans += bit2.sum(id - 1) + bit3.sum(id - 1) * cost % p;
        ans %= p;
    }
    cout << ans << endl;

}
0