#define CPP17
#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#ifdef CPP17
#include <variant>
#endif

#define endl codeforces

#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)

using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
using size_type = ssize_t;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

template <typename T, std::size_t Head, std::size_t... Tail> 
struct multi_dim_array { using type = std::array<typename multi_dim_array<T, Tail...>::type, Head>; };

template <typename T, std::size_t Head> 
struct multi_dim_array<T, Head> { using type = std::array<T, Head>; };

template <typename T, std::size_t... Args> using mdarray = typename multi_dim_array<T, Args...>::type;

#ifdef CPP17
template <typename T, typename F, typename... Args> 
void fill_seq(T &t, F f, Args... args) { 
    if constexpr (std::is_invocable<F, Args...>::value) { 
        t = f(args...); 
    } else { 
        for (size_type i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); 
    } 
}
#endif

template <typename T> vec<T> make_v(size_type sz) { return vec<T>(sz); }

template <typename T, typename... Tail> 
auto make_v(size_type hs, Tail&&... ts) { 
    auto v = std::move(make_v<T>(std::forward<Tail>(ts)...)); 
    return vec<decltype(v)>(hs, v); 
}

namespace init__ { 
struct InitIO { 
    InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } 
} init_io; 
}
#define CPP17


namespace utility {

template <typename T>
using validate_integer = typename std::enable_if<std::is_integral<T>::value, ll>::type;

template <typename T>
auto popcount(T n) -> validate_integer<T> {
    return __builtin_popcountll(n);
}

// 0 indexed
template <typename T>
auto msb(T n) -> validate_integer<T> {
    return 64 - __builtin_clzll(n) - 1;
}

template <typename T>
constexpr auto ceil_pow2(T s) -> validate_integer<T> {
    ll ret = 1;
    while (ret < s) ret *= 2;
    return ret;
}

}

namespace utility {

constexpr ll ceil_div(ll a, ll b) {
    return a / b + !!(a % b);
}

}

namespace succinct { 

template <typename T>
constexpr T ceil_log2(T n) {
    for (int i = 0; i < 63; i++) {
        T mask = 1ll << i;
        if (n <= mask) return i;
    }
    return -1;
}

template <typename T>
constexpr T ceil_even(T n) {
    return n + (n & 1);
}

template <std::size_t MaxSize>
struct FullyIndexableDictionary {
    using size_type = ssize_t;

private:
    constexpr static size_type sz_log2 = ceil_even(ceil_log2<size_type>(MaxSize));
    constexpr static size_type chunk_sz = sz_log2 * sz_log2;
    constexpr static size_type block_sz = sz_log2 / 2;
    constexpr static size_type block_per_chunk = chunk_sz / block_sz;

    size_type sz, cnt1;
    vec<size_type> chunk, block, dat;

public:
    FullyIndexableDictionary() { } 

    template <typename F>
    FullyIndexableDictionary(F f, size_type sz) 
        : sz(sz), cnt1(0),
          chunk(utility::ceil_div(sz, chunk_sz)), 
          block(chunk.size() * block_per_chunk),
          dat(block.size())
    {
        size_type idx = 0;
        for (size_type i = 0; i < chunk.size(); i++) {
            chunk[i] = cnt1;
            size_type tmp = 0;
            for (size_type j = 0; j < block_per_chunk; j++, idx++) {
                block[idx] = tmp;
                for (size_type k = 0; k < block_sz; k++) {
                    auto v = f(idx * block_sz + k);
                    size_type mask = (1ll << k) * v;
                    dat[idx] |= mask;
                    tmp += v;
                }
            }
            cnt1 += tmp;
        }
    }

    size_type rank(bool b, size_type pos) const {
        if (!b) return pos - rank(!b, pos);
        if (pos == sz) return sum(b);
        size_type ret = 0;
        
        ret += chunk[pos / chunk_sz];
        ret += block[pos / block_sz];

        auto mask = (1ll << (pos % block_sz)) - 1;
        return ret + utility::popcount(dat[pos / block_sz] & mask);
    }

    size_type rank(bool b, size_type l, size_type r) const {
        return rank(b, r) - rank(b, l);
    }

    size_type select(bool b, size_type n) const {
        if (rank(b, sz) < n) return -1;

        size_type ok = sz, ng = 0;
        while (1 < std::abs(ok - ng)) {
            size_type mid = (ok + ng) / 2;
            (n <= rank(b, mid) ? ok : ng) = mid;
        }
        return ok;
    }

    bool operator [](size_type i) const {
        size_type offset = i % block_sz;
        return (dat[i / block_sz] >> offset) & 1;
    }

    size_type sum(bool b) const {
        return b ? cnt1 : sz - cnt1;
    }
};

}

namespace succinct { 

template <typename T, std::size_t MaxSize, std::size_t MaxValueLog = 32>
class WaveletMatrix {
    using data_type = FullyIndexableDictionary<MaxSize>;

public:
    using size_type = typename data_type::size_type;

private:
    size_type sz;
    std::array<data_type, MaxValueLog> mat;
    std::array<vec<size_type>, MaxValueLog> acc_sum;

