#include #include #include #include using namespace std; #define A 500 int n, q; int a[505050]; unordered_map z; int c; int h[505050]; int u = 0; vector,pair>> f; int g[505050]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; if (z[a[i]] == 0) z[a[i]] = ++c; } for (int i = 1; i <= n; i++) a[i] = z[a[i]]; h[a[1]] = 1; for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; f.push_back(make_pair(make_pair(l/A,r),make_pair(l,i))); } int x = 1, y = 1; sort(f.begin(),f.end()); for (int i = 0; i < f.size(); i++) { int l = f[i].second.first; int r = f[i].first.second; int k = f[i].second.second; while (x > l) { x--; if (h[a[x]] == 2) u--; h[a[x]]++; if (h[a[x]] == 2) u++; } while (y < r) { y++; if (h[a[y]] == 2) u--; h[a[y]]++; if (h[a[y]] == 2) u++; } while (x < l) { if (h[a[x]] == 2) u--; h[a[x]]--; if (h[a[x]] == 2) u++; x++; } while (y > r) { if (h[a[y]] == 2) u--; h[a[y]]--; if (h[a[y]] == 2) u++; y--; } g[k] = u; } for (int i = 1; i <= q; i++) cout << g[i] << "\n"; }