#K93607. Reverse Operations and Count Inversions

    ID: 38457 Type: Default 1000ms 256MiB

Reverse Operations and Count Inversions

Reverse Operations and Count Inversions

You are given an array of n integers and q operations. Each operation specifies two indices l and r (1-indexed) which define a subarray. For each operation, you must reverse the subarray of the array between indices l and r (inclusive).

After performing all the operations, compute the number of inversions in the resulting array. An inversion is defined as a pair of indices \( (i, j) \) such that \( i a[j] \). In other words, the number of inversion pairs is given by:

[ \text{Inversions} = #{(i,j) \mid i < j \text{ and } a[i] > a[j]} ]

Your task is to output the inversion count after all reversal operations have been performed.

inputFormat

The input is given via standard input (stdin) and has the following format:

n q
a1 a2 a3 ... an
l1 r1
l2 r2
...
lq rq

Where:

  • n (1 \(\leq\) n \(\leq\) 105) is the number of elements in the array.
  • q (0 \(\leq\) q \(\leq\) 105) is the number of reversal operations.
  • The second line contains n space-separated integers representing the elements of the array.
  • Each of the next q lines contains two integers \(l\) and \(r\) (1-indexed) that specify the subarray to be reversed.

outputFormat

Output a single integer which is the number of inversions in the array after performing all the reversal operations. This output should be written to standard output (stdout).

## sample
5 3
6 3 9 1 7
1 3
2 4
1 5
5

</p>