Infinite Arrays

시간 제한1.5초메모리 제한2048 MB

요약
원소의 삭제와 삽입으로 변하는 순열 P를 관리하면서, 질의로 주어지는 배열 A에 대해 P와 A를 무한히 반복한 배열의 최장 공통 부분배열 길이를 10^18을 넘으면 *로 출력한다.
난이도

어려움10점 중 9점

유형
문자열 매칭, 연결 리스트, 해시맵, 수학
정답자
아직 제출이 없습니다

문제

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^∞ = \[ \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,1P = \[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 P∞P^∞ and A∞A^∞ 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 P∞P^∞ and A∞A^∞ 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 (1≤N≤5⋅1051 ≤ N ≤ 5 \cdot 10^5 ) indicating the initial length of PP.

The second line contains NN different integers P_1,P_2,…,P_NP\_1, P\_2, \dots , P\_N (1≤P_i≤1061 ≤ P\_i ≤ 10^6 for i=1,2,…,Ni = 1, 2, \dots , N).

The third line contains an integer EE (1≤E≤5⋅1051 ≤ E ≤ 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 (1≤X≤1061 ≤ X ≤ 10^6, and X∈PX \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 (1≤Y,Z≤1061 ≤ Y, Z ≤ 10^6, Y∉PY \not\in P, and Z∈PZ \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 A_1,A_2,…,A_KA\_1, A\_2, \dots , A\_K (1≤A_i≤1061 ≤ A\_i ≤ 10^6 for i=1,2,…,Ki = 1, 2, \dots , K), indicating that the length of the longest common subarray between P∞P^∞ and A∞A^∞ 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 P∞P^∞ and A∞A^∞, or the character “*” (asterisk) if the length of the longest common subarray is larger than 101810^{18}.

예제2

  1. 예제 1

    입력
    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
    
  2. 예제 2

    입력
    1
    217
    1
    ? 1 314
    
    예상 출력
    0