    void update_range_aux(bool bt, size_type &l, size_type &r, size_type i) const {
        const auto &dic = mat[i];
        l = dic.rank(bt, l) + bt * dic.sum(0);
        r = dic.rank(bt, r) + bt * dic.sum(0);
    }

    size_type range_freq_aux(size_type l, size_type r, T bound) const {
        if ((1ll << MaxValueLog) - 1 <= bound) return r - l;
        size_type ret = 0;
        for (size_type i = 0; i < MaxValueLog && l < r; i++) {
            T mask = 1ll << (MaxValueLog - (i + 1));
            bool bt = !!(bound & mask);
            if (bt) ret += mat[i].rank(0, l, r);
            update_range_aux(bt, l, r, i);
        }
        return ret;
    }

    size_type range_sum_aux(size_type l, size_type r, T bound) const {
        if ((1ll << MaxValueLog) - 1 <= bound) return acc_sum[0][r] - acc_sum[0][l];
        T ret = 0;
        for (size_type i = 0; i < MaxValueLog && l < r; i++) {
            T mask = 1ll << (MaxValueLog - (i + 1));
            bool bt = !!(bound & mask);
            if (bt) ret += acc_sum[i][r] - acc_sum[i][l];
            update_range_aux(bt, l, r, i);
        }
        return ret;
    }

public:
    WaveletMatrix(vec<T> dat) : sz(dat.size()) {
        vec<T> buf(sz);
        for (size_type i = 0; i < MaxValueLog; i++) {
            size_type mask = 1ll << (MaxValueLog - i - 1);
            auto f = [&](size_type idx) -> bool {
                if (dat.size() <= idx) return false;
                return !!(dat[idx] & mask);
            };
            mat[i] = std::move(data_type(f, dat.size()));
            size_type l = 0, r = 0;
            acc_sum[i].resize(sz + 1, 0);
            for (size_type j = 0; j < dat.size(); j++) {
                auto e = dat[j];
                if (e & mask) {
                    r++;
                    buf[sz - r] = e;
                } else {
                    buf[l] = e;
                    acc_sum[i][j + 1] += e;
                    l++;
                }
            }
            for (size_type k = 0; k < dat.size(); k++) acc_sum[i][k + 1] += acc_sum[i][k];
            std::reverse(buf.begin() + l, buf.end());
            std::swap(buf, dat);
        }
    }

    size_type rank(T t, size_type pos) const {
        size_type mask = 1ll << (MaxValueLog - 1);
        size_type l = 0, r = pos;
        for (size_type i = 0; i < MaxValueLog; i++, mask /= 2) {
            bool bt = !!(t & mask);
            update_range_aux(bt, l, r, i);
        }
        return r - l;
    }

    size_type select(T t, size_type n) const {
        std::array<size_type, MaxValueLog> larr, rarr;
        size_type l = 0, r = sz;
        for (size_type i = 0, mask = (1ll << (MaxValueLog - 1)); i < MaxValueLog; i++, mask /= 2) {
            larr[i] = l, rarr[i] = r;
            bool bt = !!(t & mask);
            update_range_aux(bt, l, r, i);
        }

        for (size_type i = 0, mask = 1; i < MaxValueLog; i++, mask *= 2) {
            size_type idx = MaxValueLog - (i + 1);
            const auto &dic = mat[idx];
            bool bt = !!(t & mask);
            n += dic.rank(bt, larr[idx]);
            size_type ra = dic.sel(bt, n);
            if (ra == -1) return -1;
            n = ra;
        }
        return n;
    }

    // k >= 0
    T quantile(size_type l, size_type r, size_type k) const {
        T ret = 0;
        for (size_type i = 0; i < MaxValueLog && l < r; i++) {
            T mask = 1ll << (MaxValueLog - (i + 1));
            const auto &dic = mat[i];
            size_type cl = dic.rank(1, l), cr = dic.rank(1, r);
            if (k < cr - cl) {
                l = cl + dic.sum(0);
                r = cr + dic.sum(0);
                ret |= mask;
            } else {
                k -= cr - cl;
                l -= cl;
                r -= cr;
            }
        }
        return ret;
    }

    // [l, r), [min, max)
    size_type range_freq(size_type l, size_type r, T min, T max) const {
        return range_freq_aux(l, r, max) - range_freq_aux(l, r, min);
    }

    size_type range_sum(size_type l, size_type r, T min, T max) const {
        return range_sum_aux(l, r, max) - range_sum_aux(l, r, min);
    }
};

}

int main() {
    constexpr size_type SIZE = 1e6 + 10;
    ll n, q;
    std::cin >> n >> q;
    vec<ll> cv(n);
    for (ll &e : cv) {
        std::cin >> e;
        e--;
    }

    auto mat = [&] {
        vec<ll> rv(n + 1, n + 1), idx_v(n + 1, n + 1);
        for (ll i = n - 1; 0 <= i; i--) {
            ll c = cv[i];
            idx_v[i] = rv[c];
            rv[c] = i;
        }
        return succinct::WaveletMatrix<ll, SIZE>(idx_v);
    }();

    while (q--) {
        ll l, r;
        std::cin >> l >> r;
        l--;
        ll res = mat.range_freq(l, r, l, r);
        std::cout << (r - l) - res << "\n";
    }
    return 0;
}


Measured memory_bytes: 173108