結果

問題 No.3371 Add Insert Operations
コンテスト
ユーザー 小鸡鸡
提出日時 2026-06-11 01:10:53
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,645 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 9,946 ms
コンパイル使用メモリ 173,864 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2026-06-11 01:11:08
合計ジャッジ時間 9,514 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

constexpr int N = 200000 + 1, P = 998244353;

int pre[N], nxt[N], a[N], p[N], stk[N], l[N], r[N], fa[N], fac[N], ifac[N], ord[N], sz[N];
bitset<N> vis;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int top = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        pre[i] = i - 1;
        nxt[i] = i + 1;
        if (a[i] == 0) {
            stk[++top] = i;
        }
    }
    nxt[n] = 0;
    for (int i = 1; i <= n; ++i) {
        while (top && vis[stk[top]]) {
            --top;
        }
        if (!top) {
            cout << "0\n";
            return 0;
        }
        int x = stk[top--];
        p[x] = n + 1 - i;
        a[x] = 1 << 30;
        int l = pre[x], r = nxt[x];
        if (l) {
            if (--a[l] < 0) {
                cout << "0\n";
                return 0;
            }
            if (!a[l]) {
                stk[++top] = l;
            }
            nxt[l] = r;
        }
        if (r) {
            if (--a[r] < 0) {
                cout << "0\n";
                return 0;
            }
            if (a[r] == 0) {
                stk[++top] = r;
            }
            pre[r] = l;
        }
    }
    top = 0;
    for (int i = 1; i <= n; ++i) {
        if (p[i] < p[stk[top]]) {
            --top;
            while (top && p[i] < p[stk[top]]) {
                --top;
            }
            l[i] = stk[top + 1];
            fa[stk[top + 1]] = i;
        }
        r[stk[top]] = i;
        fa[i] = stk[top];
        stk[++top] = i;
    }
    auto qPow = [](int a, int b) {
        int res = 1;
        for ( ; b; b >>= 1) {
            if (b & 1) {
                res = (ll)res * a % P;
            }
            a = (ll)a * a % P;
        }
        return res;
    };

    fac[0] = 1;
    for (int i = 1; i <= n; ++i) {
        fac[i] = (ll)fac[i - 1] * i % P;
    }
    ifac[n] = qPow(fac[n], P - 2);
    for (int i = n; i; --i) {
        ifac[i - 1] = ifac[i] * (ll)i % P;
    }
    auto inv = [](int x) {
        return fac[x - 1] * (ll)ifac[x] % P;
    };

    int rt = 1;
    while (fa[rt]) {
        ++rt;
    }
    stk[top = 1] = rt;
    int idx = 0;
    while (top) {
        int u = stk[top--];
        ord[++idx] = u;
        if (l[u]) {
            stk[++top] = l[u];
        }
        if (r[u]) {
            stk[++top] = r[u];
        }
    }
    int ans = fac[n];
    for (int i = n; i; --i) {
        int u = ord[i];
        sz[u] = sz[l[u]] + sz[r[u]] + 1;
        ans = (ll)ans * inv(sz[u]) % P;
    }
    cout << ans << '\n';
    return 0;
}   
0