Problem:
Score : 600 points

Problem Statement

We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.

You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.

Sample Input 1

4 3
1 2 1 3
1 3
2 4
3 3

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8

Sample Output 2

1
2
2
1
2
2
6
3
3
3

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(T value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                              \
  do {                                        \
    cerr << #__VA_ARGS__ << ": ";             \
    debug(__VA_ARGS__);                       \
    cerr << " (L" << __LINE__ << ")" << endl; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

using namespace std;

using P = pair<int, int>;

struct Q {
  P lr;
  int bucket;
  int i;
  int kinds;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  in(int, n, q);
  V<int> c(n);
  rep(i, n) cin >> c[i];

  auto bucket = [](int i) { return int(sqrt(i)); };

  V<Q> qs(q);
  rep(i, q) {
    cin >> qs[i].lr.first >> qs[i].lr.second;
    --qs[i].lr.first;
    --qs[i].lr.second;
    qs[i].i = i;
    qs[i].bucket = bucket(qs[i].lr.first);
  }
  sort(all(qs), [](const Q& l, const Q& r) {
    if (l.bucket != r.bucket) {
      return l.bucket < r.bucket;
    }
    return l.lr.second < r.lr.second;
  });

  vector<int> kinds(n + 1);
  int n_kinds = 0;
  auto add = [&](int i) {
    if (kinds[c[i]] == 0) {
      ++n_kinds;
    }
    ++kinds[c[i]];
  };
  auto del = [&](int i) {
    --kinds[c[i]];
    if (kinds[c[i]] == 0) {
      --n_kinds;
    }
  };
  rep(i, q) {
    auto [L, R] = qs[i].lr;
    if (i == 0 || qs[i].bucket != qs[i - 1].bucket) {
      n_kinds = 0;
      rep(i, n) kinds[i + 1] = 0;
      for (int j = L; j <= R; ++j) {
        add(j);
      }
    } else {
      auto [PL, PR] = qs[i - 1].lr;
      if (PL < L) {
        for (int j = PL; j < L; ++j) {
          del(j);
        }
      } else {
        for (int j = L; j < PL; ++j) {
          add(j);
        }
      }
      if (PR < R) {
        for (int j = R; j > PR; --j) {
          add(j);
        }
      } else {
        for (int j = PR; j > R; --j) {
          del(j);
        }
      }
    }
    qs[i].kinds = n_kinds;
  }
  sort(all(qs), [](const Q& l, const Q& r) { return l.i < r.i; });
  rep(i, q) { out(qs[i].kinds); }
}