#P5692. Sequence Operation and Minimum Distance Query
Sequence Operation and Minimum Distance Query
Sequence Operation and Minimum Distance Query
You are given a sequence a1, a2, ..., an and you need to perform m operations sequentially.
There are two types of operations:
-
Update Operation: Given integers l, r, x, y, for every index i in the range [l, r] (1-indexed) such that ai = x, change the value to y.
-
Query Operation: Given integers l, r, x, y, find indices i and j (both in the range [l, r]) such that ai = x and aj = y while minimizing |i - j|. If a valid pair does not exist, output
-1
.Note: When x = y, if there exists at least one index i in the range with ai = x, then the minimum |i - i| is
0
.
All indices are 1-indexed.
The formulas should be rendered in LaTeX. For example, the sequence is given as \(a_1, a_2, \dots, a_n\) and an update is applied to indices in \([l, r]\).
inputFormat
The first line contains two integers n and m, representing the size of the sequence and the number of operations.
The second line contains n integers, the initial sequence \(a_1, a_2, \dots, a_n\).
The next m lines each describe an operation in one of the following formats:
- For an update operation:
1 l r x y
- For a query operation:
2 l r x y
outputFormat
For each query operation (operation type 2), output the minimum absolute difference \(|i - j|\) found for the specified values in the given range. If no valid pair exists, output -1
.
sample
5 5
1 2 3 2 1
2 1 5 1 2
1 2 4 2 3
2 1 5 1 2
2 1 5 1 1
2 2 5 3 1
1
-1
0
1
</p>