#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int a[100010], ans[100010];
int cnt[100010];
int sqn, curl=1, curr=0, z=0;
vector<pair<pair<int, int>, int> > v;
bool cmp(const pair<pair<int, int>, int> &x, const pair<pair<int, int>, int> &y) {
int z1=x.first.first/sqn;
int z2=y.first.first/sqn;
if(z1!=z2)
return z1<z2;
return x.first.second<y.first.second;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int i, j, q, k, x, y, n;
cin>>n>>q;
for(i=1; i<=n; ++i) {
cin>>a[i];
}
sqn=sqrt(n);
for(i=1; i<=q; ++i) {
cin>>x>>y;
v.push_back({{x,y}, i});
}
sort(v.begin(), v.end(), cmp);
for(i=0; i<v.size(); ++i) {
int l=v[i].first.first;
int r=v[i].first.second;
//cout<<l<<" "<<r<<" "<<v[i].second<<endl;
while(curr<r) {
curr++;
cnt[a[curr]]++;
if(cnt[a[curr]-1]==0 && cnt[a[curr]+1]==0) {
//cout<<curr<<endl;
z++;
}
else if(cnt[a[curr]-1] && cnt[a[curr]+1]) z--;
}
while(curr>r) {
cnt[a[curr]]--;
if(cnt[a[curr]-1] && cnt[a[curr]+1])
z++;
else if(cnt[a[curr]-1]==0 && cnt[a[curr]+1]==0) z--;
curr--;
}
while(curl<l) {
cnt[a[curl]]--;
if(cnt[a[curl]-1] && cnt[a[curl]+1])
z++;
else if(cnt[a[curl]-1]==0 && cnt[a[curl]+1]==0) z--;
curl++;
}
while(curl>l) {
curl--;
cnt[a[curl]]++;
if(cnt[a[curl]-1]==0 && cnt[a[curl]+1]==0)
z++;
else if(cnt[a[curl]-1] && cnt[a[curl]+1]) z--;
}
ans[v[i].second]=z;
}
for(i=1; i<=q; ++i)
cout<<ans[i]<<endl;
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdW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ICAgICAgIHdoaWxlKGN1cnI8cikgewogICAgICAgICAgICBjdXJyKys7CiAgICAgICAgICAgIGNudFthW2N1cnJdXSsrOwogICAgICAgICAgICBpZihjbnRbYVtjdXJyXS0xXT09MCAmJiBjbnRbYVtjdXJyXSsxXT09MCkgewogICAgICAgICAgICAgICAgLy9jb3V0PDxjdXJyPDxlbmRsOwogICAgICAgICAgICAgICAgeisrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYoY250W2FbY3Vycl0tMV0gJiYgY250W2FbY3Vycl0rMV0pICAgei0tOwogICAgICAgIH0KICAgICAgICB3aGlsZShjdXJyPnIpIHsKICAgICAgICAgICAgY250W2FbY3Vycl1dLS07CiAgICAgICAgICAgIGlmKGNudFthW2N1cnJdLTFdICYmIGNudFthW2N1cnJdKzFdKQogICAgICAgICAgICAgICAgeisrOwogICAgICAgICAgICBlbHNlIGlmKGNudFthW2N1cnJdLTFdPT0wICYmIGNudFthW2N1cnJdKzFdPT0wKSB6LS07CiAgICAgICAgICAgIGN1cnItLTsKICAgICAgICB9CiAgICAgICAgd2hpbGUoY3VybDxsKSB7CiAgICAgICAgICAgIGNudFthW2N1cmxdXS0tOwogICAgICAgICAgICBpZihjbnRbYVtjdXJsXS0xXSAmJiBjbnRbYVtjdXJsXSsxXSkKICAgICAgICAgICAgICAgIHorKzsKICAgICAgICAgICAgZWxzZSBpZihjbnRbYVtjdXJsXS0xXT09MCAmJiBjbnRbYVtjdXJsXSsxXT09MCkgei0tOwogICAgICAgICAgICBjdXJsKys7CiAgICAgICAgfQogICAgICAgIHdoaWxlKGN1cmw+bCkgewogICAgICAgICAgICBjdXJsLS07CiAgICAgICAgICAgIGNudFthW2N1cmxdXSsrOwogICAgICAgICAgICBpZihjbnRbYVtjdXJsXS0xXT09MCAmJiBjbnRbYVtjdXJsXSsxXT09MCkKICAgICAgICAgICAgICAgIHorKzsKICAgICAgICAgICAgZWxzZSBpZihjbnRbYVtjdXJsXS0xXSAmJiBjbnRbYVtjdXJsXSsxXSkgICB6LS07CiAgICAgICAgfQogICAgICAgIGFuc1t2W2ldLnNlY29uZF09ejsKICAgIH0KICAgIGZvcihpPTE7IGk8PXE7ICsraSkKICAgICAgICBjb3V0PDxhbnNbaV08PGVuZGw7CiAgICByZXR1cm4gMDsKfQo=