/*
███████ ██████ ███ ██ ██ ██████ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██ ██ ██ ██ ███████ ███████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██████ ██ ████ ██ ██████ ██ ██ ██
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define sonic ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define gcd __gcd
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define acc(x) accumulate(all(x),0LL)
#define fixed(n) fixed << setprecision(n)
#define ll long long
#define digits(n) ((int)log10(n) + 1)
#define ull unsigned long long
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define mx(v) *max_element(all(v))
#define mn(v) *min_element(all(v))
#define mxi(v) max_element(all(v))-v.begin()
#define mni(v) min_element(all(v))-v.begin()
#define tolow(pp) transform(all(pp),pp.begin(),::tolower)
#define toup(pp) transform(all(pp),pp.begin(),::toupper)
#define cin(vec) for(auto &i : vec) cin >> i
#define cout(vec) for(auto& i : vec) cout << i << " "; cout << '\n';
bool isPowerOfTwo(ll n) {if (n==0)return 0;return !(n&(n-1));}
ll fact(ll n){ll temp = 1;for (ll i = 2; i <= n; i++)temp = temp * i;return temp;}
ll nCr(ll n, ll r){return fact(n) / (fact(r) * fact(n - r));}
ll nPr(ll n,ll r){return fact(n)/ fact(n-r);}
ll lcm(ll a,ll b){return (a/gcd(a,b))*b;}
ll sumFrom1ToN(int n) { return (ll)n * (n + 1) / 2; }
ll sumFromAtoB(ll a, ll b) { return (max(a, b) - min(a, b) + 1) * (a + b) / 2; }
bool comparePairs(const pair<int, int>& a, const pair<int, int>& b) {return a.first < b.first;}
#define ordered_set tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update>
typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
#define ll long long
void myerase(ordered_set &t, int v){
int rank = t.order_of_key(v);//Number of elements that are less than v in t
ordered_set::iterator it = t.find_by_order(rank); //Iterator that points to the (rank+1)th element in t
t.erase(it);
}
/*
██████╗ ██████╗ ██████╗ ███████╗ ███████╗████████╗ █████╗ ██████╗ ████████╗ ██╗ ██╗███████╗██████╗ ███████╗
██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔════╝╚══██╔══╝██╔══██╗██╔══██╗╚══██╔══╝ ██║ ██║██╔════╝██╔══██╗██╔════╝
██║ ██║ ██║██║ ██║█████╗ ███████╗ ██║ ███████║██████╔╝ ██║ ███████║█████╗ ██████╔╝█████╗
██║ ██║ ██║██║ ██║██╔══╝ ╚════██║ ██║ ██╔══██║██╔══██╗ ██║ ██╔══██║██╔══╝ ██╔══██╗██╔══╝
╚██████╗╚██████╔╝██████╔╝███████╗ ███████║ ██║ ██║ ██║██║ ██║ ██║ ██║ ██║███████╗██║ ██║███████╗
╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝
*/
const int N=200005;
const int Q=200005;
const int SQ=449;
void compress(vector<ll>&a,int start)
{
int n=a.size();
vector<pair<ll,ll>>pairs(n);
for(int i=0;i<n;i++)
{
pairs[i]={a[i],i};
}
sort(pairs.begin(),pairs.end());
int nxt=start;
for(int i=0;i<n;i++)
{
if(i>0&&pairs[i-1].first!=pairs[i].first)
{
nxt++;
}
a[pairs[i].second]=nxt;
}
}
struct query{
int l,r,q_idx,block_idx;
query(){}
query(int _l,int _r,int _q_idx)
{
l=_l-1,r=_r-1,q_idx=_q_idx,block_idx=_l/SQ;
}
bool operator<(const query&y)const{
if(block_idx!=y.block_idx)
{
return block_idx<y.block_idx;
}
return r<y.r;
}
};
ll n,q,s[N],res=0,q_ans[Q],f[N],ff[N];int mx=0;
vector<ll>arr(N);
query queries[Q];
void add(int i)
{
f[arr[i]]++;
if(f[arr[i]]==1)res++;
}
void remove(int i)
{
if(f[arr[i]]==1)res--;
f[arr[i]]--;
}
void MO_process()
{
sort(queries,queries+q);
int l=1,r=0;
for(int i=0;i<q;i++)
{
while(r<queries[i].r)add(++r);
while(r>queries[i].r)remove(r--);
while(l<queries[i].l)remove(l++);
while(l>queries[i].l)add(--l);
q_ans[queries[i].q_idx]=res;
}
}
void sonic444() {
//The stupidest code you will ever see
//The stupidest code you will ever see
cin>>n>>q;
arr.resize(n);
cin(arr);
compress(arr,0);
for (int i = 0; i < q; ++i) {
ll l,r;cin>>l>>r;
queries[i]=query(l,r,i);
}
MO_process();
for (int i = 0; i < q; ++i) {
cout<<q_ans[i]<<endl;
}
}
int main() {
// freopen("input1.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
sonic
//////////////////////////////////////////////
int t=1;//cin>>t;
while (t--){
sonic444();
}
return 0;
}