#K84677. Reverse Subarrays

    ID: 36473 Type: Default 1000ms 256MiB

Reverse Subarrays

Reverse Subarrays

You are given an array of integers and a sequence of operations. In each operation, you are required to reverse a subarray specified by two 1-indexed indices ( i ) and ( j ). After performing all operations, output the final state of the array.

For example, if the array is initially ( [1, 2, 3, 4, 5] ) and the operations are ( (1, 3) ), ( (2, 4) ), and ( (1, 5) ), the final array will be ( [5, 2, 1, 4, 3] ).

The reversal operation can be expressed in Python-like pseudocode as:
( arr[i-1:j] = arr[i-1:j][::-1] ).

inputFormat

The first line of input contains two integers ( n ) and ( m ), where ( n ) is the number of elements in the array and ( m ) is the number of operations. The second line contains ( n ) space-separated integers representing the array. Each of the next ( m ) lines contains two integers ( i ) and ( j ) (1-indexed) denoting the starting and ending positions of the subarray to reverse.

outputFormat

Output a single line with ( n ) space-separated integers representing the array after all reversal operations have been applied.## sample

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