#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long int llu;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define mem(a, v) memset(a, v, sizeof(a))
#define PI acos(-1)
#define S(a) scanf("%d",&a)
#define SL(a) scanf("%lld",&a)
#define S2(a, b) scanf("%d%d",&a,&b)
#define nl printf("\n")
#define deb(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x, y) cout<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define deb3(x, y, z) cout<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define debv(x) {cout<<#x<<" : "<<endl; for(int ii =0; ii < x.size(); ii++) cout<<x[ii]<<" "; cout<<endl; }
#define debarr(x, xs) {cout<<#x<<" : "<<endl; for(int ii =0; ii < xs; ii++) cout<<x[ii]<<" "; cout<<endl; }
//auto T=clock();
//cout<<double(clock()-T)/CLOCKS_PER_SEC<<'\n';
const ll mod = 1000000007LL;
const int lmt = 100005;
bool vis[lmt];
int a[lmt];
ll output[lmt];
int buck = sqrt(100000);
struct node {
int l, r, id;
node(int a, int b, int c) {
l = a;
r = b;
id = c;
}
};
bool cmp(const node &a, const node &b) {
if((a.l/buck) != (b.l/buck))
return (a.l/buck) < (b.l/buck);
return ((a.r/buck) < (b.r/buck));
}
vector<node> query;
ll ans = 0;
void add(int idx) {
int ele = a[idx];
if((vis[ele+1]) && (vis[ele-1]))
ans--;
else if((!vis[ele+1]) && (!vis[ele-1]))
ans++;
vis[ele] = true;
}
void remove(int idx) {
int ele = a[idx];
if((vis[ele+1]) && (vis[ele-1]))
ans++;
else if((!vis[ele+1]) && (!vis[ele-1]))
ans--;
vis[ele] = false;
}
int main(){
mem(vis, false);
int n, q;
S2(n, q);
for(int i = 1; i <= n; i++)
S(a[i]);
for(int i = 0; i < q; i++) {
int l, r;
S2(l, r);
query.pb(node(l, r, i));
}
sort(query.begin(), query.end(), cmp);
int _l = 1, _r = 1;
add(1);
for(int i = 0; i < q; i++) {
int L = query[i].l, R = query[i].r;
//deb2(L, R);
while(_l > L) {
_l--;
add(_l);
}
while(_r < R) {
_r++;
add(_r);
}
while(_r > R) {
remove(_r);
_r--;
}
while(_l < L) {
remove(_l);
_l++;
}
output[query[i].id] = ans;
}
for(int i = 0; i < q; i++)
printf("%lld\n", output[i]);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgaW50IGxsdTsKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBYIGZpcnN0CiNkZWZpbmUgWSBzZWNvbmQKI2RlZmluZSBtZW0oYSwgdikgbWVtc2V0KGEsIHYsIHNpemVvZihhKSkKI2RlZmluZSBQSSBhY29zKC0xKQojZGVmaW5lIFMoYSkgc2NhbmYoIiVkIiwmYSkKI2RlZmluZSBTTChhKSBzY2FuZigiJWxsZCIsJmEpCiNkZWZpbmUgUzIoYSwgYikgc2NhbmYoIiVkJWQiLCZhLCZiKQojZGVmaW5lIG5sIHByaW50ZigiXG4iKQojZGVmaW5lIGRlYih4KSBjb3V0PDwjeDw8IiA6ICI8PHg8PGVuZGw7CiNkZWZpbmUgZGViMih4LCB5KSBjb3V0PDwjeDw8IiA6ICI8PHg8PCIgfCAiPDwjeTw8IiA6ICI8PHk8PGVuZGw7CiNkZWZpbmUgZGViMyh4LCB5LCB6KSBjb3V0PDwjeDw8IiA6ICI8PHg8PCIgfCAiPDwjeTw8IiA6ICI8PHk8PCIgfCAiPDwjejw8IiA6ICI8PHo8PGVuZGw7CiNkZWZpbmUgZGVidih4KSB7Y291dDw8I3g8PCIgOiAiPDxlbmRsOyBmb3IoaW50IGlpID0wOyBpaSA8IHguc2l6ZSgpOyBpaSsrKSBjb3V0PDx4W2lpXTw8Ii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aWR4KSB7CiAgICBpbnQgZWxlID0gYVtpZHhdOwogICAgaWYoKHZpc1tlbGUrMV0pICYmICh2aXNbZWxlLTFdKSkKICAgICAgICBhbnMrKzsKICAgIGVsc2UgaWYoKCF2aXNbZWxlKzFdKSAmJiAoIXZpc1tlbGUtMV0pKQogICAgICAgIGFucy0tOwogICAgdmlzW2VsZV0gPSBmYWxzZTsKfQoKCmludCBtYWluKCl7CiAgICBtZW0odmlzLCBmYWxzZSk7CiAgICBpbnQgbiwgcTsKICAgIAogICAgUzIobiwgcSk7CiAgICAKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICAgICAgIFMoYVtpXSk7CiAgICAKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBxOyBpKyspIHsKICAgICAgICBpbnQgbCwgcjsKICAgICAgICBTMihsLCByKTsKICAgICAgICBxdWVyeS5wYihub2RlKGwsIHIsIGkpKTsKICAgIH0KCiAgICBzb3J0KHF1ZXJ5LmJlZ2luKCksIHF1ZXJ5LmVuZCgpLCBjbXApOwoKICAgIGludCBfbCA9IDEsIF9yID0gMTsKICAgIGFkZCgxKTsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBxOyBpKyspIHsKICAgICAgICBpbnQgTCA9IHF1ZXJ5W2ldLmwsIFIgPSBxdWVyeVtpXS5yOwogICAgICAgIC8vZGViMihMLCBSKTsKICAgICAgICB3aGlsZShfbCA+IEwpIHsKICAgICAgICAgICAgX2wtLTsKICAgICAgICAgICAgYWRkKF9sKTsKICAgICAgICB9CiAgICAgICAgd2hpbGUoX3IgPCBSKSB7CiAgICAgICAgICAgIF9yKys7CiAgICAgICAgICAgIGFkZChfcik7CiAgICAgICAgfQogICAgICAgIHdoaWxlKF9yID4gUikgewogICAgICAgICAgICByZW1vdmUoX3IpOwogICAgICAgICAgICBfci0tOwogICAgICAgIH0KICAgICAgICB3aGlsZShfbCA8IEwpIHsKICAgICAgICAgICAgcmVtb3ZlKF9sKTsKICAgICAgICAgICAgX2wrKzsKICAgICAgICB9CgogICAgICAgIG91dHB1dFtxdWVyeVtpXS5pZF0gPSBhbnM7CiAgICB9CiAgICAKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBxOyBpKyspCiAgICAgICAgcHJpbnRmKCIlbGxkXG4iLCBvdXRwdXRbaV0pOwoKICAgIHJldHVybiAwOwp9