AtCoder Regular Contest++ 216
B
区间 MEX 之和
Range Mex Sum
AtCoder
⌀ 1935
原题
← AC →

区间 MEX 之和AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given a positive integer $N$ and a sequence of integers $A = (A_1, A_2, \ldots, A_N)$. Each $A_i$ satisfies $-1 \leq A_i \leq N-1$, and for $1 \leq i < j \leq N$, if $A_i \neq -1$ and $A_j \neq -1$, then $A_i \neq A_j$.

给定一个正整数 $N$ 和一个整数序列 $A = (A_1, A_2, \ldots, A_N)$。每个 $A_i$ 都满足 $-1 \leq A_i \leq N-1$,并且对于 $1 \leq i < j \leq N$,如果 $A_i \neq -1$ 且 $A_j \neq -1$,则有 $A_i \neq A_j$。AI (CI翻译)

Answer the following query $Q$ times.

请回答 $Q$ 次以下询问。AI (CI翻译)

  • You are given integers $l, r\ (1 \leq l \leq r \leq N)$. Find the sum, modulo $998244353$, of $\mathrm{mex}(\lbrace P_l, P_{l+1}, \ldots, P_r \rbrace)$ over all permutations $P = (P_1, P_2, \ldots, P_N)$ of $(0, 1, \ldots, N-1)$ satisfying $A_i \neq -1 \implies P_i = A_i$.
  • 给定整数 $l, r\ (1 \leq l \leq r \leq N)$。对于所有满足 $A_i \neq -1 \implies P_i = A_i$ 的 $(0, 1, \ldots, N-1)$ 的排列 $P = (P_1, P_2, \ldots, P_N)$,求集合 $\lbrace P_l, P_{l+1}, \ldots, P_r \rbrace$ 的 $\mathrm{mex}$ 之和,结果对 $998244353$ 取模。
AI (CI翻译)

What is $\mathrm{mex}(S)$? For a set $S$ of non-negative integers, $\mathrm{mex}(S)$ is defined as the smallest non-negative integer not contained in $S$.

$\mathrm{mex}(S)$ 是什么?对于一个非负整数集合 $S$,$\mathrm{mex}(S)$ 定义为不包含在 $S$ 中的最小非负整数。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq N \leq 5000$
  • $1 \leq Q \leq 5 \times 10^5$
  • $-1 \leq A_i \leq N-1$
  • For $1 \leq i < j \leq N$, if $A_i \neq -1$ and $A_j \neq -1$, then $A_i \neq A_j$.
  • For each query, $1 \leq l \leq r \leq N$.
  • All input values are integers.
  • $1 \leq N \leq 5000$
  • $1 \leq Q \leq 5 \times 10^5$
  • $-1 \leq A_i \leq N-1$
  • 对于 $1 \leq i < j \leq N$,如果 $A_i \neq -1$ 且 $A_j \neq -1$,则有 $A_i \neq A_j$。
  • 对于每次询问,$1 \leq l \leq r \leq N$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入中按以下格式给出:AI (CI翻译)

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
AI (CI翻译)

Each query is given in the following format:

每次询问的格式如下:AI (CI翻译)

l r

l r
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $Q$ lines. The $i$-th line should contain the answer to $\mathrm{query}_i$.

输出 $Q$ 行。第 $i$ 行应包含对 $\mathrm{query}_i$ 的回答。AI (CI翻译)

样例

Input #1
3 4
0 -1 -1
1 1
2 2
1 2
1 3
Output #1
2
0
3
6
The permutations $P$ satisfying $A_i \neq -1 \implies P_i = A_i$ are $(0, 1, 2)$ and $(0, 2, 1)$. Thus, the answer to each query is computed as follows. - The answer to the first query is $\mathrm{mex}(\lbrace 0 \rbrace) + \mathrm{mex}(\lbrace 0 \rbrace) = 1 + 1 = 2$. - The answer to the second query is $\mathrm{mex}(\lbrace 1 \rbrace) + \mathrm{mex}(\lbrace 2 \rbrace) = 0 + 0 = 0$. - The answer to the third query is $\mathrm{mex}(\lbrace 0, 1 \rbrace) + \mathrm{mex}(\lbrace 0, 2 \rbrace) = 2 + 1 = 3$. - The answer to the fourth query is $\mathrm{mex}(\lbrace 0, 1, 2 \rbrace) + \mathrm{mex}(\lbrace 0, 2, 1 \rbrace) = 3 + 3 = 6$.
Input #2
5 3
-1 2 -1 -1 1
1 4
3 5
1 5
Output #2
6
8
30
Input #3
15 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
1 15
Output #3
612227903
题目信息
题号
arc216_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。