#include <iostream>
#include <cstdio>
#include <algorithm>
#define N_MAX 5000000
using namespace std;

struct node
{
  int l, r, id;
} q[N_MAX + 5];

// c:binary indexed tree, f:last ingdex(j) of a[j]
int N, a[N_MAX + 5], m, c[N_MAX + 5], f[N_MAX + 5], ans[N_MAX + 5];

int lowbit(int x) {return x & (-x);}
int cmp(node x, node y) {return x.r < y.r;}
void add(int x, int k){
  for (; x <= N; x += lowbit(x))
    c[x] += k;
}
int sum(int x)
{
  int ans = 0;
  for (; x; x -= lowbit(x))
    ans += c[x];
  return ans;
}

int main()
{
  scanf("%d%d", &N, &m);
  for (int i = 1; i <= N; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;
  sort(q + 1, q + m + 1, cmp);
  for (int i = 1; i <= m; i++)
  {
    for (int j = q[i - 1].r + 1; j <= q[i].r; j++)
    {
      if (f[a[j]])
        add(f[a[j]], -1);
      f[a[j]] = j;
      add(j, 1);
    }
    ans[q[i].id] = sum(q[i].r) - sum(q[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);
  return 0;
}