CF2154F2.Bombing (Hard Version)
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The difference between the versions is that in this version, n≤106. You can hack only if you solved all versions of this problem.
A permutation∗ b is considered a riffle shuffle of a permutation a if ∣a∣=∣b∣ and there exists k where 1≤k<∣a∣ such that a1,a2,…,ak and ak+1,ak+2,…,a∣a∣ are both subsequences† of b.
For example, [1,4,5,2,3,6] is a riffle shuffle of [1,2,3,4,5,6] because we can select k=3 and both [1,2,3] and [4,5,6] are subsequences.
You are given a permutation p of length n where some values are replaced with −1. Determine the number of ways to replace each −1 with an integer such that p becomes a riffle shuffle of [1,2,…,n] (the sorted permutation).
The number of ways could be gargantuan, so output it modulo 998244353.
∗A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
†A sequence c is a subsequence of a sequence d if c can be obtained from d by the deletion of several (possibly, zero or all) element from arbitrary positions.
这是该问题的困难版本。两个版本的区别在于,在本版本中,n≤106。仅当您已解决该问题的所有版本时,才可进行 Hack。
若排列∗ b 满足 ∣a∣=∣b∣,且存在整数 k(满足 1≤k<∣a∣),使得 a1,a2,…,ak 和 ak+1,ak+2,…,a∣a∣ 均为 b 的子序列†,则称 b 是排列 a 的一次洗牌(riffle shuffle)。
例如,[1,4,5,2,3,6] 是 [1,2,3,4,5,6] 的一次洗牌,因为可取 k=3,此时 [1,2,3] 和 [4,5,6] 均为前者的一个子序列。
给定一个长度为 n 的排列 p,其中部分元素被替换为 −1。请确定将每个 −1 替换为某个整数的方案数,使得 p 成为 [1,2,…,n](即升序排列)的一次洗牌。
答案可能极大,请对 998244353 取模后输出。
∗ 长度为 n 的排列是指由 1 到 n 中互不相同的 n 个整数按任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,而 [1,2,2] 不是排列(数字 2 出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
† 序列 c 是序列 d 的子序列,当且仅当 c 可通过从 d 中删除若干(可能为零个或全部)任意位置上的元素得到。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains an integer n (2≤n≤106) — the length of the permutation.
The second line of each test case contains n integers p1,p2,…,pn (1≤pi≤n or pi=−1) — the elements of p. All elements of p that are not −1 are distinct.
The sum of n across all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤106)—— 排列的长度。
每个测试用例的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n 或 pi=−1)—— 排列 p 的元素。所有不为 −1 的 pi 均互不相同。
所有测试用例中 n 的总和不超过 106。
输出格式
For each testcase, output the number of ways to fill p so that it is a riffle shuffle of [1,2,…,n] modulo 998244353.
对于每个测试用例,输出满足条件的排列 p 的个数(即 p 是 [1,2,…,n] 的一次洗牌排列),结果对 998244353 取模。
输入输出样例
输入#1
7 5 -1 -1 -1 -1 -1 4 1 2 3 4 5 -1 -1 -1 2 -1 6 -1 3 2 1 -1 -1 18 11 -1 2 -1 -1 -1 -1 6 -1 -1 14 8 9 15 -1 -1 -1 -1 6 -1 3 -1 4 -1 5 3 -1 2 1
输出#1
27 1 6 0 32 0 0
说明/提示
The possible permutations for the third test case are as follows:
- [1,3,4,2,5],
- [1,4,5,2,3],
- [3,1,4,2,5],
- [3,4,1,2,5],
- [4,1,5,2,3],
- [4,5,1,2,3].
第三个测试用例的所有可能排列如下:
- [1,3,4,2,5],
- [1,4,5,2,3],
- [3,1,4,2,5],
- [3,4,1,2,5],
- [4,1,5,2,3],
- [4,5,1,2,3]。
输入解题思路,AI测评打分。不知道怎么写?