#P6718. [CCO 2018] Flop Sorting

[CCO 2018] Flop Sorting

Description

Robert created a segment tree problem. The problem is:

Given a permutation aia_i of length NN, we define a flop operation as swapping the minimum value and the maximum value in an interval. Now you are given QQ flop operations; each time you perform a flop operation on [l,r][l,r]. Find the final sequence after performing the QQ flop operations.

After finishing the statement, the next step is to prepare the testdata.

Now you are given NN, the initial sequence aia_i, and the final sequence. Find the flop operations to be performed in between.

Input Format

The first line contains an integer representing the length of the sequence NN.
The second line contains NN integers representing the initial sequence aia_i.
The third line contains NN integers representing the final sequence.

Output Format

First, output an integer QQ, the number of flop operations to perform.
Then output QQ lines, each with two integers l,rl, r, meaning to perform a flop operation on [l,r][l,r].

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

Hint

Sample Explanation

For sample 11, the 44 flop operations performed are:

  • Perform a flop operation on [2,3][2,3], swapping 33 and 55.
  • Perform a flop operation on [3,6][3,6], swapping 66 and 22.
  • Perform a flop operation on [2,5][2,5], swapping 55 and 22.
  • Perform a flop operation on [4,5][4,5], swapping 55 and 44.

Constraints

For 100%100\% of the data, 1aiN40961 \le a_i \le N \le 4096, 1Q3×1051 \le Q \le 3 \times 10^5, and 1lrN1 \le l \le r \le N.
For 20%20\% of the data, N100N \le 100.
For another 40%40\% of the data, N2048N \le 2048.

Notes

Translated from Canadian Computing Olympiad 2018 Day 2 C Flop Shorting

Translated by ChatGPT 5