#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int N=5*100005;
struct node{
int l;
int r;
int id;
}u[N];
int z;
bool cool(node a,node b)
{
if(a.l/z<b.l/z)
return true;
if(a.l/z>b.l/z)
return false;
return a.r<b.r;
}
int cnt[N];
int cp[N];
int ans[N];
int v[N];
map<int,int>cmp;
int main()
{
//ios_base:: sync_with_stdio(false); cin.tie(0);
//freopen("input.in","r",stdin);
int n,q;cin>>n>>q;
int fuk=0;
z=sqrt(n);
for(int i=0;i<n;i++)
scanf("%d",&v[i]),cp[i]=v[i];
sort(cp,cp+n);
for(int i=0;i<n;i++)if(cmp.find(cp[i])==cmp.end())cmp[cp[i]]=i+1;
for(int i=0;i<n;i++)v[i]=cmp[v[i]];
for(int i=0;i<q;i++)
{
int l,r;
scanf("%d %d",&l,&r);
node tmp;
tmp.l=l-1;
tmp.r=r-1;
tmp.id=i;
u[i]=tmp;
}
sort(u,u+q,cool);
int st=0,e=0;fuk=0;cnt[v[0]]++;
for(int i=0;i<q;i++)
{
int l=u[i].l;
int r=u[i].r;
// cout<<l<<" "<<r<<" "<<st<<" "<<e<<" "<<fuk<<endl;
while(st<l)
{
cnt[v[st]]--;
int gg=cnt[v[st]];
if(gg==1)
{fuk--;}
else if(gg==2){fuk++;}
st++;
}
while(st>l)
{
st--;
cnt[v[st]]++;
int gg=cnt[v[st]];
if(gg==2)
{
fuk++;
}
else if(gg==3)
{
fuk--;
}
}
while(e<r)
{
e++;
cnt[v[e]]++;
int gg=cnt[v[e]];
if(gg==2)
{fuk++;}
else if(gg==3){fuk--;}
}
while(e>r)
{
cnt[v[e]]--;
int gg=cnt[v[e]];
if(gg==1)
{fuk--;}
else if(gg==2){fuk++;}
e--;
}
ans[u[i].id]=fuk;
}
for(int i=0;i<q;i++)
printf("%d\n",ans[i]);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGludCBMTDsKCmNvbnN0IGludCBOPTUqMTAwMDA1OwoKc3RydWN0IG5vZGV7CiAgaW50IGw7CiAgaW50IHI7CiAgaW50IGlkOwp9dVtOXTsKCmludCB6Owpib29sIGNvb2wobm9kZSBhLG5vZGUgYikKewogICAgIGlmKGEubC96PGIubC96KQogICAgICAgIHJldHVybiB0cnVlOwogICAgIGlmKGEubC96PmIubC96KQogICAgICAgIHJldHVybiBmYWxzZTsKICAgICByZXR1cm4gYS5yPGIucjsKfQppbnQgY250W05dOwppbnQgY3BbTl07CmludCBhbnNbTl07CmludCB2W05dOwoKbWFwPGludCxpbnQ+Y21wOwoKaW50IG1haW4oKQp7CgogICAgIC8vaW9zX2Jhc2U6OiBzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyBjaW4udGllKDApOwogICAgIC8vZnJlb3BlbigiaW5wdXQuaW4iLCJyIixzdGRpbik7CiAgICAgICAgCiAgICAgICAgaW50IG4scTtjaW4+Pm4+PnE7CiAgICAgICAgaW50IGZ1az0wOwogICAgICAgIHo9c3FydChuKTsKICAgICAgICAKICAgICAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICAgICAgICAgICAgc2NhbmYoIiVkIiwmdltpXSksY3BbaV09dltpXTsKCiAgICAgICAgc29ydChjcCxjcCtuKTsKICAgICAgICBmb3IoaW50IGk9MDtpPG47aSsrKWlmKGNtcC5maW5kKGNwW2ldKT09Y21wLmVuZCgpKWNtcFtjcFtpXV09aSsxOwogICAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspdltpXT1jbXBbdltpXV07CiAgICAgICAgICAgIAoKICAgICAgICAgICAgICAgICAgICAKICAgICAgICBmb3IoaW50IGk9MDtpPHE7aSsrKQogICAgICAgIHsKICAgICAgICAgICAgIGludCBsLHI7CiAgICAgICAgICAgICBzY2FuZigiJWQgJWQiLCZsLCZyKTsKICAgICAgICAgICAgIG5vZGUgdG1wOwogICAgICAgICAgICAgdG1wLmw9bC0xOwogICAgICAgICAgICAgdG1wLnI9ci0xOwogICAgICAgICAgICAgdG1wLmlkPWk7CiAgICAgICAgICAgICB1W2ldPXRtcDsKICAgICAgICB9CgogICAgICAgICBzb3J0KHUsdStxLGNvb2wpOwoKICAgICAgICAgaW50IHN0PTAsZT0wO2Z1az0wO2NudFt2WzBdXSsrOwoKICAgICAgICBmb3IoaW50IGk9MDtpPHE7aSsrKQogICAgICAgIHsKICAgICAgICAgICAgaW50IGw9dVtpXS5sOwogICAgICAgICAgICBpbnQgcj11W2ldLnI7CiAgICAgICAgICAgLy8gY291dDw8bDw8IiAiPDxyPDwiICI8PHN0PDwiICI8PGU8PCIgIjw8ZnVrPDxlbmRsOwogICAgICAgICAgICAKICAgICAgICAgICAgd2hpbGUoc3Q8bCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY250W3Zbc3RdXS0tOwogICAgICAgICAgICAgICAgaW50IGdnPWNudFt2W3N0XV07CiAgICAgICAgICAgICAgICBpZihnZz09MSkKICAgICAgICAgICAgICAgICB7ZnVrLS07fQogICAgICAgICAgICAgICAgZWxzZSBpZihnZz09Mil7ZnVrKys7fQogICAgICAgICAgICAgICAgIHN0Kys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUoc3Q+bCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgIHN0LS07CiAgICAgICAgICAgICAgICAgY250W3Zbc3RdXSsrOwogICAgICAgICAgICAgICAgIGludCBnZz1jbnRbdltzdF1dOwogICAgICAgICAgICAgICAgIGlmKGdnPT0yKQogICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBmdWsrKzsKICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgZWxzZSBpZihnZz09MykKICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgZnVrLS07CiAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHdoaWxlKGU8cikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZSsrOwogICAgICAgICAgICAgICAgY250W3ZbZV1dKys7CiAgICAgICAgICAgICAgICBpbnQgZ2c9Y250W3ZbZV1dOwogICAgICAgICAgICAgICAgaWYoZ2c9PTIpCiAgICAgICAgICAgICAgICB7ZnVrKys7fQogICAgICAgICAgICAgICBlbHNlIGlmKGdnPT0zKXtmdWstLTt9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUoZT5yKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgIGNudFt2W2VdXS0tOwogICAgICAgICAgICAgICBpbnQgZ2c9Y250W3ZbZV1dOwogICAgICAgICAgICAgICBpZihnZz09MSkKICAgICAgICAgICAgICAge2Z1ay0tO30KICAgICAgICAgICAgICAgZWxzZSBpZihnZz09Mil7ZnVrKys7fQogICAgICAgICAgICAgICBlLS07CiAgICAgICAgICAgIH0gICAgICAgICAgICAKICAgICAgICAgICAgYW5zW3VbaV0uaWRdPWZ1azsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgZm9yKGludCBpPTA7aTxxO2krKykKICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixhbnNbaV0pOwoKICAgICByZXR1cm4gMDsKfQoK