#P5692. [MtOI2019] 手牵手走向明天

    ID: 4687 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2019洛谷原创O2优化块状链表,块状数组,分块

[MtOI2019] 手牵手走向明天

Description

Rinne gives you a sequence a1,a2,,ana_1,a_2,\dots,a_n. You need to perform mm operations in order.

There are two types of operations:

  1. Given l,r,x,yl,r,x,y, change all numbers equal to xx among al,al+1,al+2,,ara_l,a_{l+1},a_{l+2},\dots,a_r into yy.

  2. Given l,r,x,yl,r,x,y, find i,ji,j such that i,j[l,r]i,j\in[l,r] and ai=x,aj=ya_i=x,a_j=y, and require ij|i-j| to be minimized. Output this minimum value. If there is no solution, output 1-1.

Input Format

The first line contains two positive integers n,mn,m, representing the sequence length and the number of operations.

The second line contains nn positive integers a1,a2,,ana_1,a_2,\dots,a_n.

The next mm lines each contain five positive integers op,l,r,x,yop,l,r,x,y. If op=1op=1, it is a modification operation. If op=2op=2, it is a query operation. The meanings of l,r,x,yl,r,x,y are as described in the statement.

Output Format

For each operation with op=2op=2, output one line with one integer, representing the answer.

6 5
1 1 4 5 1 4
1 1 3 1 7
2 1 4 7 7
1 1 5 7 3
2 2 6 1 3
2 3 3 3 3

0
3
-1

Hint

For 100%100\% of the testdata, 1n,m,ai,x,y1051\leq n,m,a_i,x,y\leq 10^5, 1lrn1\leq l\leq r\leq n.

This problem has 66 subtasks, with the following limits:

Subtask 11 (11 point): Guaranteed that for any operation, l=1,r=nl=1,r=n.

Subtask 22 (55 points): n,m50n,m\leq 50.

Subtask 33 (1818 points): n,m2000n,m\leq 2000.

Subtask 44 (77 points): Guaranteed that ai,x,y{1,2}a_i,x,y\in\{1,2\}.

Subtask 55 (2929 points): Guaranteed that when op=2op=2, x=yx=y.

Subtask 66 (4040 points): No special restrictions.

Time limit: 1.5s1.5\rm s

Memory limit: 512MB512\rm MB

Idea: nzhtl1477, mrsrz.

Solution: mrsrz, nzhtl1477.

Code: mrsrz.

Data: mrsrz.

Background: disangan233, mrsrz.

Translated by ChatGPT 5