#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <functional>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <unordered_map>
#include <set>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
int n, q;
pi spt[18][140000];
int a[140000], lg[140000];
pi query(int s, int e){
int lev = lg[e-s+1];
return max(spt[lev][s + (1<<lev) - 1], spt[lev][e]);
}
void solve(int s, int e);
void init(){
scanf("%d %d",&n,&q);
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
spt[0][i] = pi(a[i], i);
}
int p = 0;
for(int i=1; i<=n; i++){
while((2 << p) <= i) p++;
lg[i] = p;
}
for(int i=1; i<=17; i++){
for(int j=1; j<=n; j++){
spt[i][j] = spt[i-1][j];
if(j > (1 << (i-1))){
spt[i][j] = max(spt[i][j], spt[i-1][j - (1<< (i-1))]);
}
}
}
solve(1, n);
}
struct seg{
lint tree[530000], lazy[530000];
void lazydown(int p, int s, int e){
int m = (s+e)/2;
lazy[2*p] += lazy[p];
lazy[2*p+1] += lazy[p];
tree[2*p] += lazy[p] * (m - s + 1);
tree[2*p+1] += lazy[p] * (e - m);
lazy[p] = 0;
}
void add(int s, int e, int ps, int pe, int p, lint v){
if(e < ps || pe < s) return;
if(s <= ps && pe <= e){
tree[p] += v * (pe - ps + 1);
lazy[p] += v;
return;
}
lazydown(p, ps, pe);
int pm = (ps + pe) / 2;
add(s, e, ps, pm, 2*p, v);
add(s, e, pm+1, pe, 2*p+1, v);
tree[p] = tree[2*p] + tree[2*p+1];
}
lint sum(int s, int e, int ps, int pe, int p){
if(e < ps || pe < s) return 0;
if(s <= ps && pe <= e) return tree[p];
lazydown(p, ps, pe);
int pm = (ps + pe) / 2;
return sum(s, e, ps, pm, 2*p) + sum(s, e, pm+1, pe, 2*p+1);
}
}lin, tot;
lint ret[140000];
struct trapezoid{int s, e, pos, v;};
struct qry{int s, e, idx, buho;};
vector<trapezoid> v;
vector<qry> w[135005];
void solve(int s, int e){
if(s > e) return;
pi t = query(s, e);
v.push_back({t.second, e, s-1, -t.first});
v.push_back({t.second, e, t.second, t.first});
solve(s, t.second-1);
solve(t.second+1, e);
}
int main(){
init();
for(int i=0; i<q; i++){
int s, e;
scanf("%d %d",&s,&e);
w[s-1].push_back({s, e, i, -1});
w[e].push_back({s, e, i, 1});
}
sort(v.begin(), v.end(), [&](const trapezoid &a, const trapezoid &b){
return a.pos < b.pos;
});
for(auto &k : v){
lin.add(k.s, k.e, 1, n, 1, k.v);
}
int p = 0;
for(int i=0; i<=n; i++){
for(auto &j : w[i]){
ret[j.idx] += 1ll * j.buho * (1ll * i * lin.sum(j.s, j.e, 1, n, 1) + tot.sum(j.s, j.e, 1, n, 1));
/*for(auto &k : v){
int is = min(j.e, k.e) - max(j.s, k.s) + 1;
ret[j.idx] += 1ll * j.buho * max(is, 0) * k.v * min(i, k.pos);
}*/
}
while(p < v.size() && v[p].pos == i){
lin.add(v[p].s, v[p].e, 1, n, 1, -v[p].v);
tot.add(v[p].s, v[p].e, 1, n, 1, 1ll * v[p].v * v[p].pos);
p++;
}
}
for(int i=0; i<q; i++){
printf("%lld\n",ret[i]);
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8YXNzZXJ0Lmg+CiNpbmNsdWRlIDxsaW1pdHMuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHRpbWUuaD4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxkZXF1ZT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPH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