Language: C++

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 10;

int n, m, size, num;
int w[N], cnt[N], belong[N];
int now, ans[N];

struct query{
	int l, r, id;
}q[N];

//bool cmp(query a, query b)
//{
//	return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
//}
bool cmp(query a, query b) {
    return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
}

void add(int pos)
{
	if(!cnt[w[pos]]) ++now;
	++cnt[w[pos]];
}

void del(int pos)
{
	--cnt[w[pos]];
	if(!cnt[w[pos]]) --now;
}

int main()
{
	scanf("%d%d", &n, &m);
	size = sqrt(n);
	num = ceil((double) n / size);
//	cout << num << endl;
	for (int i = 1; i <= num; i ++)
	{
		for (int j = (i - 1) * size + 1; j <= i * size; j ++)
		{
			belong[j] = i;
		}
	}
	for (int i = 1; i <= n; i ++) scanf("%d", &w[i]);
	for (int i = 1; i <= m; i ++)
	{
		q[i].id = i;
		scanf("%d%d", &q[i].l, &q[i].r);
	}
	sort(q + 1, q + m + 1, cmp);
	int l = 1, r = 0;
	for (int i = 1; i <= m; i ++)
	{
		int ql = q[i].l, qr = q[i].r;
//		while(l < ql) now -= !--cnt[w[l++]];
//        while(l > ql) now += !cnt[w[--l]]++;
//        while(r < qr) now += !cnt[w[++r]]++;
//        while(r > qr) now -= !--cnt[w[r--]];
//        ans[q[i].id] = now;
		while(l < ql) del(l ++);
		while(l > ql) add(-- l);
		while(r < qr) add(++ r);
		while(r > qr) del(r --);
		ans[q[i].id] = now;
	}
	for (int i = 1; i <= m; i ++)
		printf("%d\n", ans[i]);
}

Compiler output:
a.cc: In function 'int main()':
a.cc:38:9: error: reference to 'size' is ambiguous
   38 |         size = sqrt(n);
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:11: note:                 'int size'
    7 | int n, m, size, num;
      |           ^~~~
a.cc:39:33: error: reference to 'size' is ambiguous
   39 |         num = ceil((double) n / size);
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:11: note:                 'int size'
    7 | int n, m, size, num;
      |           ^~~~
a.cc:43:40: error: reference to 'size' is ambiguous
   43 |                 for (int j = (i - 1) * size + 1; j <= i * size; j ++)
      |                                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:11: note:                 'int size'
    7 | int n, m, size, num;
      |           ^~~~
a.cc:43:59: error: reference to 'size' is ambiguous
   43 |                 for (int j = (i - 1) * size + 1; j <= i * size; j ++)
      |                                                           ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:11: note:                 'int size'
    7 | int n, m, size, num;
      |           ^~~~