#P10151. [Ynoi1999] SMV CC-64“蝰蛇”

[Ynoi1999] SMV CC-64“蝰蛇”

Background

Problem Description

Given a sequence a1,,ana_1,\dots,a_n and a permutation b1,,bnb_1,\dots,b_n, there are mm operations in total:

  • Update operation: given x,yx,y, change axa_x to yy.
  • Query operation: given l,r,xl,r,x, find the longest subsegment [l,r][l',r'] within [l,r][l,r] (i.e. satisfying llrrl\le l'\le r'\le r), such that for all li<rl'\le i<r' we have ai+1=baia_{i+1}=b_{a_i}, and there exists some lirl'\le i\le r' with ai=xa_i=x. You need to output the maximum possible value of rl+1r'-l'+1 that satisfies the conditions; if no such subsegment exists, output 00.

Input Format

The first line contains two integers n,mn,m.

The second line contains nn integers representing a1,,ana_1,\dots,a_n.

The third line contains nn integers representing b1,,bnb_1,\dots,b_n.

The next mm lines each contain either 1,x,y1,x,y or 2,l,r,x2,l,r,x, indicating an update operation or a query operation.

All input values are integers.

Output Format

For each query operation, output one line containing the corresponding answer.

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
1
1
0
1
1
3
2
1
0

Hint

Idea: s_r_f, Solution: ccz181078, Code: ccz181078, Data: ccz181078.

Constraints for 100%100\% of the testdata:

1n,m1061\le n,m\le 10^6.

1ain1\le a_i\le n.

1bin1\le b_i\le n, and all bib_i are pairwise distinct.

For update operations, 1x,yn1\le x,y\le n.

For query operations, 1lrn1\le l\le r\le n, 1xn1\le x\le n.

Translated by ChatGPT 5