#include <bits/stdc++.h>
#define task   "brianthecrab"
#define int    long long
#define pii    pair <int, int>
#define fi     first
#define se     second
#define szf    sizeof
#define sz(s)  (int)((s).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int N = 5e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
const int block = 700;
struct query {
    int l, r, id;
};
 
bool cmp (query A, query B) {
    if (A.l / block == B.l / block) {
        return A.r < B.r;
    }
    return A.l / block < B.l / block;
}
int n, cur = 0;
int a[N], res[N], cnt[N];
vector <query> que;
void add (int i) {
    cnt[a[i]] ++;
    if (cnt[a[i]] == 2) {
        cur ++;
    }
    else if (cnt[a[i]] == 3) {
        cur --;
    }
}
void del (int i) {
    cnt[a[i]] --;
    if (cnt[a[i]] == 2) {
        cur ++;
    }
    else if (cnt[a[i]] == 1) {
        cur --;
    }
}
signed main () {
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int q;
    cin >> n >> q;
    vector <int> comp;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        comp.push_back (a[i]);
    }
    sort (all (comp));
    comp.erase (unique (all (comp)), comp.end ()); 
    for (int i = 1; i <= q; i ++) {
        int l, r;
        cin >> l >> r;
        que.push_back ({l, r, i});
    }
    sort (que.begin (), que.end (), cmp);
    int tL = 0, tR = 0;
    for (auto [l, r, id] : que) {
        while (tR < r) {
            add (++ tR);
        }
        while (tL > l) {
            add (-- tL);
        }
        while (tR > r) {
            del (tR --);
        }
        while (tL < l) {
            del (tL ++);
        }
        res[id] = cur;
    }
    for (int i = 1; i <= q; i ++) {
        cout << res[i] << '\n';
    }
    return 0;
}
// hmvncvdqdela
컴파일 시 표준 에러 (stderr) 메시지
poklon.cpp: In function 'int main()':
poklon.cpp:63:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
poklon.cpp:64:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |