#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;
}
