Language: C++

>
#define ll long long
#define ar array
#define AC ios_base::sync_with_stdio(0); cin.tie(NULL);
const int M = 1e9+7;
using namespace std;
const int N = 5*1e5;
int n, m;
int arr[N+5];
int occur[N+5];
struct node{
    int l, r, i;
} q[N];
int block;
int ans = 0;
int result[N+5];

bool cmp(node a, node b){
    if(a.l/block != b.l/block)
        return a.l/block<b.l/block;
    return a.r < b.r;
}

void add(int pos){
    occur[arr[pos]]++;
    if(occur[arr[pos]]==1)
        ans++;
}

void remove(int pos){
    occur[arr[pos]]--;
    if(occur[arr[pos]]==0)
        ans--;
}


int main(){
    AC
    cin>>n>>m;
    block = sqrt(n);
    for(int i=0; i<n; i++){
        cin>>arr[i];
    }
    for(int i=0; i<m; i++){
        int l, r;
        cin>>l>>r;
        q[i].l = --l;
        q[i].r = --r;
        q[i].i = i;
    }

    sort(q, q+m, cmp);
    /*
    for(int i=0; i<m; i++){
        cout<<q[i].l<<" "<<q[i].r<<endl;
    }
    cout<<"-------------------"<<endl;
    */

    int cur_l = q[0].l, cur_r = q[0].r;
    for(int i = cur_l; i<=cur_r; i++)
        add(i);
    result[q[0].i] = ans;

    for(int i=1; i<m; i++){
        int L=q[i].l, R=q[i].r;
        while(cur_l<L){
            remove(cur_l);
            cur_l++;
        }
        while(cur_l>L){
            add(cur_l-1);
            cur_l--;
        }
        while(cur_r<R){
            add(cur_r+1);
            cur_r++;
        }
        while(cur_r>R){
            remove(cur_r);
            cur_r--;
        }
        result[q[i].i] = ans;
    }
    for(int i=0; i<m; i++)
        cout<<result[i]<<endl;
}

Compiler output:
a.cc:1:1: error: expected unqualified-id before '>' token
    1 | >
      | ^
a.cc: In function 'int main()':
a.cc:4:12: error: 'ios_base' has not been declared
    4 | #define AC ios_base::sync_with_stdio(0); cin.tie(NULL);
      |            ^~~~~~~~
a.cc:38:5: note: in expansion of macro 'AC'
   38 |     AC
      |     ^~
a.cc:4:42: error: 'cin' was not declared in this scope
    4 | #define AC ios_base::sync_with_stdio(0); cin.tie(NULL);
      |                                          ^~~
a.cc:38:5: note: in expansion of macro 'AC'
   38 |     AC
      |     ^~
a.cc:1:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | >
a.cc:4:50: error: 'NULL' was not declared in this scope
    4 | #define AC ios_base::sync_with_stdio(0); cin.tie(NULL);
      |                                                  ^~~~
a.cc:38:5: note: in expansion of macro 'AC'
   38 |     AC
      |     ^~
a.cc:1:1: note: 'NULL' is defined in header '<cstddef>'; this is probably fixable by adding '#include <cstddef>'
  +++ |+#include <cstddef>
    1 | >
a.cc:40:13: error: 'sqrt' was not declared in this scope
   40 |     block = sqrt(n);
      |             ^~~~
a.cc:52:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   52 |     sort(q, q+m, cmp);
      |     ^~~~
      |     short
a.cc:86:9: error: 'cout' was not declared in this scope
   86 |         cout<<result[i]<<endl;
      |         ^~~~
a.cc:86:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:86:26: error: 'endl' was not declared in this scope
   86 |         cout<<result[i]<<endl;
      |                          ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | >