#line 1 "F.cpp"
#include <cstdio>
#include <algorithm>
#include <array>
#include <tuple>
#include <vector>

#line 1 "~/git/library/utility/io.cpp"



/**
 * @brief 入出力
 * @author えびちゃん
 */

#include <cstddef>
#include <cctype>
#include <iomanip>
#include <iostream>
#line 14 "~/git/library/utility/io.cpp"
#include <utility>

class format {
public:
  using size_type = size_t;

private:
  char const* M_fmt;
  size_type M_nvars = 0;
  size_type M_error = -1;

  void M_print(std::ostream& os, char const* pos) const {
    while (*pos) {
      if (*pos == '{' || *pos == '}') ++pos;
      os << *pos++;
    }
  }

  template <typename Tp, typename... Args>
  void M_print(std::ostream& os, char const* pos, Tp const& x, Args const&... xs) const {
    while (true) {
      if (*pos == '{') {
        if (pos[1] == '{') {
          ++pos;
          os << '{';
        } else {
          char const* next = M_print_formatted(os, pos, x);
          return M_print(os, next, xs...);
        }
      } else {
        if (*pos == '}') ++pos;
        os << *pos;
      }
      ++pos;
    }
    char const* next = M_print_formatted(os, pos, x);
    M_print(os, next, xs...);
  }

  template <typename Tp>
  char const* M_print_formatted(std::ostream& os, char const* pos, Tp const& x) const {
    // parse flags, preferably
    while (*++pos != '}') {}
    os << x;
    return ++pos;
  }

  void M_scan(std::istream& is, char const* pos) const {
    while (true) {
      if (*pos == '{' || *pos == '}') ++pos;
      if (isspace(*pos)) {
        while (isspace(is.peek())) is.get();
      } else {
        if (is.peek() == *pos) {
          ++pos;
          is.get();
        } else {
          return;
        }
      }
    }
  }

  template <typename Tp, typename... Args>
  void M_scan(std::istream& is, char const* pos, Tp& x, Args&&... xs) const {
    while (true) {
      if (*pos == '{') {
        if (pos[1] == '{') {
          if (is.peek() == '{') {
            ++pos;
            is.get();
          } else {
            return;
          }
        } else {
          char const* next = M_scan_formatted(is, pos, x);
          return M_scan(is, next, xs...);
        }
      } else if (isspace(*pos)) {
        while (isspace(is.peek())) is.get();
        ++pos;
      } else {
        if (*pos == '}') ++pos;
        if (is.peek() == *pos) {
          ++pos;
          is.get();
        } else {
          return;
        }
      }
    }
  }

  template <typename Tp>
  char const* M_scan_formatted(std::istream& is, char const* pos, Tp& x) const {
    // parse flags, preferably
    while (*++pos != '}') {}
    is >> x;
    return ++pos;
  }

public:
  constexpr format(char const* fmt): M_fmt(fmt) {
    bool opens = 0;
    size_type i = 0;
    for (; fmt[i]; ++i) {
      if (fmt[i] == '{') {
        if (fmt[i+1] == '{') { ++i; continue; }  // escaped
        if (opens) { M_error = i; return; }
        opens = true;
      } else if (fmt[i] == '}') {
        if (fmt[i+1] == '}') { ++i; continue; }  // escaped
        if (!opens) { M_error = i; return; }
        opens = false;
        ++M_nvars;
      }
    }
    if (opens) { M_error = i; return; }
  }

  template <typename... Args>
  void print_(std::ostream& os, Args const&... xs) const {
    M_print(os, M_fmt, xs...);
  }

  template <typename... Args>
  void scan_(std::istream& is, Args&&... xs) const {
    M_scan(is, M_fmt, xs...);
  }

  constexpr size_type count() const { return M_nvars; }
  constexpr size_type error() const { return M_error; }
};

#define VA_COUNT(...)                                           \
  std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value

#define fprint(os, fmt, ...) (void)({                           \
      constexpr format fmt_(fmt);                               \
      constexpr size_t lhs = fmt_.count();                      \
      constexpr size_t rhs = VA_COUNT(__VA_ARGS__);             \
      static_assert(lhs == rhs, "size mismatch");               \
      static_assert(fmt_.error()+1 == 0, "misformatted");       \
      fmt_.print_(os, ##__VA_ARGS__);                           \
    })

#define fprintln(os, fmt, ...) (void)({         \
      fprint(os, fmt, ##__VA_ARGS__);           \
      os << '\n';;                              \
    })

#define print(...) fprint(std::cout, ##__VA_ARGS__)
#define println(...) fprintln(std::cout, ##__VA_ARGS__)
#define eprint(...) fprint(std::cerr, ##__VA_ARGS__)
#define eprintln(...) fprintln(std::cerr, ##__VA_ARGS__)

#define fscan(is, fmt, ...) (void)({                            \
      constexpr format fmt_(fmt);                               \
      constexpr size_t lhs = fmt_.count();                      \
      constexpr size_t rhs = VA_COUNT(__VA_ARGS__);             \
      static_assert(lhs == rhs, "size mismatch");               \
      static_assert(fmt_.error()+1 == 0, "misformatted");       \
      fmt_.scan_(is, ##__VA_ARGS__);                            \
    })

#define scan(...) fscan(std::cin, __VA_ARGS__)

__attribute__((constructor))
void ioinit() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cerr.tie(nullptr);
  std::cout << std::fixed << std::setprecision(16);
}


#line 1 "~/git/library/algorithm/mo.cpp"



/**
 * @brief Mo's algorithm
 * @author えびちゃん
 */

#line 10 "~/git/library/algorithm/mo.cpp"
#include <numeric>
#line 14 "~/git/library/algorithm/mo.cpp"

#line 1 "~/git/library/integer/sqrt.cpp"



/**
 * @brief 整数の平方根
 * @author えびちゃん
 */

#include <climits>

template <typename Tp>
Tp isqrt(Tp n) {
  if (n <= 1) return n;
  Tp lb = 1;
  Tp ub = static_cast<Tp>(1) << (CHAR_BIT * (sizeof(Tp) / 2));
  while (ub-lb > 1) {
    Tp mid = (lb+ub) >> 1;
    ((mid*mid <= n)? lb: ub) = mid;
  }
  return lb;
}


#line 16 "~/git/library/algorithm/mo.cpp"

template <typename Rf, typename Args>
auto mo(Rf&& rf, std::vector<std::tuple<size_t, size_t, Args>> const& qs)
  -> std::vector<decltype(rf(std::declval<Args>()))>
{
  if (qs.empty()) return {};
  using value_type = decltype(rf(std::declval<Args>()));
  size_t n = rf.size();
  size_t q = qs.size();
  std::vector<value_type> res(q);
  size_t b = n / isqrt(q);
  std::vector<size_t> is(q);
  std::iota(is.begin(), is.end(), 0);
  std::sort(is.begin(), is.end(), [&](size_t i0, size_t i1) {
    size_t l0 = std::get<0>(qs[i0]) / b;
    size_t r0 = std::get<1>(qs[i0]);
    size_t l1 = std::get<0>(qs[i1]) / b;
    size_t r1 = std::get<1>(qs[i1]);
    if (l0 != l1) return l0 < l1;
    if (r0 != r1) return r0 < r1;
    return false;
  });

  size_t l = 0;
  size_t r = 0;
  for (auto i: is) {
    auto const& [l0, r0, x] = qs[i];
    while (r < r0) rf.push_back(), ++r;
    while (l > l0) rf.push_front(), --l;
    while (l < l0) rf.pop_front(), ++l;
    while (r > r0) rf.pop_back(), --r;
    res[i] = rf(x);
    l = l0;
    r = r0;
  }
  return res;
}


#line 9 "F.cpp"

#line 14 "F.cpp"

// #include "integer/sqrt.cpp"

template <typename Rf>
auto mo(Rf&& rf, std::vector<std::pair<size_t, size_t>> const& qs)
  -> std::vector<decltype(rf())>
{
  if (qs.empty()) return {};
  using value_type = decltype(rf());
  size_t n = rf.size();
  size_t q = qs.size();
  std::vector<value_type> res(q);
  // size_t b = n / isqrt(q);
  size_t const b = 1024;
  std::vector<size_t> is(q);
  std::iota(is.begin(), is.end(), 0);
  std::sort(is.begin(), is.end(), [&](size_t i0, size_t i1) {
    size_t l0 = std::get<0>(qs[i0]) / b;
    size_t r0 = std::get<1>(qs[i0]);
    size_t l1 = std::get<0>(qs[i1]) / b;
    size_t r1 = std::get<1>(qs[i1]);
    if (l0 != l1) return l0 < l1;
    if (r0 != r1) return r0 < r1;
    return false;
  });

  size_t l = 0, r = 0;
  for (auto i: is) {
    auto const& [l0, r0] = qs[i];
    while (r < r0) rf.push_back(), ++r;
    while (l > l0) rf.push_front(), --l;
    while (l < l0) rf.pop_front(), ++l;
    while (r > r0) rf.pop_back(), --r;
    res[i] = rf();
    l = l0;
    r = r0;
  }
  return res;
}

class neko {
  std::vector<size_t> M_a;
  size_t M_l = 0, M_r = 0;
  int32_t M_res = 0;
  std::array<int32_t, 500000> M_count{{}};

public:
  neko() = default;
  neko(std::vector<size_t> const& a): M_a(a) {}

  void push_back() {
    size_t x = M_a[M_r++];
    if (M_count[x]++ == 0) ++M_res;
  }
  void push_front() {
    size_t x = M_a[--M_l];
    if (M_count[x]++ == 0) ++M_res;
  }
  void pop_back() {
    size_t x = M_a[--M_r];
    if (--M_count[x] == 0) --M_res;
  }
  void pop_front() {
    size_t x = M_a[M_l++];
    if (--M_count[x] == 0) --M_res;
  }
  size_t size() const noexcept { return M_a.size(); }
  int32_t operator ()() const noexcept { return M_res; }
};

int main() {
  size_t n, q;
  scan("{} {}", n, q);

  std::vector<size_t> c(n);
  for (auto& ci: c) scan("{}", ci), --ci;

  std::vector<std::pair<size_t, size_t>> qs(q);
  for (auto& [l, r]: qs) scan("{} {}", l, r), --l;

  neko nya(c);
  for (auto res: mo(nya, qs)) println("{}", res);
}


Measured memory_bytes: 26612