Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define UP(a,b,c) for(ll (a)=(b);(a)<(c);(a)++)
#define UU(a,b,c) for(ll (a)=(b);(a)<=(c);(a)++)
#define DN(a,b,c) for(ll (a)=(b);(a)>(c);(a)--)
#define DU(a,b,c) for(ll (a)=(b);(a)>=(c);(a)--)

set<int>segment[2000005]; 
int arr[500005], n, q, diff = 0;
set<int>ans;
bool flag[500005] = {0};
int size = 0;

void reset()
{
	ans.clear();
}

void build(int i, int l, int r) 
{ 
	if(l == r)
	{ 
		segment[i].insert(arr[l]); 
		return; 
	} 

	int m = (r+l)/2;
	
	build(2 * i, l, m); 
	build(1 + 2 * i, m + 1, r); 

	segment[i].insert(segment[2 * i].begin(), segment[2 * i].end()); 

	segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end()); 
} 

void query(int idx, int l, int r, int tl, int tr) 
{
	if(ans.size() == diff || ans.size() == size)
	{
		return ;
	}
	if(tr < l || tl > r) 
	{
		return ;
	}

	if(tl <= l && r <= tr)
	{
		ans.insert(segment[idx].begin(), segment[idx].end());
		return ;
	}

	int m = (r+l)/2;
	query(2 * idx, l, m, tl, tr);
	query(2 * idx + 1, m + 1, r, tl, tr);
} 

void Get(int l, int r) 
{ 
	reset();
	query(1, 0, n - 1, l, r); 
	printf("%d\n",ans.size());
} 

int main() 
{ 
	scanf("%d %d",&n, &q);
	
	UP(i,0,n)
	{
		scanf("%d", &arr[i]);
		if(flag[arr[i]] == 0)
		{
			flag[arr[i]] = 1;
			diff++;
		}
	}
	build(1, 0, n - 1); 
	
	int l, r;
	UP(i,0,q)
	{
		scanf("%d %d", &l, &r);
		size = r - l + 1;
		Get(l - 1, r - 1);
	}
	return 0; 
} 


Compiler output:
a.cc: In function 'void query(int, int, int, int, int)':
a.cc:45:48: error: reference to 'size' is ambiguous
   45 |         if(ans.size() == diff || ans.size() == size)
      |                                                ^~~~
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:18:5: note:                 'int size'
   18 | int size = 0;
      |     ^~~~
a.cc: In function 'int main()':
a.cc:91:17: error: reference to 'size' is ambiguous
   91 |                 size = r - l + 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:18:5: note:                 'int size'
   18 | int size = 0;
      |     ^~~~