Language: C++

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef unsigned long long ull;

#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ep emplace_back
#define EL printf("\n")
#define sz(A) (int) A.size()
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define FOD(i,r,l) for (int i=r;i>=l;i--)
#define fillchar(a,x) memset(a, x, sizeof (a))
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);


struct data {
	int i, j, type, id;
};

const int   N = 30001, Q = 200001, A = 1000001;
int         n, q, res[Q], last_pos[A], bit[N];
data        a[N+Q];

bool cmp(data u, data v) {
	return (u.j < v.j or (u.j == v.j and u.type < v.type));
}

void update(int x, int k) {
	for (x; x<=n; x+=x&-x) bit[x] += k;
}

int get(int x) {
	int ans = 0;
	for (x; x>0; x-=x&-x) ans += bit[x];
	return ans;
}



int main() {

	scanf("%d", &n);
	for (int i=1; i<=n; i++)
		scanf("%d", &a[i].i), a[i].j = i, a[i].type = -1;
	scanf("%d", &q);
	for (int i=1; i<=q; i++)
		scanf("%d%d", &a[i+n].i, &a[i+n].j), a[i+n].id = i;

	sort(a+1,a+n+q+1,cmp);

	for (int i=1; i<=n+q; i++) {
		if (a[i].type == -1) {
			update(a[i].j, 1);
			if (last_pos[a[i].i] > 0) update(last_pos[a[i].i], -1);
			last_pos[a[i].i] = a[i].j;
		} else {
			res[a[i].id] = get(a[i].j) - get(a[i].i-1);
		}
	}

	for (int i=1; i<=q; i++) printf("%d\n", res[i]);

	return 0;
}


Compiler output:
a.cc:28:1: error: reference to 'data' is ambiguous
   28 | data        a[N+Q];
      | ^~~~
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:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:22:8: note:                 'struct data'
   22 | struct data {
      |        ^~~~
a.cc:30:10: error: reference to 'data' is ambiguous
   30 | bool cmp(data u, data v) {
      |          ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:22:8: note:                 'struct data'
   22 | struct data {
      |        ^~~~
a.cc:30:18: error: reference to 'data' is ambiguous
   30 | bool cmp(data u, data v) {
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:22:8: note:                 'struct data'
   22 | struct data {
      |        ^~~~
a.cc:30:24: error: expression list treated as compound expression in initializer [-fpermissive]
   30 | bool cmp(data u, data v) {
      |                        ^
a.cc: In function 'int main()':
a.cc:50:30: error: 'a' was not declared in this scope
   50 |                 scanf("%d", &a[i].i), a[i].j = i, a[i].type = -1;
      |                              ^
a.cc:53:32: error: 'a' was not declared in this scope
   53 |                 scanf("%d%d", &a[i+n].i, &a[i+n].j), a[i+n].id = i;
      |                                ^
a.cc:55:14: error: 'a' was not declared in this scope
   55 |         sort(a+1,a+n+q+1,cmp);
      |              ^