1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<algorithm>
6 #define inf 1000000001
7 using namespace std;
8 const int N=200085;
9 long long F[4*N],Tag[4*N],f[4*N],tag[4*N];
10 long long ans[N];
11 long long a[N],st[N],L[N],R[N];
12 struct opr{int id,l,r,ps,dd;}op[2*N];
13 struct mar{int l,r,dd,ps;long long w;}data[2*N];
14 int n,m,l,r,tt,tot;
15 inline long long read()
16 {
17 int x=0,f=1;char ch=getchar();
18 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
19 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
20 return x*f;
21 }
22 bool cmp1(mar a,mar b)
23 {
24 return a.ps<b.ps;
25 }
26 bool cmp2(opr a,opr b)
27 {
28 return a.ps<b.ps;
29 }
30 void INS(int i,int l,int r,int ll,int rr,long long w)
31 {
32 if((ll<=l)&&(r<=rr))
33 {
34 Tag[i]+=w;F[i]+=w*(long long)(r-l+1);return;
35 }
36 int mid=(l+r)/2;
37 if(ll<=mid)INS(i*2,l,mid,ll,rr,w);if(mid+1<=rr)INS(i*2+1,mid+1,r,ll,rr,w);
38 F[i]=Tag[i]*(long long)(r-l+1)+F[i*2]+F[i*2+1];
39 }
40 void ins(int i,int l,int r,int ll,int rr,long long w)
41 {
42 if((ll<=l)&&(r<=rr))
43 {
44 tag[i]+=w;f[i]+=w*(long long)(r-l+1);return;
45 }
46 int mid=(l+r)/2;
47 if(ll<=mid)ins(i*2,l,mid,ll,rr,w);if(mid+1<=rr)ins(i*2+1,mid+1,r,ll,rr,w);
48 f[i]=tag[i]*(long long)(r-l+1)+f[i*2]+f[i*2+1];
49 }
50 long long ASK(int i,int l,int r,int ll,int rr)
51 {
52 if((ll<=l)&&(r<=rr))return F[i];
53 int mid=(l+r)/2;long long tmp=Tag[i]*(long long)(min(rr,r)-max(ll,l)+1);
54 if(ll<=mid)tmp+=ASK(i*2,l,mid,ll,rr);if(mid+1<=rr)tmp+=ASK(i*2+1,mid+1,r,ll,rr);
55 return tmp;
56 }
57 long long ask(int i,int l,int r,int ll,int rr)
58 {
59 if((ll<=l)&&(r<=rr))return f[i];
60 int mid=(l+r)/2;long long tmp=tag[i]*(long long)(min(rr,r)-max(ll,l)+1);
61 if(ll<=mid)tmp+=ask(i*2,l,mid,ll,rr);if(mid+1<=rr)tmp+=ask(i*2+1,mid+1,r,ll,rr);
62 return tmp;
63 }
64 int main()
65 {
66 int n=read(),m=read();
67 for(int i=1;i<=n;i++)a[i]=read();
68 a[0]=-inf;
69 int top=0;st[0]=0;
70 for(int i=1;i<=n;i++)
71 {
72 while(a[st[top]]>=a[i]){L[st[top]]=st[top-1]+1;R[st[top]]=i-1;top--;}
73 st[++top]=i;
74 }
75 while(top){L[st[top]]=st[top-1]+1;R[st[top]]=n;top--;}
76 for(int i=1;i<=n;i++)
77 {
78 if(L[i]<=R[i])
79 {
80 data[++tt].w=a[i];data[tt].l=L[i];data[tt].r=i;data[tt].ps=i;data[tt].dd=1;
81 data[++tt].w=a[i];data[tt].l=L[i];data[tt].r=i;data[tt].ps=R[i]+1;data[tt].dd=-1;
82 }
83 }
84 sort(data+1,data+tt+1,cmp1);
85 for(int i=1;i<=m;i++)
86 {
87 l=read();r=read();
88 op[++tot].id=i;op[tot].l=l;op[tot].r=r;op[tot].ps=r;op[tot].dd=1;
89 op[++tot].id=i;op[tot].l=l;op[tot].r=r;op[tot].ps=l-1;op[tot].dd=-1;
90 }
91 sort(op+1,op+tot+1,cmp2);int j=1;
92 for(int i=1;i<=tot;i++)
93 {
94 while((data[j].ps<=op[i].ps)&&(j<=tt))
95 {
96 long long now=data[j].ps;long long w=now*(long long)data[j].w;
97 INS(1,1,n,data[j].l,data[j].r,w*data[j].dd);
98 ins(1,1,n,data[j].l,data[j].r,data[j].w*data[j].dd);
99 j++;
100 }
101 long long w;
102 w=ask(1,1,n,op[i].l,op[i].r)*(long long)(op[i].ps+1);
103 w=w-ASK(1,1,n,op[i].l,op[i].r);
104 ans[op[i].id]+=w*op[i].dd;
105 }
106 for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
107 return 0;
108 }