#P3391. Maintain Ordered Sequence with Interval Reversal

    ID: 16648 Type: Default 1000ms 256MiB

Maintain Ordered Sequence with Interval Reversal

Maintain Ordered Sequence with Interval Reversal

You are required to implement a data structure to maintain an ordered sequence of numbers. Your structure should support an operation to reverse any sub-interval of the sequence.

For example, given the initial sequence: $$5\ 4\ 3\ 2\ 1$$ and a reversal operation on the interval \([2,4]\), the sequence becomes: $$5\ 2\ 3\ 4\ 1$$

Indices are 1-indexed.

inputFormat

The first line contains two integers \(N\) and \(Q\), where \(N\) is the length of the sequence and \(Q\) is the number of queries.

The second line contains \(N\) space-separated integers representing the initial sequence.

Each of the following \(Q\) lines contains two integers \(L\) and \(R\) (\(1 \leq L \leq R \leq N\)), indicating that the subarray from index \(L\) to \(R\) should be reversed.

outputFormat

Print the final sequence after performing all queries. The integers should be printed in a single line, separated by a space.

sample

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