公式

B - Range Mex Sum 解説 by evima


We have \(\mathrm{mex}(\lbrace P_l, \ldots, P_r \rbrace) = \min(N, P_1, \ldots, P_{l-1}, P_{r+1}, \ldots, P_N)\). Thus, the answer depends on the minimum value \(m\) of elements other than \(-1\) that are not contained in \(A[l:r]\) (if none exists, set \(m = N\)) and the number \(c\) of \(-1\)s not contained in \(A[l:r]\).

Let \(S\) be the set of integers between \(0\) and \(N-1\) that are not contained in \(A\), and let \(d\) be the number of \(-1\)s in \(A[l:r]\). When \(c \neq 0\), the answer is \(c! d! \displaystyle \sum_{v \in S} \min(v, m) \times \left(\binom{|\lbrace i \geq v \mid i \in S \rbrace|}{c} - \binom{|\lbrace i > v \mid i \in S \rbrace|}{c}\right)\).

By precomputing the answers for all \(c\) and \(m\) together in \(O(N^2)\), we can answer each query in \(O(1)\).

投稿日時:
最終更新: