Language: C++

#include <cstdio>
#include <algorithm>

using namespace std;

struct req {
    int l, r, val, idx;
    bool t;
};

bool operator <( req a, req b ) {
    if ( a.r == b.r ) {
        return a.t < b.t;
    }
    return a.r < b.r;
}

int bit[ 30001 ];

void add( int y, int val ) {
    while ( y <= 30000 ) {
        bit[ y ] += val;
        y += ( y & -y );
    }
}

int sum( int y ) {
    int res = 0;
    while ( y > 0 ) {
        res += bit[ y ];
        y -= ( y & -y );
    }
    return res;
}

bool visited[ 2000001 ];
req array[ 600000 ];
int main() {
    int N, Q, i;
    int *prev = new int[ 2000001 ];
    for ( i = 0; i <= 100000; ++i ) {
        visited[ i ] = false;
        prev[ i ] = 0;
    }
    scanf( "%d", &N );
    scanf( "%d", &Q );
    for ( i = 1; i <= N; ++i ) {
        add( i, 1 );
    }
    for ( i = 0; i < N; ++i ) {
        scanf( "%d", &array[ i ].val );
        array[ i ].idx = i + 1;
        array[ i ].t = 0;
        array[ i ].r = i + 1;
    }
    for ( i = N; i < N + Q; ++i ) {
        scanf( "%d%d", &array[ i ].l, &array[ i ].r );
        array[ i ].idx = i - N;
        array[ i ].t = 1;
    }
    sort( array, array + N + Q );
    int ans[ Q ];
    for ( i = 0; i < N + Q; ++i ) {
        if ( array[ i ].t ) {
            ans[ array[ i ].idx ] = sum( array[ i ].r ) - sum( array[ i ].l - 1 );
        }
        else {
            if ( visited[ array[ i ].val ] ) {
                if ( prev[ array[ i ].val ] != 0 ) {
                    add( prev[ array[ i ].val ], -1 );
                }
            }
            visited[ array[ i ].val ] = true;
            prev[ array[ i ].val ] = array[ i ].idx;
        }
    }
    for ( i = 0; i < Q; ++i ) {
        printf( "%d\n", ans[ i ] );
    }
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:51:23: error: reference to 'array' is ambiguous
   51 |         scanf( "%d", &array[ i ].val );
      |                       ^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:52:9: error: reference to 'array' is ambiguous
   52 |         array[ i ].idx = i + 1;
      |         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:53:9: error: reference to 'array' is ambiguous
   53 |         array[ i ].t = 0;
      |         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:54:9: error: reference to 'array' is ambiguous
   54 |         array[ i ].r = i + 1;
      |         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:57:25: error: reference to 'array' is ambiguous
   57 |         scanf( "%d%d", &array[ i ].l, &array[ i ].r );
      |                         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:57:40: error: reference to 'array' is ambiguous
   57 |         scanf( "%d%d", &array[ i ].l, &array[ i ].r );
      |                                        ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:58:9: error: reference to 'array' is ambiguous
   58 |         array[ i ].idx = i - N;
      |         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:59:9: error: reference to 'array' is ambiguous
   59 |         array[ i ].t = 1;
      |         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:61:11: error: reference to 'array' is ambiguous
   61 |     sort( array, array + N + Q );
      |           ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:61:18: error: reference to 'array' is ambiguous
   61 |     sort( array, array + N + Q );
      |                  ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:64:14: error: reference to 'array' is ambiguous
   64 |         if ( array[ i ].t ) {
      |              ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:65:18: error: reference to 'array' is ambiguous
   65 |             ans[ array[ i ].idx ] = sum( array[ i ].r ) - sum( array[ i ].l - 1 );
      |                  ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:65:42: error: reference to 'array' is ambiguous
   65 |             ans[ array[ i ].idx ] = sum( array[ i ].r ) - sum( array[ i ].l - 1 );
      |                                          ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:65:64: error: reference to 'array' is ambiguous
   65 |             ans[ array[ i ].idx ] = sum( array[ i ].r ) - sum( array[ i ].l - 1 );
      |                                                                ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:68:27: error: reference to 'array' is ambiguous
   68 |             if ( visited[ array[ i ].val ] ) {
      |                           ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:69:28: error: reference to 'array' is ambiguous
   69 |                 if ( prev[ array[ i ].val ] != 0 ) {
      |                            ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:70:32: error: reference to 'array' is ambiguous
   70 |                     add( prev[ array[ i ].val ], -1 );
      |                                ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:73:22: error: reference to 'array' is ambiguous
   73 |             visited[ array[ i ].val ] = true;
      |                      ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:74:19: error: reference to 'array' is ambiguous
   74 |             prev[ array[ i ].val ] = array[ i ].idx;
      |                   ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~
a.cc:74:38: error: reference to 'array' is ambiguous
   74 |             prev[ array[ i ].val ] = array[ i ].idx;
      |                                      ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:37:5: note:                 'req array [600000]'
   37 | req array[ 600000 ];
      |     ^~~~~