Language: C++

#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#define ll long long
#define lson (rt<< 1)
#define rson (rt<< 1 | 1)
#define gmid ((l+r)>> 1 )
using namespace std;

const int maxn=20000050;

int n,q,a[maxn],f[maxn],ans[maxn],v[maxn];

struct node
{
	int l,r,ans;
}p[maxn];

int lowbit(int x)
{
	return x&(x^(x-1));
}

void add(int pos,int val)
{
	int i=pos;
	while(i<=n)
	  {
	  	  f[i]+=val;
	  	  i+=lowbit(i);
	  } 
}

int sum(int pos)
{
	int ans=0,i=pos;
    while(i>0)
    {
    	ans+=f[i];
    	i-=lowbit(i);
    }
    return ans;
}

int main()
{
	cin>>n>>q;
	for(int i=1;i<=n;++i)
	  cin>>a[i];
	for(int i=1;i<=q;++i)
	  cin>>p[i].l>>p[i].r;
	sort(p+1,p+1+q,[&](P x,P y)
	{
		return x.r<y.r;
	});
	int r=1;
	for(int i=1;i<=m;++i)
	  {
	  	 for(int j=r;j<=p[i].r;++j)
	  	   {
	  	   	  if(v[a[j]]) add(v[a[j]],-1);
	  	   	  add(j,1);
			  v[a[j]]=j;	     
	  	   }
	  	 r=p[i].r+1;
		 ans[p[i].pos]=sum(p[i].r)-sum(p[i].l-1);    
	  }
	for(int i=1;i<=m;++i)
	  cout<<ans[i]<<"\n";  
}


Compiler output:
a.cc: In function 'int main()':
a.cc:53:28: error: 'P' has not been declared
   53 |         sort(p+1,p+1+q,[&](P x,P y)
      |                            ^
a.cc:53:32: error: 'P' has not been declared
   53 |         sort(p+1,p+1+q,[&](P x,P y)
      |                                ^
a.cc: In lambda function:
a.cc:55:26: error: request for member 'r' in 'x', which is of non-class type 'int'
   55 |                 return x.r<y.r;
      |                          ^
a.cc:55:30: error: request for member 'r' in 'y', which is of non-class type 'int'
   55 |                 return x.r<y.r;
      |                              ^
a.cc: In function 'int main()':
a.cc:58:24: error: 'm' was not declared in this scope
   58 |         for(int i=1;i<=m;++i)
      |                        ^
a.cc:67:27: error: 'struct node' has no member named 'pos'
   67 |                  ans[p[i].pos]=sum(p[i].r)-sum(p[i].l-1);
      |                           ^~~
a.cc:69:24: error: 'm' was not declared in this scope
   69 |         for(int i=1;i<=m;++i)
      |                        ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = node*; _Iterator2 = node*; _Compare = main()::<lambda(int, int)>]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 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 = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 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 = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 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 = node*; _Compare = main()::<lambda(int, int)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:53:6:   required from here
   53 |         sort(p+1,p+1+q,[&](P x,P y)
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~
   54 |         {
      |         ~    
   55 |                 return x.r<y.r;
      |                 ~~~~~~~~~~~~~~~
   56 |         });
      |         ~~   
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: no match for call to '(main()::<lambda(int, int)>) (node&, node&)'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
a.cc:53:24: note: candidate: 'main()::<lambda(int, int)>'
   53 |         sort(p+1,p+1+q,[&](P x,P y)
      |                        ^
a.cc:53:24: note:   no known conversion for argument 1 from 'node' to 'int'
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = node; _Iterator = node*; _Compare = main()::<lambda(int, int)>]':
/usr/include/c++/14/bits/stl_algo.h:1757:20:   required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Val_comp_iter<main()::<lambda(int, int)> >]'
 1757 |       while (__comp(__val, __next))
      |              ~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1785:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1785 |             std::__unguarded_linear_insert(__i,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
 1786 |                                 __gnu_cxx::__ops::__val_comp_iter(__comp));
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 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 = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 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 = node*; _Compare = main()::<lambda(int, int)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:53:6:   required from here
   53 |         sort(p+1,p+1+q,[&](P x,P y)
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~
   54 |         {
      |         ~    
   55 |                 return x.r<y.r;
      |                 ~~~~~~~~~~~~~~~
   56 |         });
      |         ~~   
/usr/include/c++/14/bits/predefined_ops.h:240:30: error: no match for call to '(main()::<lambda(int, int)>) (node&, node&)'
  240 |         { return bool(_M_comp(__val, *__it)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
a.cc:53:24: note: candidate: 'main()::<lambda(int, int)>'
   53 |         sort(p+1,p+1+q,[&](P x,P y)
      |                        ^
a.cc:53:24: note:   no known conversion for argument 1 from 'node' to 'int'
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = node*; _Value = node; _Compare = main()::<lambda(int, int)>]':
/usr/include/c++/14/bits/stl_heap.h:140:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = node*; _Distance = long int; _Tp = node; _Compare = __gnu_cxx::__ops::_Iter_comp_val<main()::<lambda(int, int)> >]'
  140 |       while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
      |                                          ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:247:23:   required from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = node*; _Distance = long int; _Tp = node; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
  247 |       std::__push_heap(__first, __holeIndex, __topIndex,
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  248 |                        _GLIBCXX_MOVE(__value), __cmp);
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:356:22:   required from 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
  356 |           std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
      |           ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  357 |                              __comp);
      |                              ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1593:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1593 |       std::__make_heap(__first, __middle, __comp);
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1868 |       std::__heap_select(__first, __middle, __last, __comp);
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1884:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = node*; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1884 |               std::__partial_sort(__first, __last, __last, __comp);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1905:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'