結果
問題 | No.1712 Read and Pile |
ユーザー |
![]() |
提出日時 | 2021-10-04 21:34:19 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 634 ms / 2,000 ms |
コード長 | 1,578 bytes |
コンパイル時間 | 1,665 ms |
コンパイル使用メモリ | 136,040 KB |
実行使用メモリ | 25,148 KB |
最終ジャッジ日時 | 2024-09-17 17:11:15 |
合計ジャッジ時間 | 17,178 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 40 |
ソースコード
#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';}