/*
 * Author: .-O_O-.
 */
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pb push_back
#define vi vector<int>
#define rep0(a,b) for(int a=0;a<b;a++)
#define rep1(a,b) for(int a=1;a<=b;a++)
#define repp(a,b,c) for(int a=b;a<c;a++)
#define in insert
#define len length()
#define si size()
#define lb lower_bound
#define ub upper_bound
#define beg begin()
#define en end()
#define f first
#define s second
#define pr(a) cout<<a
#define pr2(a,b) cout<<a<<" "<<b<<"\n"
#define nx cout<<"\n"
#define int long long
// void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
                                                     
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]"<<endl;}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define deb(x...)
#endif
const ll mod = 1000000007;
const ll N = 500005;
const ll mm = 1e+18;
struct dt{
    int l,r,id;
};
int arr[N];
int freq[N];
int ans[N];
int answer=0;
vector<dt>v;
int B;
bool comp(const dt&d1,const dt&d2){
    if(d1.l/B==d2.l/B) return ((d1.l / B) & 1) ? (d1.r < d2.r) : (d1.r > d2.r);
    else return (d1.l/B<d2.l/B);  
}
void add(int idx){
    freq[arr[idx]]++;
    if(freq[arr[idx]]==1) answer++;
}
void remove(int idx){
    freq[arr[idx]]--;
    if(!freq[arr[idx]]) answer--;
}
void compute(){
    int cur_l=1;
    int cur_r=0;
    for(auto q:v){
        while(cur_l>q.l){
            cur_l--;
            add(cur_l);
        }
        while(cur_r<q.r){
            cur_r++;
            add(cur_r);
        }
        while(cur_l<q.l){
            remove(cur_l);
            cur_l++;
        }
        while(cur_r>q.r){
            remove(cur_r);
            cur_r--;
        }
        ans[q.id]=answer;
    }
}
void solve()
{
    int n,q;
    cin>>n>>q;
    B=sqrt(n);
    for(int i=0;i<n;i++) cin>>arr[i];
    rep0(i,q){
        int l1,r1;
        cin>>l1>>r1;
        dt d;
        d.l=l1-1;
        d.r=r1-1;
        d.id=i;
        v.push_back(d);
    }
    sort(v.beg,v.en,comp);
    compute();
    rep0(i,q) cout<<ans[i]<<"\n";
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
    rep1(_i,t){
        //cout<<"Case #"<<_i<<": ";
        solve();
        //nx;
    }
    return 0;
}

Measured memory_bytes: 27368