#include<bits/stdc++.h>
typedef int LL;
typedef double dl;
#define opt operator
#define pb push_back
#define pii std::pair<LL,LL>
const LL maxn=1e6+9,mod=998244353,inf=0x3f3f3f3f;
LL Read(){
	LL x(0),f(1); char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') f=-1; c=getchar();
	}
	while(c>='0' && c<='9'){
		x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
	}return x*f;
}
void Chkmin(LL &x,LL y){
	if(y<x) x=y;
}
void Chkmax(LL &x,LL y){
	if(y>x) x=y;
}
LL add(LL x,LL y){
	return x+=y,x>=mod?x-mod:x;
}
LL dec(LL x,LL y){
	return x-=y,x<0?x+mod:x;
}
LL mul(LL x,LL y){
	return 1ll*x*y%mod;
}
LL Pow(LL base,LL b){
	LL ret(1); while(b){
		if(b&1) ret=mul(ret,base); base=mul(base,base); b>>=1;
	}return ret;
}
struct Range{
	LL l,r;
}range[maxn];
LL n,q,ret;
LL a[maxn],lt[maxn],rt[maxn],pos[maxn],rd[maxn],ld[maxn];
LL ls[maxn][2],rs[maxn][2];
void Init(){
	n=Read(); q=Read();
	for(LL i=1;i<=n;++i) a[i]=Read();
	for(LL i=1;i<=q;++i){
		LL l(Read()),r(Read());
		range[i]=(Range){l,r};
	}
}
void Fir(){
	for(LL i=1;i<=n;++i) pos[a[i]]=i;
	for(LL i=1;i<=n;++i) rd[i]=i,ld[i]=i;
	for(LL i=1;i<=n;++i){
		ls[i][0]=ls[i][1]=0;
		rs[i][0]=rs[i][1]=0;
	}
}
void Modify(LL l,LL r){
	LL lt,rt;
	lt=l;
	while(lt<r){
		rt=rd[lt];
		Chkmin(rt,r);
		for(LL i=lt;i<=rt;++i){
			if(rd[i]<r){
				for(LL j=rd[i]+1;j<=r;++j){
					if(a[j]<a[i]){
						LL &x(rs[i][0]);
						if(!x || (x && a[x]<a[j])){
							ret-=ls[x][1]==i;
							x=j;
							ret+=ls[x][1]==i;
						}
					}else{
						LL &x(rs[i][1]);
						if(!x || (x && a[x]>a[j])){
							ret-=ls[x][0]==i;
							x=j;
							ret+=ls[x][0]==i;
						}
					}
				}
				rd[i]=r;
			}else break;
		}
		lt=rt+1;
	}
	rt=r;
	while(rt>l){
		lt=ld[rt];
		Chkmax(lt,l);
		for(LL i=rt;i>=lt;--i){
			if(ld[i]>l){
				for(LL j=l;j<=ld[i]-1;++j){
					if(a[j]<a[i]){
						LL &x(ls[i][0]);
						if(!x || (x && a[x]<a[j])){
							ret-=rs[x][1]==i;
							x=j;
							ret+=rs[x][1]==i;
						}
					}else{
						LL &x(ls[i][1]);
						if(!x || (x && a[x]>a[j])){
							ret-=rs[x][0]==i;
							x=j;
							ret+=rs[x][0]==i;
						}
					}
				}
				ld[i]=l;
			}else break;
		}
		rt=lt-1;
	}
}
void Solve(){
	for(LL ti=1;ti<=q;++ti){
		LL l(range[ti].l),r(range[ti].r);
		Modify(l,r);
		printf("%d\n",ret);
	}
}
int main(){
	Init();
	Fir();
	Solve();
	return 0;
}



