CF1718D.Permutation for Burenka

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

We call an array aa pure if all elements in it are pairwise distinct. For example, an array [1,7,9][1, 7, 9] is pure, [1,3,3,7][1, 3, 3, 7] isn't, because 33 occurs twice in it.

A pure array bb is similar to a pure array cc if their lengths nn are the same and for all pairs of indices ll, rr, such that 1≤l≤r≤n1 \le l \le r \le n, it's true that $$\operatorname{argmax}([b_l, b_{l + 1}, \ldots, b_r]) = \operatorname{argmax}([c_l, c_{l + 1}, \ldots, c_r]),$$ where argmax⁡(x)\operatorname{argmax}(x) is defined as the index of the largest element in xx (which is unique for pure arrays). For example, argmax⁡([3,4,2])=2\operatorname{argmax}([3, 4, 2]) = 2, argmax⁡([1337,179,57])=1\operatorname{argmax}([1337, 179, 57]) = 1.

Recently, Tonya found out that Burenka really likes a permutation pp of length nn. Tonya decided to please her and give her an array aa similar to pp. He already fixed some elements of aa, but exactly kk elements are missing (in these positions temporarily ai=0a_i = 0). It is guaranteed that k≥2k \ge 2. Also, he has a set SS of k−1k - 1 numbers.

Tonya realized that he was missing one number to fill the empty places of aa, so he decided to buy it. He has qq options to buy. Tonya thinks that the number dd suits him, if it is possible to replace all zeros in aa with numbers from SS and the number dd, so that aa becomes a pure array similar to pp. For each option of dd, output whether this number is suitable for him or not.

我们称一个数组 aa 是纯的,如果其中所有元素两两互不相同。例如,数组 [1,7,9][1, 7, 9] 是纯的,而 [1,3,3,7][1, 3, 3, 7] 不是纯的,因为 33 在其中出现了两次。

两个纯数组 bb 和 cc 称为相似的,当且仅当它们长度 nn 相同,且对所有满足 1≤l≤r≤n1 \le l \le r \le n 的下标对 (l,r)(l, r),均有

argmax⁡([bl,bl+1,…,br])=argmax⁡([cl,cl+1,…,cr]),\operatorname{argmax}([b_l, b_{l + 1}, \ldots, b_r]) = \operatorname{argmax}([c_l, c_{l + 1}, \ldots, c_r]),

其中 argmax⁡(x)\operatorname{argmax}(x) 定义为 xx 中最大元素的下标(由于数组是纯的,该下标唯一)。例如,argmax⁡([3,4,2])=2\operatorname{argmax}([3, 4, 2]) = 2,argmax⁡([1337,179,57])=1\operatorname{argmax}([1337, 179, 57]) = 1。

最近,Tonya 得知 Burenka 非常喜欢一个长度为 nn 的排列 pp。Tonya 决定取悦她,送给她一个与 pp 相似的数组 aa。他已预先固定了 aa 中的部分元素,但恰好有 kk 个位置缺失(这些位置暂时记为 ai=0a_i = 0)。题目保证 k≥2k \ge 2。此外,他还拥有一个大小为 k−1k - 1 的数集 SS。

Tonya 意识到,他还缺少一个数来填满 aa 中所有空缺位置,于是决定购买它。他共有 qq 种购买选项。Tonya 认为一个数 dd 是合适的,当且仅当可以将 aa 中所有 00 替换为 SS 中的数以及该数 dd,使得最终得到的数组 aa 成为一个纯数组,且与 pp 相似。对每个候选数 dd,请输出它是否合适。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) is the number of test cases. The description of the test cases follows.

The first line of each test case contains a couple of integers nn and qq (1≤n,q≤3⋅1051 \le n, q \le 3 \cdot 10^5).

The second line of each input test case contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_i \le n) — the permutation Burenka likes.

