結果

問題 No.1712 Read and Pile
ユーザー 👑 hitonanodehitonanode
提出日時 2021-10-04 21:34:19
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 842 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 1,639 ms
コンパイル使用メモリ 136,020 KB
実行使用メモリ 25,148 KB
最終ジャッジ日時 2023-10-17 19:59:23
合計ジャッジ時間 21,818 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 536 ms
14,396 KB
testcase_09 AC 753 ms
23,364 KB
testcase_10 AC 493 ms
14,660 KB
testcase_11 AC 603 ms
23,100 KB
testcase_12 AC 402 ms
14,660 KB
testcase_13 AC 528 ms
14,396 KB
testcase_14 AC 454 ms
14,660 KB
testcase_15 AC 655 ms
14,660 KB
testcase_16 AC 606 ms
14,924 KB
testcase_17 AC 534 ms
14,660 KB
testcase_18 AC 590 ms
14,396 KB
testcase_19 AC 528 ms
14,132 KB
testcase_20 AC 521 ms
14,660 KB
testcase_21 AC 667 ms
23,100 KB
testcase_22 AC 565 ms
23,100 KB
testcase_23 AC 670 ms
25,148 KB
testcase_24 AC 598 ms
25,148 KB
testcase_25 AC 690 ms
25,148 KB
testcase_26 AC 822 ms
25,148 KB
testcase_27 AC 813 ms
25,148 KB
testcase_28 AC 387 ms
14,660 KB
testcase_29 AC 613 ms
24,352 KB
testcase_30 AC 463 ms
23,560 KB
testcase_31 AC 178 ms
24,880 KB
testcase_32 AC 112 ms
23,824 KB
testcase_33 AC 539 ms
23,824 KB
testcase_34 AC 527 ms
23,628 KB
testcase_35 AC 764 ms
24,088 KB
testcase_36 AC 842 ms
24,616 KB
testcase_37 AC 776 ms
24,088 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 279 ms
8,680 KB
testcase_40 AC 237 ms
13,032 KB
testcase_41 AC 323 ms
13,076 KB
testcase_42 AC 285 ms
13,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;


#include <atcoder/modint>
using mint = atcoder::modint998244353;

#include <atcoder/lazysegtree>
struct S {
    mint v0, v1;
    int n;
};
S op(S l, S r) {
    return {l.v0 + r.v0, l.v1 + r.v1, l.n + r.n};
}
S e() {
    return {0, 0, 0};
}
struct F {
    mint nsame, ndiff;
};
S mapping(F f, S x) {
    return {x.v0 * f.nsame + x.v1 * f.ndiff, x.v1 * f.nsame + x.v0 * f.ndiff, x.n};
}
F composition(F fnew, F gold) {
    return {fnew.nsame * gold.nsame + fnew.ndiff * gold.ndiff, fnew.nsame * gold.ndiff + fnew.ndiff * gold.nsame};
}
F id() {
    return {1, 0};
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;

    mint ret = 0;
    vector<S> segtree_init(N + M, e());
    vector<int> seen_last(N);
    for (int i = 0; i < N; ++i) {
        segtree_init[i] = S{1, 0, 1};
        seen_last[N - 1 - i] = i;
    }

    atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> tree(segtree_init);
    int nempty = 0;
    const F f1{mint(N - 1) / N, mint(1) / N};
    for (int t = 0; t < M; ++t) {
        int a;
        cin >> a;
        --a;
        if (a < 0) {
            ++nempty;
            tree.apply(0, N + t, f1);
        } else {
            int l = seen_last[a], r = N + t;
            auto p = tree.prod(l + 1, r);
            ret += tree.get(l).v1 * (N - 1 - p.n) + p.v0;
            tree.set(l, e());
            tree.set(r, S{1, 0, 1});
            seen_last[a] = r;
        }
    }
    cout << (ret + M + mint(N - 1) / 2 * nempty).val() << '\n';
}
0