Language: C++

#include <bits/stdc++.h>
using namespace std;
#pragma region
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
#define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

int n, q;
vector<int> a;
int used[555555];
int cnt = 0;
void add(int nm)
{
    if (++used[nm] == 1)
    {
        ++cnt;
    }
}
void erase(int nm)
{
    if (--used[nm] == 0)
    {
        --cnt;
    }
}
int ans[555555];
void solve()
{
    scanf("%lld%lld", &n, &q);

    a.resize(n);
    forn(i, n) scanf("%lld", &a[i]);
    vector<tuple<int,int,int>> qry(q);
    forn(i, q) scanf("%lld%lld", &get<0>(qry[i]), &get<1>(qry[i])), get<2>(qry[i]) = i;
    int sz = 745;
    sort(qry.begin(), qry.end(), [&](auto &l, auto &r) {
        int ldx = get<1>(l) / sz;
        int rdx = get<1>(r) / sz;
        return ldx==rdx?l.f<r.f:ldx<rdx;
    });
    int lf = 0, rg = 0;
    add(a[0]);
    for (auto &cur : qry)
    {
        int nlf = get<0>(cur), nrg = get<1>(cur);
        for (int i = lf; i < nlf; ++i)
            if (--used[a[i]] == 0)
            {
                --cnt;
            }
        for (int i = lf - 1; i >= nlf; --i)
            if (++used[a[i]] == 1)
            {
                ++cnt;
            }
        for (int i = rg + 1; i <= nrg; ++i)
            if (++used[a[i]] == 1)
            {
                ++cnt;
            }
        for (int i = rg; i > nrg; --i)
            if (--used[a[i]] == 0)
            {
                --cnt;
            }
        lf = nlf;
        rg = nrg;
        ans[get<2>(cur)] = cnt;
    }

    for (int i = 0; i < q; ++i)
        printf("%lld\n", ans[i]);
}
signed main(void)
{
    int T = 1; //scanf("%lld", &T);
    while(T--) solve();
    return 0;
}

Compiler output:
a.cc: In instantiation of 'solve()::<lambda(auto:28&, auto:29&)> [with auto:28 = std::tuple<long long int, long long int, long long int>; auto:29 = std::tuple<long long int, long long int, long long int>]':
/usr/include/c++/14/bits/predefined_ops.h:158:30:   required from 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::tuple<long long int, long long int, long long int>*, std::vector<std::tuple<long long int, long long int, long long int> > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::tuple<long long int, long long int, long long int>*, std::vector<std::tuple<long long int, long long int, long long int> > >; _Compare = solve()::<lambda(auto:28&, auto:29&)>]'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<tuple<long long int, long long int, long long int>*, vector<tuple<long long int, long long int, long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<solve()::<lambda(auto:28&, auto:29&)> >]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<tuple<long long int, long long int, long long int>*, vector<tuple<long long int, long long int, long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<solve()::<lambda(auto:28&, auto:29&)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<tuple<long long int, long long int, long long int>*, vector<tuple<long long int, long long int, long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<solve()::<lambda(auto:28&, auto:29&)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<tuple<long long int, long long int, long long int>*, vector<tuple<long long int, long long int, long long int> > >; _Compare = solve()::<lambda(auto:28&, auto:29&)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:69:9:   required from here
   69 |     sort(qry.begin(), qry.end(), [&](auto &l, auto &r) {
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |         int ldx = get<1>(l) / sz;
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~
   71 |         int rdx = get<1>(r) / sz;
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |         return ldx==rdx?l.f<r.f:ldx<rdx;
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |     });
      |     ~~   
a.cc:35:11: error: 'class std::tuple<long long int, long long int, long long int>' has no member named 'first'
   35 | #define f first
      |           ^
a.cc:72:27: note: in expansion of macro 'f'
   72 |         return ldx==rdx?l.f<r.f:ldx<rdx;
      |                           ^
a.cc:35:11: error: 'class std::tuple<long long int, long long int, long long int>' has no member named 'first'
   35 | #define f first
      |           ^
a.cc:72:31: note: in expansion of macro 'f'
   72 |         return ldx==rdx?l.f<r.f:ldx<rdx;
      |                               ^