Language: C++

// https://ei1333.hateblo.jp/entry/2017/09/11/211011

#include <vector>

struct Mo
{
  vector< int > left, right, order;
  vector< bool > v;
  int width;
  int nl, nr, ptr;

  Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}

  void insert(int l, int r) /* [l, r) */
  {
    left.push_back(l);
    right.push_back(r);
  }

  /* ソート */
  void build()
  {
    order.resize(left.size());
    iota(begin(order), end(order), 0);
    sort(begin(order), end(order), [&](int a, int b)
    {
      if(left[a] / width != left[b] / width) return left[a] < left[b];
      return right[a] < right[b];
    });
  }

  /* クエリを 1 つぶんすすめて, クエリのidを返す */
  int process()
  {
    if(ptr == order.size()) return (-1);
    const auto id = order[ptr];
    while(nl > left[id]) distribute(--nl);
    while(nr < right[id]) distribute(nr++);
    while(nl < left[id]) distribute(nl++);
    while(nr > right[id]) distribute(--nr);
    return (order[ptr++]);
  }

  inline void distribute(int idx)
  {
    v[idx].flip();
    if(v[idx]) add(idx);
    else del(idx);
  }

  void add(int idx);

  void del(int idx);
};

int N, A[300000], Q;
int ans[200000];
int cnt[1000001], sum;

void Mo::add(int idx)
{
  if(cnt[A[idx]]++ == 0) ++sum;
}

void Mo::del(int idx)
{
  if(--cnt[A[idx]] == 0) --sum;
}

int main()
{
  scanf("%d %d", &N, &Q);
  for(int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  Mo mo(N);
  for(int i = 0; i < Q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    mo.insert(--a, b);
  }
  mo.build();
  for(int i = 0; i < Q; i++) {
    ans[mo.process()] = sum;
  }
  for(int i = 0; i < Q; i++) {
    printf("%d\n", ans[i]);
  }
}

Compiler output:
a.cc:7:3: error: 'vector' does not name a type
    7 |   vector< int > left, right, order;
      |   ^~~~~~
a.cc:8:3: error: 'vector' does not name a type
    8 |   vector< bool > v;
      |   ^~~~~~
a.cc: In constructor 'Mo::Mo(int)':
a.cc:12:27: error: 'sqrt' was not declared in this scope
   12 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |                           ^~~~
a.cc:12:59: error: class 'Mo' does not have any field named 'v'
   12 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |                                                           ^
a.cc: In member function 'void Mo::insert(int, int)':
a.cc:16:5: error: 'left' was not declared in this scope
   16 |     left.push_back(l);
      |     ^~~~
a.cc:17:5: error: 'right' was not declared in this scope
   17 |     right.push_back(r);
      |     ^~~~~
a.cc: In member function 'void Mo::build()':
a.cc:23:5: error: 'order' was not declared in this scope
   23 |     order.resize(left.size());
      |     ^~~~~
a.cc:23:18: error: 'left' was not declared in this scope
   23 |     order.resize(left.size());
      |                  ^~~~
a.cc:24:10: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   24 |     iota(begin(order), end(order), 0);
      |          ^~~~~
      |          std::begin
In file included from /usr/include/c++/14/vector:69,
                 from a.cc:3:
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:24:24: error: 'end' was not declared in this scope; did you mean 'std::end'?
   24 |     iota(begin(order), end(order), 0);
      |                        ^~~
      |                        std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:24:5: error: 'iota' was not declared in this scope
   24 |     iota(begin(order), end(order), 0);
      |     ^~~~
a.cc: In lambda function:
a.cc:28:14: error: 'right' was not declared in this scope
   28 |       return right[a] < right[b];
      |              ^~~~~
a.cc: In member function 'void Mo::build()':
a.cc:25:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   25 |     sort(begin(order), end(order), [&](int a, int b)
      |     ^~~~
      |     short
a.cc: In member function 'int Mo::process()':
a.cc:35:15: error: 'order' was not declared in this scope
   35 |     if(ptr == order.size()) return (-1);
      |               ^~~~~
a.cc:36:21: error: 'order' was not declared in this scope
   36 |     const auto id = order[ptr];
      |                     ^~~~~
a.cc:37:16: error: 'left' was not declared in this scope
   37 |     while(nl > left[id]) distribute(--nl);
      |                ^~~~
a.cc:38:16: error: 'right' was not declared in this scope
   38 |     while(nr < right[id]) distribute(nr++);
      |                ^~~~~
a.cc:39:16: error: 'left' was not declared in this scope
   39 |     while(nl < left[id]) distribute(nl++);
      |                ^~~~
a.cc:40:16: error: 'right' was not declared in this scope
   40 |     while(nr > right[id]) distribute(--nr);
      |                ^~~~~
a.cc: In member function 'void Mo::distribute(int)':
a.cc:46:5: error: 'v' was not declared in this scope
   46 |     v[idx].flip();
      |     ^
a.cc: In function 'int main()':
a.cc:72:3: error: 'scanf' was not declared in this scope
   72 |   scanf("%d %d", &N, &Q);
      |   ^~~~~
a.cc:87:5: error: 'printf' was not declared in this scope
   87 |     printf("%d\n", ans[i]);
      |     ^~~~~~
a.cc:4:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
    3 | #include <vector>
  +++ |+#include <cstdio>
    4 |