#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  using T = tuple<int, int, int>;
  vector<T> b(q);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    b[i] = {l - 1, r - 1, i};
  }
  sort(b.begin(), b.end(), [](const T &x, const T &y) {
    int xl, xr, xi;
    int yl, yr, yi;
    tie(xl, xr, xi) = x;
    tie(yl, yr, yi) = y;
    xl /= 708;
    yl /= 708;
    if (xl != yl) return xl < yl;
    else return xr < yr;
  });

  int l = 0, r = 0;

  vector<int> cnt(n + 1);
  int ta = 0;
  auto add = [&](int i) {
    if (++cnt[a[i]] == 1) {
      ++ta;
    }
  };
  auto del = [&](int i) {
    if (--cnt[a[i]] == 0) {
      --ta;
    }
  };

  add(0);

  vector<int> ans(q);
  for (auto &x : b) {
    int ql, qr, qi;
    tie(ql, qr, qi) = x;
    while (ql < l) add(--l);
    while (qr > r) add(++r);
    while (ql > l) del(l++);
    while (qr < r) del(r--);
    ans[qi] = ta;
  }

  for (int x : ans) {
    cout << x << endl;
  }
  return 0;
}

