CF1442B.Identify the Operations

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

We start with a permutation a1,a2,…,ana_1, a_2, \ldots, a_n and with an empty array bb. We apply the following operation kk times.

On the ii-th iteration, we select an index tit_i (1≤ti≤n−i+11 \le t_i \le n-i+1), remove atia_{t_i} from the array, and append one of the numbers ati−1a_{t_i-1} or ati+1a_{t_i+1} (if ti−1t_i-1 or ti+1t_i+1 are within the array bounds) to the right end of the array bb. Then we move elements ati+1,…,ana_{t_i+1}, \ldots, a_n to the left in order to fill in the empty space.

You are given the initial permutation a1,a2,…,ana_1, a_2, \ldots, a_n and the resulting array b1,b2,…,bkb_1, b_2, \ldots, b_k. All elements of an array bb are distinct. Calculate the number of possible sequences of indices t1,t2,…,tkt_1, t_2, \ldots, t_k modulo 998 244 353998\,244\,353.

我们从一个排列 a1,a2,…,ana_1, a_2, \ldots, a_n 和一个空数组 bb 开始。我们执行以下操作共 kk 次。

在第 ii 轮操作中,我们选择一个下标 tit_i(满足 1≤ti≤n−i+11 \le t_i \le n-i+1),从数组 aa 中移除元素 atia_{t_i},并将 ati−1a_{t_i-1} 或 ati+1a_{t_i+1}(若下标 ti−1t_i-1 或 ti+1t_i+1 在当前数组 aa 的有效范围内)中的一个数添加到数组 bb 的右端。随后,我们将 ati+1,…,ana_{t_i+1}, \ldots, a_n 各元素依次左移一位,以填补被移除元素留下的空位。

已知初始排列 a1,a2,…,ana_1, a_2, \ldots, a_n 以及最终得到的数组 b1,b2,…,bkb_1, b_2, \ldots, b_k。数组 bb 中的所有元素互不相同。请计算满足条件的下标序列 t1,t2,…,tkt_1, t_2, \ldots, t_k 的总数,结果对 998 244 353998\,244\,353 取模。

输入格式

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤100 0001 \le t \le 100\,000), denoting the number of test cases, followed by a description of the test cases.

The first line of each test case contains two integers n,kn, k (1≤k<n≤200 0001 \le k \lt n \le 200\,000): sizes of arrays aa and bb.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n): elements of aa. All elements of aa are distinct.

The third line of each test case contains kk integers b1,b2,…,bkb_1, b_2, \ldots, b_k (1≤bi≤n1 \le b_i \le n): elements of bb. All elements of bb are distinct.

The sum of all nn among all test cases is guaranteed to not exceed 200 000200\,000.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤100 0001 \le t \le 100\,000),表示测试用例的数量,随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 n,kn, k(1≤k<n≤200 0001 \le k \lt n \le 200\,000):分别表示数组 aa 和 bb 的大小。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n):即数组 aa 的元素。aa 中所有元素互不相同。

每个测试用例的第三行包含 kk 个整数 b1,b2,…,bkb_1, b_2, \ldots, b_k(1≤bi≤n1 \le b_i \le n):即数组 bb 的元素。bb 中所有元素互不相同。

保证所有测试用例中 nn 的总和不超过 200 000200\,000。

输出格式

For each test case print one integer: the number of possible sequences modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数:可能的序列数量对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

    3
    5 3
    1 2 3 4 5
    3 2 5
    4 3
    4 3 2 1
    4 3 1
    7 4
    1 4 7 3 6 2 5
    3 2 4 5

    输出#1

    2
    0
    4

说明/提示

\require{cancel}

Let's denote as a1a2…aiai+1‾…an→a1a2…ai−1ai+1…an−1a_1 a_2 \ldots \cancel{a_i} \underline{a_{i+1}} \ldots a_n \rightarrow a_1 a_2 \ldots a_{i-1} a_{i+1} \ldots a_{n-1} an operation over an element with index ii: removal of element aia_i from array aa and appending element ai+1a_{i+1} to array bb.

In the first example test, the following two options can be used to produce the given array bb:

  • 123‾45→12‾35→125‾→121 2 \underline{3} \cancel{4} 5 \rightarrow 1 \underline{2} \cancel{3} 5 \rightarrow 1 \cancel{2} \underline{5} \rightarrow 1 2; (t1,t2,t3)=(4,3,2)(t_1, t_2, t_3) = (4, 3, 2);
  • 123‾45→12‾35→235‾→151 2 \underline{3} \cancel{4} 5 \rightarrow \cancel{1} \underline{2} 3 5 \rightarrow 2 \cancel{3} \underline{5} \rightarrow 1 5; (t1,t2,t3)=(4,1,2)(t_1, t_2, t_3) = (4, 1, 2).

In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 44, namely number 33, which means that it couldn't be added to array bb on the second step.

In the third example test, there are four options to achieve the given array bb:

  • 1473‾625→14362‾5→14‾325→4325‾→4351 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 3 2 5 \rightarrow 4 3 \cancel{2} \underline{5} \rightarrow 4 3 5;
  • 1473‾625→14362‾5→14‾325→1425‾→1451 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{3} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5;
  • 1473‾625→14732‾5→14‾725→4725‾→4751 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 7 2 5 \rightarrow 4 7 \cancel{2} \underline{5} \rightarrow 4 7 5;
  • 1473‾625→14732‾5→14‾725→1425‾→1451 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{7} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5;

\require{cancel}

我们用 a1a2…aiai+1‾…an→a1a2…ai−1ai+1…an−1a_1 a_2 \ldots \cancel{a_i} \underline{a_{i+1}} \ldots a_n \rightarrow a_1 a_2 \ldots a_{i-1} a_{i+1} \ldots a_{n-1} 表示对下标为 ii 的元素执行的一种操作:从数组 aa 中删除元素 aia_i,并将元素 ai+1a_{i+1} 追加到数组 bb 的末尾。

在第一个样例测试中,存在以下两种方式可得到给定的数组 bb:

  • 123‾45→12‾35→125‾→121 2 \underline{3} \cancel{4} 5 \rightarrow 1 \underline{2} \cancel{3} 5 \rightarrow 1 \cancel{2} \underline{5} \rightarrow 1 2;(t1,t2,t3)=(4,3,2)(t_1, t_2, t_3) = (4, 3, 2);
  • 123‾45→12‾35→235‾→151 2 \underline{3} \cancel{4} 5 \rightarrow \cancel{1} \underline{2} 3 5 \rightarrow 2 \cancel{3} \underline{5} \rightarrow 1 5;(t1,t2,t3)=(4,1,2)(t_1, t_2, t_3) = (4, 1, 2)。

在第二个样例测试中,无论采用何种操作,都不可能得到给定的数组。这是因为:在第一次操作中,我们删除了紧邻 44 的元素(即数字 33),这意味着 33 在第二步中无法被添加到数组 bb 中。

在第三个样例测试中,共有四种方式可得到给定的数组 bb:

  • 1473‾625→14362‾5→14‾325→4325‾→4351 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 3 2 5 \rightarrow 4 3 \cancel{2} \underline{5} \rightarrow 4 3 5;
  • 1473‾625→14362‾5→14‾325→1425‾→1451 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{3} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5;
  • 1473‾625→14732‾5→14‾725→4725‾→4751 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 7 2 5 \rightarrow 4 7 \cancel{2} \underline{5} \rightarrow 4 7 5;
  • 1473‾625→14732‾5→14‾725→1425‾→1451 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{7} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5;

输入解题思路,AI测评打分。不知道怎么写?

首页