#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ub              upper_bound
#define lb              lower_bound
#define isrt            insert
#define clr             clear
#define rsz             resize
#define ff              first
#define ss              second
#define lli             long long int
#define pb              push_back
#define pf              push_front
#define mkp              make_pair
#define pii             pair<lli,lli>
#define vi              vector<int>
#define mii             map<lli,lli>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define INF             1e9
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define all(v)          v.begin(),v.end()
#define endl            "\n"


typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

lli maxm(lli a, lli b)
{
    return (a >= b ? a : b);
}


lli minm(lli a, lli b)
{
    return (a <= b ? a : b);
}

lli power(lli x, lli y, lli p)
{
    lli res = 1;

    x = x % p;
    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;


        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

lli nCrmodp(lli n, lli r, lli p)
{
    if (r == 0)
        return 1;
    if (r == 1)
        return n;


    lli pro = 1, k = n;
    while (k > r)
    {
        pro *= k;
        pro %= p;
        --k;
    }

    k = 2;
    while (k <= (n - r))
    {
        pro *= power(k, (p - 2), p);
        pro %= p;
        ++k;
    }

    return pro;
}


string num_str(lli n, lli k)
{
    string str = "";
    lli x = n;
    while (x != 0)
    {
        string s = to_string(x % k);
        str = s + str;
        x /= k;
    }

    return str;
}


lli first_big(lli n, lli k, lli arr[])
{
    lli l = 0;
    lli r = n - 1;

    lli leftGreater = n;

    while (l <= r) {
        lli m = l + (r - l) / 2;

        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }

        else
            l = m + 1;
    }

    return leftGreater;
}

void c_p_c()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

#define MAXN 500100
vector<int> arr;
vector<int> cnt;
vector<int> brr;
vector<int> adj[2000010];


void build(int node, int l, int r) {
    if (l == r) {
        adj[node].clear();
        adj[node].push_back(brr[l]);
        return;
    }
    int mid = (l + r) >> 1,
        left = node << 1, right = left | 1;

    adj[node].clear();
    build(left, l, mid);
    build(right, mid + 1, r);


    merge(adj[left].begin(), adj[left].end(), adj[right].begin(), adj[right].end(),
          back_inserter(adj[node]));
}


int query(int node, int l, int r, int i, int j, int k) {

    if (i > r || l > j) return 0;
    if (i <= l && r <= j) {
        return (adj[node].end() - upper_bound(adj[node].begin(), adj[node].end(), k));
    }
    int mid = (l + r) >> 1,
        left = node << 1, right = left | 1;
    return query(left, l, mid, i, j, k) +
           query(right, mid + 1, r, i, j, k);
}

int main()
{
    c_p_c();

    int n, q;

    cin >> n >> q;

    cnt.rsz(n + 1, INF);
    arr.rsz(n);
    brr.rsz(n);

    for (int i = 0; i < n; ++i)
        cin >> arr[i];


    for (int j = (n - 1); j >= 0; --j)
    {
        brr[j] = cnt[arr[j]];
        cnt[arr[j]] = j;
    }
    build(1, 0, (n - 1));

    int l, r;

    for (int i = 0; i < q; ++i)
    {
        cin >> l >> r;
        --l;
        --r;
        cout << query(1, 0, (n - 1), l, r, r) << endl;
    }

    return 0;
}

Measured memory_bytes: 121404