結果

問題 No.1733 Sum of Sorted Subarrays
ユーザー sapphire__15sapphire__15
提出日時 2021-10-20 16:08:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 458 ms / 3,000 ms
コード長 1,552 bytes
コンパイル時間 1,692 ms
コンパイル使用メモリ 109,764 KB
実行使用メモリ 15,760 KB
最終ジャッジ日時 2024-04-24 04:55:50
合計ジャッジ時間 8,978 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 242 ms
10,092 KB
testcase_09 AC 363 ms
14,188 KB
testcase_10 AC 215 ms
9,628 KB
testcase_11 AC 273 ms
10,880 KB
testcase_12 AC 303 ms
12,680 KB
testcase_13 AC 300 ms
12,672 KB
testcase_14 AC 434 ms
15,360 KB
testcase_15 AC 399 ms
14,676 KB
testcase_16 AC 241 ms
9,640 KB
testcase_17 AC 439 ms
15,388 KB
testcase_18 AC 334 ms
13,428 KB
testcase_19 AC 360 ms
13,856 KB
testcase_20 AC 320 ms
13,180 KB
testcase_21 AC 387 ms
14,652 KB
testcase_22 AC 263 ms
10,396 KB
testcase_23 AC 451 ms
15,760 KB
testcase_24 AC 458 ms
15,624 KB
testcase_25 AC 456 ms
15,628 KB
testcase_26 AC 385 ms
15,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <atcoder/lazysegtree>
#include <atcoder/modint>
#include <functional>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

using mint = atcoder::modint998244353;

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

mint e() {
    return mint(0);
}

mint mapping(mint f, mint x) {
    return f * x;
}

mint composition(mint f, mint g) {
    return f * g;
}

mint id() {
    return mint(1);
}

vector<size_t> sorted_idx(const vector<size_t> A, bool rev) {
    auto n = A.size();
    vector<pair<size_t, long long>> tmp(n);
    for (size_t i = 0; i < n; i++) tmp[i] = {A[i], rev ? -i : i};
    stable_sort(begin(tmp), end(tmp));
    vector<size_t> ret(n);
    for (size_t i = 0; i < n; i++) ret[i] = abs(tmp[i].second);
    return ret;
}

vector<mint> cumulative_sum(const vector<size_t> &A, bool rev = false) {
    auto n = A.size();
    atcoder::lazy_segtree<mint, op, e, mint, mapping, composition, id> lst(
        vector<mint>(n, 1));
    vector<mint> ret(n);
    auto &&tmp = sorted_idx(A, rev);
    for (auto &i : tmp) {
        ret[i] = lst.prod(0, i + 1) / lst.get(i);
        lst.apply(0, i + 1, mint(2));
    }
    return ret;
}

int main() {
    size_t n;
    cin >> n;
    vector<size_t> A(n);
    for (size_t i = 0; i < n; i++) cin >> A[i];
    auto &&x = cumulative_sum(A);
    reverse(begin(A), end(A));
    auto &&y = cumulative_sum(A, true);
    reverse(begin(x), end(x));
    mint ans;
    for (size_t i = 0; i < n; i++) { ans += x[i] * y[i] * A[i]; }
    cout << ans.val() << endl;
}
0