Language: C++

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<n;++i)
#define FOR(i, a, b) for(int i=a;i<b;++i)
#define RREP(i, n) for(int i=n-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=b-1;i>=a;--i)

#define all(x) begin(x),end(x)
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}


template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
    os << p.first << ":" << p.second;
    return os;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
    os << "[";
    for (auto it = mp.begin(); it != mp.end(); ++it) {
        if (it != mp.begin()) {
            os << ", ";
        }
        os << *it;
    }
    return os << "]";
}

// edit
class Mo {
    vector<int> queryL;
    vector<int> queryR;
    vector<int> idx;
    int pt = 0;
    int ql = 0, qr = 0;

public:
    // [l, r)
    inline void insert(int l, int r) {
        queryL.push_back(l);
        queryR.push_back(r);
        if (idx.empty()) idx.push_back(0);
        else idx.push_back(idx.back() + 1);
    }

    inline void build() {
        int K = (int) round(sqrt(idx.size()));
        sort(all(idx), [&](int x, int y) {
            if (queryL[x] / K == queryL[y] / K) {
                return queryR[x] < queryR[y];
            } else {
                return queryL[x] < queryL[y];
            }
        });
    }

    inline int query() {
        if (pt >= idx.size()) return -1;
        while (qr < queryR[idx[pt]]) add(qr++);
        while (ql > queryL[idx[pt]]) add(--ql);

        while (qr > queryR[idx[pt]]) del(--qr);
        while (ql < queryL[idx[pt]]) del(ql++);

        return idx[pt++];
    }

    inline void add(int &x);

    inline void del(int &x);
};

int N;
//vector<int> c;
int c[512345];

int val = 0;
//vector<int> cnt;
int cnt[512345];

inline void Mo::add(int &x) {
    if (!cnt[c[x]]) val++;
    cnt[c[x]]++;

//    if (cnt[c[x]]++ == 0) {
//        val++;
//    }
//    cnt[c[x]]++;
}

inline void Mo::del(int &x) {
    cnt[c[x]]--;
    if (!cnt[c[x]]) val--;
//    if (--cnt[c[x]] == 0) {
//        val--;
//    }
}

void fastscan(int &x) {
    bool neg = false;
    int c;
    x = 0;
    c = getchar();
    if (c == '-') {
        neg = true;
        c = getchar();
    }
    for (; (c > 47 && c < 58); c = getchar())
        x = (x << 1) + (x << 3) + c - 48;
    if (neg)
        x *= -1;
}

int main() {
    int Q;
//    cin >> N >> Q;
//    scanf("%d", &N);
//    scanf("%d", &Q);
    fastscan(N);
    fastscan(Q);
//    c.resize(N);
//    cnt.resize(N + 1);

    REP(i, N) {
//        cin >> c[i];
//        scanf("%d", &c[i]);
        fastscan(c[i]);
    }

    Mo ei1333;

    REP(i, Q) {
        int l, r;
//        cin >> l >> r;
//        scanf("%d", &l);
//        scanf("%d", &r);
        fastscan(l);
        fastscan(r);
        l--;
        ei1333.insert(l, r);
    }

    ei1333.build();

//    vector<int> ans(Q);
    int ans[512345];
    int q = Q;
//    REP(i, Q) {
//    while (true) {
    while (q--) {
        int idx = ei1333.query();
//        if (idx == -1) break;
        ans[idx] = val;
//        ans[ei1333.query()] = val;
    }

//    string res = "";
    for (int i = 0; i < Q; ++i) {
        printf("%d\n", ans[i]);
//        cout << ans[i] << endl;
//        res += to_string(ans[i]);
//        res += '\n';
    }
//    printf("%s", res);

    return 0;
}


//int main() {
////    cin.tie(0);
////    ios::sync_with_stdio(false);
////    cout << fixed << setprecision(10);
////    std::ifstream in("input.txt");
////    std::cin.rdbuf(in.rdbuf());
//
//    solve();
//
//
//    return 0;
//}


Compiler output:
a.cc: In member function 'int Mo::query()':
a.cc:137:44: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
  137 |         while (qr < queryR[idx[pt]]) add(qr++);
      |                                          ~~^~
a.cc:146:26: note:   initializing argument 1 of 'void Mo::add(int&)'
  146 |     inline void add(int &x);
      |                     ~~~~~^
a.cc:141:44: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
  141 |         while (ql < queryL[idx[pt]]) del(ql++);
      |                                          ~~^~
a.cc:148:26: note:   initializing argument 1 of 'void Mo::del(int&)'
  148 |     inline void del(int &x);
      |                     ~~~~~^