#P5561. [Celeste-B] Mirror Magic

[Celeste-B] Mirror Magic

Description

The Mirror Temple is a magical temple, where you can catch a glimpse of the truth in your heart.

Theo is trapped inside a mirror. When Madeline tries to save Theo, the “creatures” in the temple give her a difficult puzzle. The puzzle is as follows:

The “creatures” prepare nn strawberry strings, and the sum of their lengths <=106<= 10^6. Now they can perform the following operations:

1 x l r1\ x\ l\ r means copying the substring [l,r][l,r] of the strawberry string with index xx, and inserting the copied string into the current set of strawberry strings. It is guaranteed that the length of string xx is >=r>=r.

2 k2\ k means deleting the strawberry string that was inserted by the kk-th operation. It is guaranteed that the kk-th operation is an insertion, and the string inserted by the kk-th operation is currently in the set.

The “creatures” believe that a harmonious flavor is delicious. After performing any operation, they want to know the deliciousness of all strawberry strings in the current set (i.e., LCPLCP - the longest common prefix). (If the set is empty, the deliciousness is 00. If there is only one strawberry string in the set, the deliciousness is the length of that string.)

For convenience, we represent each type of strawberry as a lowercase English letter.

Many years later, Madeline comes to the Mirror Temple again, wanting to recall her climbing journey from long ago, but she no longer remembers how she solved that puzzle back then. Can you help her find the answer to the puzzle?

Input Format

The first line contains a positive integer nn, indicating the number of strawberry strings.

The next nn lines each contain a string consisting of only lowercase letters, representing the original nn strawberry strings.

The next line contains a positive integer qq, indicating the number of operations.

The next qq lines each contain one operation.

Output Format

Output qq lines. Each line should be the deliciousness (LCPLCP - the longest common prefix) of the strawberry strings in the set after the corresponding operation.

3
abccccd
abccddc
acbbcad
5
1 1 1 7
1 2 1 7
1 3 1 7
2 3
2 2

7
4
1
4
7

Hint

Sample explanation:

After the first operation, the set is {"abccccd"}\{"abccccd"\}, LCP=7LCP=7.

After the second operation, the set is {"abccccd","abccddc"}\{"abccccd","abccddc"\}, LCP=4LCP=4.

After the third operation, the set is {"abccccd","abccddc","acbbcad"}\{"abccccd","abccddc","acbbcad"\}, LCP=1LCP=1.

The fourth operation deletes the string inserted by the third operation, so LCP=4LCP=4.

The fifth operation deletes the string inserted by the second operation, so LCP=7LCP=7.

Let lenlen be the sum of lengths of the nn strings. We always have len106len \leq 10^6.

Subtask 11 (1010 Pts): n5,q=10n \leq 5, q = 10.

Subtask 22 (3030 Pts): n106,q=106n \leq 10^6, q = 10^6, and it is guaranteed that each insertion is a prefix of a string.

Subtask 33 (1010 Pts): n106,q=105n \leq 10^6, q = 10^5, there are no type 22 operations, and it is guaranteed that the queries are randomly generated.

Subtask 44 (5050 Pts): n106,q=106n \leq 10^6, q = 10^6.

Hint: You can determine the subtask number based on qq.

Translated by ChatGPT 5