結果

問題 No.1733 Sum of Sorted Subarrays
ユーザー daddydaddy
提出日時 2022-12-09 03:12:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 515 ms / 3,000 ms
コード長 1,552 bytes
コンパイル時間 1,683 ms
コンパイル使用メモリ 95,564 KB
実行使用メモリ 15,760 KB
最終ジャッジ日時 2024-04-22 19:00:20
合計ジャッジ時間 11,560 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 281 ms
10,216 KB
testcase_09 AC 435 ms
14,060 KB
testcase_10 AC 253 ms
9,496 KB
testcase_11 AC 314 ms
10,756 KB
testcase_12 AC 341 ms
12,684 KB
testcase_13 AC 341 ms
12,568 KB
testcase_14 AC 497 ms
15,360 KB
testcase_15 AC 462 ms
14,544 KB
testcase_16 AC 247 ms
9,640 KB
testcase_17 AC 503 ms
15,388 KB
testcase_18 AC 388 ms
13,428 KB
testcase_19 AC 417 ms
13,800 KB
testcase_20 AC 376 ms
13,184 KB
testcase_21 AC 448 ms
14,528 KB
testcase_22 AC 296 ms
10,380 KB
testcase_23 AC 515 ms
15,500 KB
testcase_24 AC 515 ms
15,596 KB
testcase_25 AC 514 ms
15,760 KB
testcase_26 AC 429 ms
15,644 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