#P5826. 【模板】子序列自动机

    ID: 6578 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>字符串线段树O2优化可持久化有限状态自动机

【模板】子序列自动机

Background

In this problem, if xx is a subsequence of yy, then it is equivalent to the existence of a strictly increasing sequence zz such that z=x|z| = |x|, zxyz_{|x|} \leq |y|, and i[1, x], yzi=xi\forall i \in [1, ~|x|],~y_{z_i} = x_i. Here x, y, z|x|,~|y|,~|z| denote the lengths of sequences x, y, zx,~y,~z, and xi, yi, zix_i,~y_i,~z_i denote the ii-th element of sequences x, y, zx,~y,~z.

This problem was rejected in the backup problem set of yLOI2020.

Problem Description

Given a positive integer sequence aa of length nn, there are qq queries. In the ii-th query, a sequence bib_i of length LiL_i is given. You need to determine whether bib_i is a subsequence of aa. All elements in sequence aa and all bib_i are no greater than a given positive integer mm.

Input Format

Each test case contains exactly one dataset.

The first line contains four integers separated by spaces: type, n, q, mtype,~n,~q,~m. Here typetype indicates the subtask number of the test point, and the meanings of the other variables are the same as in the Description.

The second line contains nn integers separated by spaces. The ii-th number is the ii-th element aia_i of sequence aa.

Lines 33 to (q+2)(q + 2) each represent a query. The input format of line (i+2)(i + 2) is:

  • At the beginning of line (i+2)(i + 2) there is an integer lil_i, which is the length of the sequence in the ii-th query. After one space, there are lil_i integers separated by spaces. The (j+1)(j + 1)-th integer on this line is the jj-th element bi,jb_{i, j} of sequence bib_i.

Output Format

For each query, output one string per line. If the given sequence is a subsequence of aa, output Yes; otherwise output No.

0 5 5 5
1 3 2 2 4
3 1 5 2
2 3 2
3 1 2 3
3 1 2 4
5 1 3 2 2 4

No
Yes
No
Yes
Yes

Hint

Explanation for Sample 1

  • For the first query, there is no 55 in the original sequence, so the given sequence is obviously not a subsequence of the original sequence.
  • For the second query, there are two valid sequences zz, which are {2, 3}\{2,~3\} and {2, 4}\{2,~4\}. That is, a2=b2,1, a3=b2,2a_{2} = b_{2, 1},~a_3 = b_{2, 2} or a2=b2,1, a4=b2,2a_2 = b_{2, 1},~a_{4} = b_{2, 2}. Here bi,jb_{i, j} denotes the jj-th element of sequence bib_i. The meaning of sequence zz is given in the Background, and the same below.
  • For the third query, there is no valid sequence zz.
  • For the fourth query, there are two valid sequences zz, which are {1, 3, 5}\{1,~3,~5\} and {1, 4, 5}\{1,~4,~5\}.
  • For the fifth query, there is one valid sequence zz, which is {1, 2, 3, 4, 5}\{1,~2,~3,~4,~5\}.

Constraints and Conventions

This problem uses bundled multi-point testing, with a total of 3 subtasks.

  • Subtask 1 (20 points): type=1type = 1, n,q,m100n, q, m \leq 100, i=1qli103\sum_{i = 1}^{q} l_i \leq 10^3.
  • Subtask 2 (35 points): type=2type = 2, n,q105n,q \leq 10^5, m26m \leq 26, i=1qli106\sum_{i = 1}^{q} l_i \leq 10^6.
  • Subtask 3 (45 points): type=3type = 3, n,q,m105n,q,m \leq 10^5, i=1qLi106\sum_{i = 1}^q L_i \leq 10^6.

For all test points, it is guaranteed that 1n,m,q1051 \leq n, m, q \leq 10^5, 1ai,bi,jm1 \leq a_i, b_{i, j} \leq m, 1li1061 \leq l_i \leq 10^6, and i=1qli106\sum_{i = 1}^{q} l_i \leq 10^6.

Notes

  • Please pay attention to the impact of constant factors on program efficiency.
  • The input size of this problem is large, so please pay attention to input reading efficiency.
  • Please note that in the first line of input, the order is to input the number of queries qq first, and then the maximum value mm of sequence elements.

Translated by ChatGPT 5