#P8415. [THUPC 2022 决赛] pmrmscxip

[THUPC 2022 决赛] pmrmscxip

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:

  • Modify operation: given x,yx,y, change axa_x to yy.
  • Query operation: given l,r,xl,r,x, find the longest sub-interval [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 interval 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 in order.

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

The next mm lines each contain either 1,x,y1,x,y or 2,l,r,x2,l,r,x, indicating one modify operation or one 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

Constraints:

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 distinct.

For modify 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