#K14241. Sum of Absolute Differences in Subarray

    ID: 24091 Type: Default 1000ms 256MiB

Sum of Absolute Differences in Subarray

Sum of Absolute Differences in Subarray

You are given an array of n integers and q queries. For each query, you are provided with two integers l and r (1-indexed) representing the endpoints of a subarray. Your task is to compute the sum of the absolute differences between each pair of consecutive elements in the subarray.

Formally, for a subarray a[l], a[l+1], \dots, a[r], compute:

[ S = \sum_{i=l}^{r-1} \left| a[i+1] - a[i] \right| ]

If the subarray contains only one element, the sum is 0. The input is read from standard input (stdin) and the result for each query should be output to standard output (stdout) as space-separated values.

inputFormat

The input is read from stdin and is structured as follows:

  1. An integer n, the number of elements in the array.
  2. n space-separated integers representing the array.
  3. An integer q, the number of queries.
  4. q lines follow, each containing two space-separated integers l and r, representing a query.

Note: The array is 1-indexed for the queries.

outputFormat

For each query, output the sum of the absolute differences between consecutive elements in the specified subarray. The answers for all queries should be printed on a single line separated by a single space.

## sample
1
5
1
1 1
0