#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#define int long long
#define pii pair<int, int>
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define ALL(x) x.begin(), x.end()
using namespace std;
struct Node {
Node* lc = nullptr;
Node* rc = nullptr;
int l, r, sum = 0;
Node() {
}
Node(int l, int r) : l(l), r(r) {
}
void pull() {
sum = lc->sum + rc->sum;
}
};
Node pool[500000000 / sizeof(Node)];
int cnt = 0;
struct DS {
DS(const vector<int>& v) {
n = v.size();
roots = vector<Node*>(n + 1, nullptr);
int maxv = *max_element(ALL(v));
last = vector<int>(maxv + 1, -1);
roots[0] = build(0, n - 1);
for (int i = 0; i < n; i++) {
if (last[v[i]] != -1) {
roots[i + 1] = update(roots[i], last[v[i]], 0);
roots[i + 1] = update(roots[i + 1], i, 1);
} else {
roots[i + 1] = update(roots[i], i, 1);
}
last[v[i]] = i;
}
}
int query(int l, int r) {
return query_sum(roots[r], l - 1, r - 1);
}
private:
int n;
vector<Node*> roots;
vector<int> last;
// 單點改值 區間查詢
Node* build(int l, int r) {
Node* root = new (&pool[cnt++]) Node(l, r);
if (l == r) {
return root;
}
int mid = (l + r) / 2;
root->lc = build(l, mid);
root->rc = build(mid + 1, r);
root->pull();
return root;
}
Node* update(const Node* root, int pos, int val) {
Node* now = new (&pool[cnt++]) Node(*root);
if (now->l == now->r) {
now->sum = val;
return now;
}
if (pos <= now->lc->r) {
now->lc = update(now->lc, pos, val);
} else {
now->rc = update(now->rc, pos, val);
}
now->pull();
return now;
}
int query_sum(const Node* root, int qL, int qR) {
if (root->r < qL || qR < root->l) return 0;
if (qL <= root->l && root->r <= qR) {
return root->sum;
}
return query_sum(root->lc, qL, qR) + query_sum(root->rc, qL, qR);
}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b = a;
sort(ALL(b));
for (int i = 0; i < n; i++) {
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
}
DS ds(a);
while (q--) {
int l, r;
cin >> l >> r;
cout << ds.query(l, r) << '\n';
}
}