#P15103. [ICPC 2025 LAC] Infinite Arrays

[ICPC 2025 LAC] Infinite Arrays

题目描述

A subarray of an array BB is a contiguous sequence of elements taken from BB. For example, if B=[3,1,4,1,5]B = [3, 1, 4, 1, 5], then [3,1,4][3, 1, 4], [4,1][4, 1], the empty array [][] and the whole array BB are subarrays of BB (among others), while [3,4,5][3, 4, 5] and [1,3][1, 3] are not subarrays of BB.

We also define CmC^m as the array obtained by the concatenation of mm copies of the array CC. For example, if C=[3,2]C = [3, 2] then C1=[3,2]C^1 = [3, 2], C2=[3,2,3,2]C^2 = [3, 2, 3, 2] and C=[,3,2,3,2,3,2,]C^\infty = [\dots, 3, 2, 3, 2, 3, 2, \dots].

In this problem you are given an array PP containing no repeated numbers, and you have to process a sequence of events that occur in order. The events can be of three types:

  • Delete: a number present in PP must be deleted. For example, if P=[2,3,4]P = [2, 3, 4] and the number 33 has to be deleted, once the event is processed PP will become [2,4][2, 4].
  • Insert: a number not present in PP must be inserted into PP before some other number present in PP. For example, if P=[4,3,2,1]P = [4, 3, 2, 1] and the number 99 must be inserted before the number 11, once the event is processed PP will become [4,3,2,9,1][4, 3, 2, 9, 1].
  • Query: the length of the longest common subarray between PP^\infty and AA^\infty must be computed, where AA is an array given in the query. For example, if P=[1,2,3,4]P = [1, 2, 3, 4] and A=[3,2]A = [3, 2], then the longest common subarray between PP^\infty and AA^\infty is [2,3][2, 3], so the answer to the query is 22.

Are you ready for this challenge?

输入格式

The first line contains an integer NN (1N51051 \le N \le 5 \cdot 10^5) indicating the initial length of PP.

The second line contains NN different integers P1,P2,,PNP_1, P_2, \dots, P_N (1Pi1061 \le P_i \le 10^6 for i=1,2,,Ni = 1, 2, \dots, N).

The third line contains an integer EE (1E51051 \le E \le 5 \cdot 10^5) representing the number of events that need to be processed.

Each of the next EE lines describes an event, in the order they must be processed. The content of the line depends on the event, as follows:

  • Delete: the line contains the character “-” (minus sign) and an integer XX (1X1061 \le X \le 10^6, and XPX \in P), denoting that XX must be deleted from PP. It is guaranteed that after the removal PP does not become empty.
  • Insert: the line contains the character “+” (plus sign) and two integers YY and ZZ (1Y,Z1061 \le Y, Z \le 10^6, YPY \notin P, and ZPZ \in P), denoting that YY must be inserted into PP immediately to the left of ZZ.
  • Query: the line contains the character “?” (question mark), a positive integer KK, and KK integers A1,A2,,AKA_1, A_2, \dots, A_K (1Ai1061 \le A_i \le 10^6 for i=1,2,,Ki = 1, 2, \dots, K), indicating that the length of the longest common subarray between PP^\infty and AA^\infty must be computed. It is guaranteed that the input contains at least one query, and the sum of KK across all the queries is at most 10610^6.

输出格式

Output a line for each query, with an integer indicating the length of the longest common subarray between PP^\infty and AA^\infty, or the character “*” (asterisk) if the length of the longest common subarray is larger than 101810^{18}.

4
1 2 3 4
10
? 2 3 2
? 3 99 99 99
- 1
- 4
? 2 3 3
? 3 4 1 4
+ 64 3
+ 1 2
? 4 1 2 64 3
? 4 1 3 64 2
2
0
1
0
*
1
1
217
1
? 1 314
0