#P3823. [NOI2017] 蚯蚓排队

    ID: 2780 远端评测题 3000ms 1250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串2017NOI 系列O2优化哈希,HASH

[NOI2017] 蚯蚓排队

Description

There are nn earthworms in a kindergarten. For easier management, the principal Shen Dao Shou often asks the earthworms to line up and perform.

All earthworms are numbered with consecutive positive integers from 11 to nn. The length of each earthworm is a positive integer, and by admission requirements, all lengths do not exceed 66. Shen Dao Shou wants these earthworms to form several queues. Initially, each earthworm forms a queue by itself, where that earthworm is both the head and the tail of its queue.

Shen Dao Shou will perform mm operations in sequence. Each operation is one of the following three types:

  1. Given ii and jj, merge the two queues containing earthworms ii and jj into a single queue. Specifically, place earthworm jj immediately after earthworm ii, and keep the relative order of all other earthworms unchanged.

  2. Given ii, split earthworm ii and the earthworm immediately after it into two queues. Specifically, after the split, earthworm ii becomes the tail of one queue, and the earthworm that was immediately after it becomes the head of the other queue. The relative order of all other earthworms remains unchanged.

  3. Given a positive integer kk and a digit string ss of length at least kk, for every length-kk contiguous substring tt of ss (there are sk+1|s|-k+1 such substrings, where s|s| is the length of ss), define a function f(t)f(t) and ask for the product of all these f(t)f(t) values modulo 998244353998244353. The definition of f(t)f(t) is as follows:

For the current queues of earthworms, define an earthworm’s backward kk-digit string as follows: starting from that earthworm and moving along the queue towards the back, find the nearest kk earthworms (including itself) and concatenate their lengths as characters to form a digit string. If fewer than kk earthworms can be found this way, then it does not have a backward kk-digit string. For example, if the queue of earthworms has earthworm 1010 at the head, followed by earthworm 2222, followed by earthworm 33 (which is at the tail), and their lengths are 44, 55, and 66 respectively, then the backward 33-digit string of earthworm 1010 is 456. Earthworm 2222 does not have a backward 33-digit string, but its backward 22-digit string is 56, and its backward 11-digit string is 5.

Then f(t)f(t) denotes the number of earthworms whose backward kk-digit string is exactly tt.

Input Format

The first line contains two positive integers n,mn, m, denoting the number of earthworms and the number of operations.

The second line contains nn positive integers not exceeding 66, in order, denoting the lengths of earthworms numbered 1,2,,n1, 2, \dots, n.

Each of the next mm lines describes an operation, in one of the following formats:

  • 1 ii jj (1i,jn1 \leq i, j \leq n): Merge the two queues containing earthworms ii and jj into one queue, and in the new queue, place earthworm jj immediately after earthworm ii. It is guaranteed that before this operation, earthworm ii is at the tail of some queue, earthworm jj is at the head of some queue, and the two earthworms are not in the same queue.

  • 2 ii (1in1 \leq i \leq n): Split earthworm ii and the earthworm immediately after it into two queues. It is guaranteed that before this operation, earthworm ii is not the tail of its queue.

  • 3 ss kk (kk is a positive integer, and ss is a digit string of length at least kk): Query the product of f(t)f(t) over every length-kk substring tt of ss, modulo 998244353998244353. The definition of f(t)f(t) is given in the problem statement.

Adjacent elements on the same line are separated by exactly one space.

The input file can be large. Please avoid overly slow input methods.

Output Format

For each operation of the form 3 s k, output a single line containing a single integer, which is the answer to that query.

5 9
3 1 3 5 3
3 333135 2
3 333135 1
1 1 3
1 2 5
1 3 2
1 5 4
3 333135 2
3 333135 1
3 333135 3
0
81
1
81
0
2 10
6 6
3 666666 1
1 1 2
3 666666 2
3 666666 4
3 666666666666666666666666666666 1
2 1
1 2 1
3 666666 2
3 666666 4
3 666666666666666666666666666666 1
64
1
0
75497471
1
0
75497471

Hint

Constraints:

It is guaranteed that n2×105n \leq 2 \times 10^{5}, m5×105m \leq 5 \times 10^{5}, k50k \leq 50.

Let s\sum |s| be the total length of all query strings ss in an input file. Then s107\sum |s| \leq 10^{7}.

Let cc be the number of operations of the form 2 i in an input file. Then c103c \leq 10^{3}.

Details for each test point are shown in the table below:

