Under construction

Still being built. Things may change

Big Data Permutation

Time limit15sMemory limit2048 MB

Summary
Given an array a and a permutation b, handle point updates to a and answer queries for the longest chain a[j+1]=b[a[j]] inside [l, r] that contains the value x.
Level

Hard9 of 10

Topics
Segment tree, Array, Graph
Solved
No attempts yet

Problem

You are given a sequence a1,…,ana_1, \ldots, a_n and a permutation b1,…,bnb_1, \ldots, b_n. Process mm operations of the following two kinds.

  • Modification: given xx and yy, change axa_x to yy.
  • Query: given ℓ\ell, rr, and xx, find the longest sub-interval [ℓ′,r′][\ell', r'] of [ℓ,r][\ell, r] (that is, ℓ≤ℓ′≤r′≤r\ell \le \ell' \le r' \le r) such that aj+1=baja_{j+1} = b_{a_j} for every ℓ′≤j<r′\ell' \le j < r', and there is some ii with ℓ′≤i≤r′\ell' \le i \le r' and ai=xa_i = x. Output only the maximum length r′−ℓ′+1r' - \ell' + 1. If there is no such interval, output 00.

Input

The first line contains nn and mm (1≤n,m≤1061 \le n, m \le 10^6). The second line contains a1,…,ana_1, \ldots, a_n (1≤ai≤n1 \le a_i \le n). The third line contains b1,…,bnb_1, \ldots, b_n (1≤bi≤n1 \le b_i \le n; all bib_i are distinct). Each of the next mm lines is either 1 x y for a modification or 2 l r x for a query (1≤x,y≤n1 \le x, y \le n; 1≤ℓ≤r≤n1 \le \ell \le r \le n). There is at least one query.

Output

For each query, print the answer on its own line.

Examples1

  1. Example 1

    Input
    8 10
    1 4 7 3 8 2 4 7
    5 4 8 7 1 6 3 2
    2 6 6 2
    2 8 8 7
    1 4 3
    2 6 8 3
    2 4 4 3
    2 4 4 3
    2 6 8 4
    2 5 6 2
    2 1 8 1
    2 1 1 6
    
    Expected output
    1
    1
    0
    1
    1
    3
    2
    1
    0