The third line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤1060 \le a_i \le 10^6) — elements of Tonya's array, where 00 denotes a missing element. It is guaranteed that there are two indexes i,ji, j (1≤i,j≤n,i≠j)(1 \le i, j \le n, i \ne j) such that ai=0,aj=0a_i = 0, a_j = 0, which implies that k≥2k \geq 2.

The fourth line of each test case contains k−1k - 1 distinct integers s1,s2,…,sk−1s_1, s_2, \ldots, s_{k-1} (1≤si≤1061 \le s_i \le 10^6) — elements of Tonya's set SS.

Each of the next qq lines contains a single integer dd (1≤d≤1061 \le d \le 10^6) — the number that Tonya plans to buy.

It is guaranteed that for each given dd it's possible to fill in the gaps in aa with numbers from SS and the number dd to get a pure array.

It is guaranteed that the sum of nn and the sum of qq in all tests does not exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 qq(1≤n,q≤3⋅1051 \le n, q \le 3 \cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_i \le n)——即 Burenka 喜欢的排列。

每个测试用例的第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤1060 \le a_i \le 10^6)——即 Tonya 的数组,其中 00 表示缺失的元素。保证存在两个下标 i,ji, j(1≤i,j≤n, i≠j1 \le i, j \le n,\, i \ne j),使得 ai=0a_i = 0 且 aj=0a_j = 0,即 k≥2k \geq 2。

每个测试用例的第四行包含 k−1k - 1 个互不相同的整数 s1,s2,…,sk−1s_1, s_2, \ldots, s_{k-1}(1≤si≤1061 \le s_i \le 10^6)——即 Tonya 的集合 SS 中的元素。

接下来的 qq 行中,每行包含一个整数 dd(1≤d≤1061 \le d \le 10^6)——即 Tonya 计划购买的数字。

保证:对每个给定的 dd,总能用集合 SS 中的数以及数字 dd 填补数组 aa 中的空缺,从而得到一个纯数组(pure array)。

保证:所有测试用例中 nn 的总和与 qq 的总和均不超过 3⋅1053 \cdot 10^5。

输出格式

Output qq lines. For each value dd, print "YES" if there is a way to fill the array aa to make it similar to pp, and "NO" otherwise.

输出 qq 行。对于每个值 dd,如果存在一种方式填充数组 aa 使其与 pp 相似,则输出 "YES";否则输出 "NO"。

输入输出样例

  • 输入#1

    4
    4 3
    1 4 3 2
    5 0 7 0
    6
    9
    1
    4
    5 3
    1 2 5 4 3
    0 5 10 0 0
    3 9
    1
    8
    11
    5 2
    1 4 3 2 5
    0 0 0 0 0
    7 9 1 5
    6
    100
    4 2
    4 1 3 2
    0 5 3 0
    2
    4
    6

    输出#1

    YES
    NO
    NO
    YES
    YES
    NO
    YES
    YES
    NO
    NO

说明/提示

In the first test case for d=9d = 9, you can get a=[5,9,7,6]a = [5, 9, 7, 6], it can be proved that aa is similar to pp, for d=1d=1 and d=4d=4 it can be proved that there is no answer.

In the second test case for d=1d = 1, you can get a=[1,5,10,9,3]a = [1, 5, 10, 9, 3], for d=8d = 8, you can get a=[3,5,10,9,8]a = [3, 5, 10, 9, 8], it can be proved that for d=11d = 11 there is no answer.

在第一个测试用例中,当 d=9d = 9 时,可得到 a=[5,9,7,6]a = [5, 9, 7, 6],可以证明该数组 aa 与 pp 相似;而当 d=1d = 1 和 d=4d = 4 时,可以证明不存在满足条件的解。

在第二个测试用例中,当 d=1d = 1 时,可得到 a=[1,5,10,9,3]a = [1, 5, 10, 9, 3];当 d=8d = 8 时,可得到 a=[3,5,10,9,8]a = [3, 5, 10, 9, 8];可以证明当 d=11d = 11 时不存在满足条件的解。

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

首页