| Test Point | nn | mm | kk | s\sum |s| | cc | All are 1\texttt{1} | |:-:|:-:|:-:|:-:|:-:|:-:|:-:| | 1 | =1=1 | 103\leq 10^{3} | =1=1 | 103\leq 10^{3} | =0=0 | No | | 2 | 20\leq 20 | 40\leq 40 | 10\leq 10 | 103\leq 10^{3} | =0=0 | No | | 3 | 150\leq 150 | 2,000\leq 2,000 | 50\leq 50 | 103\leq 10^{3} | 103\leq 10^{3} | No | | 4 | 500\leq 500 | 600\leq 600 | 50\leq 50 | 103\leq 10^{3} | =0=0 | No | | 5 | 103\leq 10^{3} | 2,000\leq 2,000 | 50\leq 50 | 103\leq 10^{3} | 103\leq 10^{3} | No | | 6 | 5×104\leq 5 \times 10^{4} | 6×104\leq 6 \times 10^{4} | 5\leq 5 | 5×104\leq 5 \times 10^{4} | 103\leq 10^{3} | No | | 7 | 5×104\leq 5 \times 10^{4} | 6×104\leq 6 \times 10^{4} | 50\leq 50 | 5×104\leq 5 \times 10^{4} | =0=0 | Yes | | 8 | 5×104\leq 5 \times 10^{4} | 6×104\leq 6 \times 10^{4} | 50\leq 50 | 5×104\leq 5 \times 10^{4} | =0=0 | No | | 9 | 5×104\leq 5 \times 10^{4} | 6×104\leq 6 \times 10^{4} | 50\leq 50 | 5×104\leq 5 \times 10^{4} | 103\leq 10^{3} | No | | 10 | 5×104\leq 5 \times 10^{4} | 8×104\leq 8 \times 10^{4} | 50\leq 50 | 2.5×106\leq 2.5 \times 10^{6} | =0=0 | No | | 11 | 5×104\leq 5 \times 10^{4} | 8×104\leq 8 \times 10^{4} | 50\leq 50 | 2.5×106\leq 2.5 \times 10^{6} | 103\leq 10^{3} | No | | 12 | 105\leq 10^{5} | 1.1×105\leq 1.1 \times 10^{5} | 6\leq 6 | 105\leq 10^{5} | 103\leq 10^{3} | No | | 13 | 105\leq 10^{5} | 1.1×105\leq 1.1 \times 10^{5} | 50\leq 50 | 105\leq 10^{5} | =0=0 | Yes | | 14 | 105\leq 10^{5} | 1.1×105\leq 1.1 \times 10^{5} | 50\leq 50 | 105\leq 10^{5} | =0=0 | No | | 15 | 105\leq 10^{5} | 1.1×105\leq 1.1 \times 10^{5} | 50\leq 50 | 105\leq 10^{5} | 103\leq 10^{3} | No | | 16 | 105\leq 10^{5} | 1.5×105\leq 1.5 \times 10^{5} | 50\leq 50 | 5×106\leq 5 \times 10^{6} | =0=0 | No | | 17 | 105\leq 10^{5} | 1.5×105\leq 1.5 \times 10^{5} | 50\leq 50 | 5×106\leq 5 \times 10^{6} | 103\leq 10^{3} | No | | 18 | 2×105\leq 2 \times 10^{5} | 5×105\leq 5 \times 10^{5} | =1=1 | 107\leq 10^{7} | =0=0 | No | | 19 | 2×105\leq 2 \times 10^{5} | 5×105\leq 5 \times 10^{5} | =1=1 | 107\leq 10^{7} | 103\leq 10^{3} | No | | 20 | 2×105\leq 2 \times 10^{5} | 2.5×105\leq 2.5 \times 10^{5} | 7\leq 7 | 2×105\leq 2 \times 10^{5} | 103\leq 10^{3} | No | | 21 | 2×105\leq 2 \times 10^{5} | 2.5×105\leq 2.5 \times 10^{5} | 50\leq 50 | 2×105\leq 2 \times 10^{5} | =0=0 | Yes | | 22 | 2×105\leq 2 \times 10^{5} | 2.5×105\leq 2.5 \times 10^{5} | 50\leq 50 | 2×105\leq 2 \times 10^{5} | =0=0 | No | | 23 | 2×105\leq 2 \times 10^{5} | 2.5×105\leq 2.5 \times 10^{5} | 50\leq 50 | 2×105\leq 2 \times 10^{5} | 103\leq 10^{3} | No | | 24 | 2×105\leq 2 \times 10^{5} | 3×105\leq 3 \times 10^{5} | 50\leq 50 | 107\leq 10^{7} | =0=0 | No | | 25 | 2×105\leq 2 \times 10^{5} | 3×105\leq 3 \times 10^{5} | 50\leq 50 | 107\leq 10^{7} | 103\leq 10^{3} | No |

If the “All are 1” column of a test point is “Yes”, it means all earthworms have length 11, and every digit in every query string ss is also 1.

Translated by ChatGPT 5