#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
struct Query {
int L, R, i;
Query() {}
Query(int left, int right, int idx): L(left), R(right), i(idx) {}
};
const int maxn = 2e5 + 5;
int n, q, x, y, A[maxn], B[maxn], fenwick[maxn];
vector<Query> queries;
vector<int> ans;
map<int, int> seen;
bool cmp(Query &q1, Query &q2) {
return q1.L < q2.L;
}
void upd(int i, int v) { // fenwick tree is 1-indexed
if(i == 0) return;
int c = i;
while(c <= n) {
fenwick[c] += v;
c += (c & -c);
}
}
int qr(int i) { // fenwick tree is 1-indexed
if(i == 0) return 0;
int c = i, ans = 0;
while(c > 0) {
ans += fenwick[c];
c -= (c & -c);
}
return ans;
}
void printFenwick() {
for(int i = 1; i <= n; i++) {
cout << qr(i) - qr(i - 1) << " ";
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++) {
cin >> A[i];
}
for(int i = n; i > 0; i--) {
if(seen.count(A[i]) != 0) B[i] = seen[A[i]];
seen[A[i]] = i;
}
seen.clear();
for(int i = 1; i <= n; i++) {
if(seen.count(A[i]) == 0) upd(i, 1);
seen[A[i]] = 1;
}
queries.resize(q), ans.resize(q);
for(int i = 0; i < q; i++) {
cin >> x >> y;
queries[i] = {x, y, i};
}
sort(queries.begin(), queries.end(), cmp);
for(int i = 0, beforeL = 1; i < q; i++) {
int curL = queries[i].L, curR = queries[i].R;
while(beforeL < curL) {
upd(B[beforeL], 1);
beforeL++;
}
ans[queries[i].i] = qr(curR) - qr(curL - 1);
}
for(auto &qr_ans : ans) cout << qr_ans << '\n';
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHZlY3Rvcj4KI2luY2x1ZGU8YWxnb3JpdGhtPgojaW5jbHVkZTxjbWF0aD4KI2luY2x1ZGU8bWFwPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBRdWVyeSB7CglpbnQgTC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