結果

問題 No.1300 Sum of Inversions
ユーザー 259_Momone259_Momone
提出日時 2020-11-27 22:16:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 2,718 bytes
コンパイル時間 2,358 ms
コンパイル使用メモリ 210,656 KB
実行使用メモリ 18,756 KB
最終ジャッジ日時 2023-10-01 06:26:42
合計ジャッジ時間 8,460 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 135 ms
14,988 KB
testcase_04 AC 132 ms
14,868 KB
testcase_05 AC 108 ms
12,824 KB
testcase_06 AC 152 ms
16,532 KB
testcase_07 AC 145 ms
15,876 KB
testcase_08 AC 161 ms
17,248 KB
testcase_09 AC 165 ms
17,148 KB
testcase_10 AC 88 ms
11,072 KB
testcase_11 AC 87 ms
11,072 KB
testcase_12 AC 136 ms
14,848 KB
testcase_13 AC 132 ms
14,476 KB
testcase_14 AC 183 ms
18,436 KB
testcase_15 AC 163 ms
17,000 KB
testcase_16 AC 144 ms
15,432 KB
testcase_17 AC 86 ms
10,768 KB
testcase_18 AC 99 ms
12,076 KB
testcase_19 AC 121 ms
13,844 KB
testcase_20 AC 120 ms
13,772 KB
testcase_21 AC 121 ms
13,924 KB
testcase_22 AC 108 ms
12,868 KB
testcase_23 AC 157 ms
16,604 KB
testcase_24 AC 116 ms
13,168 KB
testcase_25 AC 95 ms
11,628 KB
testcase_26 AC 93 ms
11,488 KB
testcase_27 AC 107 ms
12,496 KB
testcase_28 AC 168 ms
17,480 KB
testcase_29 AC 123 ms
13,720 KB
testcase_30 AC 162 ms
17,076 KB
testcase_31 AC 108 ms
12,880 KB
testcase_32 AC 118 ms
13,240 KB
testcase_33 AC 83 ms
18,724 KB
testcase_34 AC 131 ms
18,728 KB
testcase_35 AC 105 ms
18,700 KB
testcase_36 AC 123 ms
18,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

int main(){
    using namespace std;
    const auto N{[]{unsigned long N; cin >> N; return N;}()};
    auto perm{[&N]{
        vector<pair<unsigned long, unsigned long>> ret(N);
        unsigned long idx{0};
        for(auto&& [p, i] : ret){
            cin >> p;
            i = idx++;
        }
        return ret;
    }()};
    const auto small{[&N, &perm]{
        sort(begin(perm), end(perm));
        vector<complex<unsigned long>> segtree(N * 2);
        vector<pair<unsigned long, unsigned long>> ret(N);
        const auto update{[&N, &segtree](unsigned long idx, const auto& val){
            idx += N;
            segtree[idx] += val;
            while(idx >>= 1UL)segtree[idx] += val;
        }};
        const auto fold{[&N, &segtree](unsigned long l, unsigned long r){
            decltype(segtree)::value_type ret{};
            l += N;
            r += N;
            while(l < r){
                if(l & 1UL)ret += segtree[l++];
                if(r & 1UL)ret += segtree[--r];
                l >>= 1UL;
                r >>= 1UL;
            }
            return ret;
        }};
        for(const auto& [p, i] : perm){
            const auto& tmp{fold(i, N)};
            ret[i] = {tmp.real(), tmp.imag()};
            update(i, complex{p, 1UL});
        }
        return ret;
    }()};
    const auto large{[&N, &perm]{
        reverse(begin(perm), end(perm));
        vector<complex<unsigned long>> segtree(N * 2);
        vector<pair<unsigned long, unsigned long>> ret(N);
        const auto update{[&N, &segtree](unsigned long idx, const auto& val){
            idx += N;
            segtree[idx] += val;
            while(idx >>= 1UL)segtree[idx] += val;
        }};
        const auto fold{[&N, &segtree](unsigned long l, unsigned long r){
            decltype(segtree)::value_type ret{};
            l += N;
            r += N;
            while(l < r){
                if(l & 1UL)ret += segtree[l++];
                if(r & 1UL)ret += segtree[--r];
                l >>= 1UL;
                r >>= 1UL;
            }
            return ret;
        }};
        for(const auto& [p, i] : perm){
            const auto& tmp{fold(0, i)};
            ret[i] = {tmp.real(), tmp.imag()};
            update(i, complex{p, 1UL});
        }
        return ret;
    }()};
    unsigned long ans{0};
    sort(begin(perm), end(perm), [](const auto& i, const auto& j){return i.second < j.second;});
    constexpr unsigned long MOD{998244353};
    for(unsigned long i{0}; i < N; ++i)ans += small[i].second * large[i].second % MOD * perm[i].first % MOD + small[i].second * large[i].first % MOD + small[i].first * large[i].second % MOD;
    cout << ans % MOD << endl;
    return 0;
}
0