結果
| 問題 | No.3371 Add Insert Operations |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-06-11 01:11:14 |
| 言語 | C++17(gcc12) (gcc 12.4.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 18 ms / 2,000 ms |
| コード長 | 2,645 bytes |
| 記録 | |
| コンパイル時間 | 3,309 ms |
| コンパイル使用メモリ | 206,436 KB |
| 実行使用メモリ | 12,544 KB |
| 最終ジャッジ日時 | 2026-06-11 01:11:23 |
| 合計ジャッジ時間 | 4,962 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_1 |
| 純コード判定待ち |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 36 |
ソースコード
#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;
}