∑|ΔEasyMAX|

Interview

Time limit1sMemory limit256 MB

Summary
Given a sequence of tempos, answer queries that ask for the sum of absolute differences between consecutive values over a range.
Level

Easy2 of 10

Topics
Prefix sum, Array, Math, Implementation
Solved
No attempts yet

Problem

GUN, a composer, likes writing songs whose tempo changes.

After finishing a song with all his effort, GUN became curious about the total per-second tempo change of his song. But GUN's song has so many places where the tempo changes that computing the sum of changes over a range by hand is hard. GUN asks you to find the sum of per-second tempo changes over specific ranges of the song. Help GUN find the sum of per-second tempo changes for the given ranges.

The sum of per-second tempo changes between second i and second j is defined as ∑k=ij−1∣ak+1−ak∣ \sum_{k=i}^{j-1}|a_{k+1} - a_k|, and when j−1<ij-1 < i, ∑k=ij−1∣ak+1−ak∣=0 \sum_{k=i}^{j-1}|a_{k+1} - a_k| = 0.

The symbol |a| denotes, for any real number a, |a| = -a if a<0 and |a| = a if a≥0.

Input

The first line of the input gives N(1 ≤ N ≤ 1,000), the length in seconds of the song GUN wrote, and Q(1 ≤ Q ≤ 1,000), the number of ranges whose per-second tempo change sums must be computed, separated by a space.

The second line gives the sequence a1 a2, ... , an representing the tempo of GUN's song in order, separated by spaces, where ai(-104 ≤ ai ≤ 104) is the tempo at second i.

From the third line, Q lines follow, each giving the start and end points Q(i,l), Q(i,r) (1 ≤ Q(i,l) ≤ Q(i,r) ≤ N) of a range whose change sum must be computed.

Output

For each range of GUN's song, output the sum of per-second tempo changes in input order over Q lines.

Examples2

  1. Example 1

    Input
    4 3
    100 101 382 573
    1 1
    1 3
    2 4
    
    Expected output
    0
    282
    472
    
  2. Example 2

    Input
    5 3
    1 0 1 0 -11
    1 5
    3 5
    2 4
    
    Expected output
    14
    12
    2