Sequence and Queries 34

Time limit2sMemory limit512 MB

Summary
Maintain two integer sequences under updates and range queries: for a suffix of a compute the longest match against b and how many suffixes achieve it, compare suffixes of b, and test whether a concatenation of two b-substrings is itself a substring of b.
Level

Hard9 of 10

Topics
String matching, Segment tree, Sorting, String
Solved
No attempts yet

Problem

You are given two sequences aa and bb of positive integers with lengths nn and mm. Write a program that processes the queries below. All indices are 1-based.

  • 1 y z: set a[y]=za[y] = z, then print F(a,b)F(a, b). (1≤y≤n1 \le y \le n, 1≤z≤1051 \le z \le 10^5)
  • 2 y z: print F(a[y..z],b)F(a[y..z], b). (1≤y≤z≤n1 \le y \le z \le n)
  • 3 y z: print f(b[y..m],b[z..m])f(b[y..m], b[z..m]). (1≤y,z≤m1 \le y, z \le m)
  • 4 p q r s: print yes if the sequence [bp,bp+1,…,bq,br,br+1,…,bs][b_p, b_{p+1}, \ldots, b_q, b_r, b_{r+1}, \ldots, b_s] is a contiguous subsequence of bb, and no otherwise. (1≤p≤q≤m1 \le p \le q \le m, 1≤r≤s≤m1 \le r \le s \le m)

a[l..r]a[l..r] is the subsequence [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r]. The same definition applies to bb.

For two sequences xx and yy:

  • f(x,y)f(x, y) is the length of the longest common prefix of xx and yy.
  • F(x,y)F(x, y) is the pair of integers (p,q)(p, q), where pp is the maximum of f(z,y)f(z, y) over all suffixes zz of xx, and qq is the number of suffixes zz that attain this maximum.

Input

The first line gives the length nn of aa. (1≤n≤1051 \le n \le 10^5)

The next line gives nn integers a1,a2,…,ana_1, a_2, \ldots, a_n. (1≤ai≤1051 \le a_i \le 10^5)

The next line gives the length mm of bb. (1≤m≤1051 \le m \le 10^5)

The next line gives mm integers b1,b2,…,bmb_1, b_2, \ldots, b_m. (1≤bi≤1051 \le b_i \le 10^5)

The next line gives the number of queries qq. (1≤q≤1051 \le q \le 10^5)

Each of the following qq lines contains a query as described above.

Output

Print the result of each query in order, one per line.

Examples1

  1. Example 1

    Input
    10
    1 2 3 3 3 1 2 3 2 1
    3
    1 3 1
    10
    3 1 3
    4 3 3 2 2
    2 2 10
    1 3 2
    2 7 9
    2 7 10
    2 3 9
    2 2 8
    1 7 1
    1 4 2
    
    Expected output
    1
    yes
    1 2
    1 3
    0 3
    1 1
    1 1
    1 1
    2 1
    2 1