#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}

#ifdef DEBUG
void debug() { cerr << "\n"; }
template <class T> void debug(const T &x) { cerr << x << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
    for(const T &x : v) {
        cerr << x << " ";
    }
    cerr << "\n";
}
#else
template <class T, class... Args>
void debug(const T &x, const Args &... args) {}
template <class T> void debugVector(const vector<T> &v) {}
#endif

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

struct BIT // 0-indexed
{
    int n;
    vector<int> bit;
    // 初期化
    void init(int n_input) {
        n = n_input + 1;
        bit.resize(n + 1, 0);
    }
    // v[0] + ... + v[k-1]
    int sum(int k) {
        int res = 0;
        for(int i = k - 1; i >= 0; i = (i & (i + 1)) - 1) {
            res += bit[i];
        }
        return res;
    }
    // v[i] + ... + v[j]
    int sum_between(int i, int j) { return i <= j ? sum(j + 1) - sum(i) : 0; }
    // v[k] += x
    void add(int k, int x) {
        for(int i = k; i < n; i |= i + 1) {
            bit[i] += x;
        }
    }
};

struct Data {
    int l, r, id;
    Data() {}
    Data(int _l, int _r, int _id) : l(_l), r(_r), id(_id) {}
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<Data> query(q);
    for(int i = 0; i < q; i++) {
        cin >> query[i].l >> query[i].r;
        query[i].id = i;
        query[i].l--;
        query[i].r--;
    }
    sort(ALL(query), [](const Data &x, const Data &y) { return x.r < y.r; });
    vector<int> last_id(n + 1, -1);
    for(int i = 0; i <= query[0].r; i++) {
        debug(1);
        last_id[a[i]] = i;
    }
    BIT bit;
    bit.init(n);
    for(int i = 1; i <= n; i++) {
        debug(2);
        if(last_id[i] != -1) {
            bit.add(last_id[i], 1);
        }
    }
    vector<int> ans(q);
    ans[query[0].id] = bit.sum_between(query[0].l, query[0].r);
    for(int i = 1; i < q; i++) {
        debug(3);
        if(query[i - 1].r < query[i].r) {
            for(int j = query[i - 1].r + 1; j <= query[i].r; j++) {
                if(last_id[a[j]] == -1) {
                    last_id[a[j]] = j;
                    bit.add(j, 1);
                    continue;
                }
                debug(4);
                bit.add(last_id[a[j]], -1);
                last_id[a[j]] = j;
                bit.add(last_id[a[j]], 1);
            }
        }
        ans[query[i].id] = bit.sum_between(query[i].l, query[i].r);
    }

    for(int i = 0; i < q; i++) {
        cout << ans[i] << "\n";
    }
}

Measured memory_bytes: 17400